Class: Omega::Server::Event

Inherits:
Object show all
Defined in:
lib/omega/server/event.rb

Overview

Omega Event, tracks time and invokes handlers after time has expired

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Event

Omega::Server::Event initializer

Options Hash (args):

  • :timestamp, (Time)

    'timestamp' timestamp to assign to event

  • :handlers, (Array<Callable>)

    'handlers' callable objects to invoke on event


46
47
48
49
50
51
52
53
54
# File 'lib/omega/server/event.rb', line 46

def initialize(args = {})
  attr_from_args args, :timestamp => Time.now,
                       :handlers  =>       [],
                       :id        =>      nil,
                       :registry  =>      nil,
                       :type      =>      nil

  @timestamp = Time.parse(@timestamp) if @timestamp.is_a?(String)
end

Instance Attribute Details

#handlersObject

Callable objects to be invoked upon event


26
27
28
# File 'lib/omega/server/event.rb', line 26

def handlers
  @handlers
end

#idObject

Id of the event


14
15
16
# File 'lib/omega/server/event.rb', line 14

def id
  @id
end

#registryObject

Handle to registry event is running in


29
30
31
# File 'lib/omega/server/event.rb', line 29

def registry
  @registry
end

#timestampObject

Timestamp which event is set to occur


23
24
25
# File 'lib/omega/server/event.rb', line 23

def timestamp
  @timestamp
end

#typeObject

Optional type of event


17
18
19
# File 'lib/omega/server/event.rb', line 17

def type
  @type
end

Class Method Details

.json_create(o) ⇒ Object

Create new event from json representation


95
96
97
98
# File 'lib/omega/server/event.rb', line 95

def self.json_create(o)
  event = new(o['data'])
  return event
end

Instance Method Details

#event_argsObject

Optional args which may be attached to class. Meant for subclasses, not used in central omega subsystem


58
59
# File 'lib/omega/server/event.rb', line 58

def event_args
end

#handlers_jsonObject

Include handlers in json data


70
71
72
# File 'lib/omega/server/event.rb', line 70

def handlers_json
  {:handlers => handlers}
end

#invoke(*args) ⇒ Object

Invoke the registered handler w/ the specified args


64
65
66
67
# File 'lib/omega/server/event.rb', line 64

def invoke(*args)
  handlers.each { |h| h.call *args }
  @invoked = true
end

#json_dataObject

Return event json data


75
76
77
78
79
# File 'lib/omega/server/event.rb', line 75

def json_data
  {:id        => id,
   :type      => type,
   :timestamp => timestamp}.merge(handlers_json)
end

#should_exec?Boolean

Return boolean indicating if all checks pass for event execution


37
38
39
# File 'lib/omega/server/event.rb', line 37

def should_exec?
  time_elapsed?
end

#time_elapsed?Boolean

Return boolean if timestamp has elapsed


32
33
34
# File 'lib/omega/server/event.rb', line 32

def time_elapsed?
  @timestamp <= Time.now
end

#to_json(*a) ⇒ Object

Convert event to json representation and return it


82
83
84
85
86
87
# File 'lib/omega/server/event.rb', line 82

def to_json(*a)
  {
    'json_class' => self.class.name,
    'data'       => json_data
  }.to_json(*a)
end

#to_sObject

Convert event to human readable string and return it


90
91
92
# File 'lib/omega/server/event.rb', line 90

def to_s
  "event-#{@id}"
end