Class: Omega::Server::EventHandler

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

Overview

Encapsulates a handler which to be invoked on a future event, or one not already created

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ EventHandler


206
207
208
209
210
211
212
# File 'lib/omega/server/event.rb', line 206

def initialize(args = {}, &block)
  attr_from_args args, :event_id => nil,
                       :event_type => nil,
                       :handlers => [block].compact,
                       :persist  => false,
                       :endpoint_id => nil
end

Instance Attribute Details

#endpoint_idObject

RJR Node Endpoint which this handler is registered for


204
205
206
# File 'lib/omega/server/event.rb', line 204

def endpoint_id
  @endpoint_id
end

#event_idObject

Event ID which to look for


192
193
194
# File 'lib/omega/server/event.rb', line 192

def event_id
  @event_id
end

#event_typeObject

Event Type which to look for


195
196
197
# File 'lib/omega/server/event.rb', line 195

def event_type
  @event_type
end

#handlersObject

Handlers to invoke when event occurs


198
199
200
# File 'lib/omega/server/event.rb', line 198

def handlers
  @handlers
end

#persistObject

Set true to keep event handler in registry after execution


201
202
203
# File 'lib/omega/server/event.rb', line 201

def persist
  @persist
end

Class Method Details

.json_create(o) ⇒ Object

Create new handler from json representation


265
266
267
268
# File 'lib/omega/server/event.rb', line 265

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

Instance Method Details

#exec(&block) ⇒ Object


220
221
222
# File 'lib/omega/server/event.rb', line 220

def exec(&block)
  @handlers << block
end

#handlers_jsonObject

Include handlers in json data


232
233
234
# File 'lib/omega/server/event.rb', line 232

def handlers_json
  {:handlers => handlers}
end

#invoke(*args) ⇒ Object

Run handlers, note this method *isn't* invoked in registry event cycle


225
226
227
228
229
# File 'lib/omega/server/event.rb', line 225

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

#json_dataObject

Return event handler json data


237
238
239
240
241
242
# File 'lib/omega/server/event.rb', line 237

def json_data
  {:event_id    => event_id,
   :event_type  => event_type,
   :persist     => persist,
   :endpoint_id => endpoint_id}.merge(handlers_json)
end

#matches?(event) ⇒ Boolean

Return bool indicating if this handles is meant for the specified event


215
216
217
218
# File 'lib/omega/server/event.rb', line 215

def matches?(event)
  (event_id.nil?   || event_id   == event.id) &&
  (event_type.nil? || event_type == event.type)
end

#to_json(*a) ⇒ Object

Convert handler to json representation and return it

XXX handlers allows us to pass procs through registry serialization to where they need to be used which is required but is iffy at best (eg registry serialization shouldn't be bypassed though there is

no security vulnerabilty because of this AFAIK).

Look into a more robust solution for this (perhaps static EventHandler specific callbacks or something similar to the missions dsl proxy system)

Also applies to to_json methods in EventHandler subclasses


257
258
259
260
261
262
# File 'lib/omega/server/event.rb', line 257

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