class Evnt::Event

Events are used to save on a persistent data structure what happends on the system.

Attributes

attributes[R]

DEPRECATED: Attribute containings the list of attributes of the event.

extras[R]

Attribute containings the extra parameters of the event.

extras_attributes[R]

Attribute containings the list of extras attributes of the event.

name[R]

Attribute containings the name of the event.

payload[R]

Attribute containings the payload of the event.

payload_attributes[R]

Attribute containings the list of payload attributes of the event.

Public Class Methods

new(params = {}) click to toggle source

The constructor is used to initialize a new event. The parameters are validated and added to the payload of the event. The parameters with the _ in their name are not saved on the payload.

Attributes

  • params - The list of parameters for the event.

  • _options - The list of options for the event.

Options

  • silent - Boolean value used to avoid the call of the notify method of

handlers.

# File lib/evnt/event.rb, line 59
def initialize(params = {})
  _init_event_data(params)
  _validate_payload
  _validate_extras
  _run_event_steps
  _notify_handlers if @state[:saved]
end

Private Class Methods

add_handler(handler) click to toggle source

This function is used to add a new handler to the event from the external.

# File lib/evnt/event.rb, line 274
def add_handler(handler)
  @handlers ||= []
  @handlers.push(handler)
  event_handlers = @handlers

  define_method('_handlers', -> { return event_handlers })
end
attributes_are(*attributes) click to toggle source

DEPRECATED: This function sets the list of attributes for the event.

# File lib/evnt/event.rb, line 251
def attributes_are(*attributes)
  @payload_attributes ||= []
  @payload_attributes.concat(attributes).uniq!
  event_payload_attributes = @payload_attributes

  define_method('_payload_attributes', -> { return event_payload_attributes })
end
default_options(options) click to toggle source

This function sets the default options that should be used by the event.

# File lib/evnt/event.rb, line 219
def default_options(options)
  @options ||= {}
  @options.merge!(options)
  event_options = @options

  define_method('_default_options', -> { return event_options })
end
extras_attributes_are(*attributes) click to toggle source

This function sets the list of extras attributes for the event.

# File lib/evnt/event.rb, line 242
def extras_attributes_are(*attributes)
  @extras_attributes ||= []
  @extras_attributes.concat(attributes).uniq!
  event_extras_attributes = @extras_attributes

  define_method('_extras_attributes', -> { return event_extras_attributes })
end
handlers_are(handlers) click to toggle source

This function sets the list of handlers for the event.

# File lib/evnt/event.rb, line 260
def handlers_are(handlers)
  @handlers ||= []
  @handlers.concat(handlers)
  event_handlers = @handlers

  define_method('_handlers', -> { return event_handlers })
end
name_is(name) click to toggle source

This function sets the name for the event.

# File lib/evnt/event.rb, line 228
def name_is(name)
  define_method('_name', -> { return name })
end
payload_attributes_are(*attributes) click to toggle source

This function sets the list of payload attributes for the event.

# File lib/evnt/event.rb, line 233
def payload_attributes_are(*attributes)
  @payload_attributes ||= []
  @payload_attributes.concat(attributes).uniq!
  event_payload_attributes = @payload_attributes

  define_method('_payload_attributes', -> { return event_payload_attributes })
end
to_write_event(&block) click to toggle source

This function sets the write event function for the event.

# File lib/evnt/event.rb, line 269
def to_write_event(&block)
  define_method('_write_event', &block)
end

Public Instance Methods

reloaded?() click to toggle source

This function tells if the event is reloaded or not. The returned object should be a boolean value corresponding to the presence of evnt value inside the event params.

# File lib/evnt/event.rb, line 75
def reloaded?
  @state[:reloaded]
end
saved?() click to toggle source

This function tells if the event is saved or not. As default an event is considered saved. It should be updated to not saved when the to_write_event has some problems.

# File lib/evnt/event.rb, line 84
def saved?
  @state[:saved]
end
set_not_saved() click to toggle source

This function can be used to set the event as not saved. A not saved event should not notify handlers. If the exceptions option is active, it should raise a new error.

# File lib/evnt/event.rb, line 93
def set_not_saved
  @state[:saved] = false

  # raise error if event needs exceptions
  raise 'Event can not be saved' if @options[:exceptions]
end

Private Instance Methods

_generate_payload(params) click to toggle source

This function generates the complete event payload.

# File lib/evnt/event.rb, line 140
def _generate_payload(params)
  # add evnt informations
  params[:evnt] = {
    timestamp: Time.now.to_i,
    name: @name
  }
  # return payload
  params
end
_init_event_data(params) click to toggle source

This function initializes the event required data.

# File lib/evnt/event.rb, line 106
def _init_event_data(params)
  # set state
  @state = {
    reloaded: !params[:evnt].nil?,
    saved: true
  }

  # set options
  initial_options = {
    exceptions: false,
    silent: false
  }
  default_options = _safe_default_options || {}
  params_options = params[:_options] || {}
  @options = initial_options.merge(default_options)
                            .merge(params_options)

  # set name and attributes
  @name = _safe_name
  @payload_attributes = _safe_payload_attributes
  @extras_attributes = _safe_extras_attributes
  @attributes = _safe_payload_attributes # DEPRECATED

  # set payload
  payload = params.reject { |k, _v| k[0] == '_' }
  @payload = @state[:reloaded] ? payload : _generate_payload(payload)

  # set extras
  @extras = {}
  extras = params.select { |k, _v| k[0] == '_' && k != :_options }
  extras.each { |k, v| @extras[k[1..-1].to_sym] = v }
end
_notify_handlers() click to toggle source

This function notify all handlers for the event.

# File lib/evnt/event.rb, line 170
def _notify_handlers
  return if @options[:silent]
  handlers = _safe_handlers

  # notify every handler
  handlers.each do |handler|
    handler.notify(self)
  end
end
_run_event_steps() click to toggle source

This function calls requested steps for the event.

# File lib/evnt/event.rb, line 165
def _run_event_steps
  _safe_write_event
end
_safe_default_options() click to toggle source

Safe defined functions:

# File lib/evnt/event.rb, line 182
def _safe_default_options
  return _default_options if defined?(_default_options)
  {}
end
_safe_extras_attributes() click to toggle source
# File lib/evnt/event.rb, line 197
def _safe_extras_attributes
  return _extras_attributes if defined?(_extras_attributes)
  []
end
_safe_handlers() click to toggle source
# File lib/evnt/event.rb, line 202
def _safe_handlers
  return _handlers if defined?(_handlers)
  []
end
_safe_name() click to toggle source
# File lib/evnt/event.rb, line 187
def _safe_name
  return _name if defined?(_name)
  ''
end
_safe_payload_attributes() click to toggle source
# File lib/evnt/event.rb, line 192
def _safe_payload_attributes
  return _payload_attributes if defined?(_payload_attributes)
  []
end
_safe_write_event() click to toggle source
# File lib/evnt/event.rb, line 207
def _safe_write_event
  return _write_event if defined?(_write_event)
  nil
end
_validate_extras() click to toggle source

This function validates all extras and check they are completed.

# File lib/evnt/event.rb, line 158
def _validate_extras
  # check all attributes are present
  check_attr = @extras.keys.sort == @extras_attributes.sort
  puts 'Event extras are not correct; in future releases they will be required' unless check_attr
end
_validate_payload() click to toggle source

This function validates all payload and check they are completed.

# File lib/evnt/event.rb, line 151
def _validate_payload
  # check all attributes are present
  check_attr = (@payload.keys - [:evnt]).sort == @payload_attributes.sort
  raise 'Event parameters are not correct' unless check_attr
end