Class: Adva::Event

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

Constant Summary collapse

@@observers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, object, source, options = {}) ⇒ Event

Returns a new instance of Event.



25
26
27
# File 'lib/adva/event.rb', line 25

def initialize(type, object, source, options = {})
  @type, @object, @source, @options = type, object, source, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



29
30
31
32
# File 'lib/adva/event.rb', line 29

def method_missing(name, *args)
  return @options[name] if @options.has_key?(name)
  super
end

Instance Attribute Details

#objectObject (readonly)

the object that the event is about, e.g. payment



7
8
9
# File 'lib/adva/event.rb', line 7

def object
  @object
end

#optionsObject (readonly)

optional options for the event



9
10
11
# File 'lib/adva/event.rb', line 9

def options
  @options
end

#sourceObject (readonly)

the origin or the event, e.g. payment processor



8
9
10
# File 'lib/adva/event.rb', line 8

def source
  @source
end

#typeObject (readonly)

what happened



6
7
8
# File 'lib/adva/event.rb', line 6

def type
  @type
end

Class Method Details

.trigger(type, object, source, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/adva/event.rb', line 11

def self.trigger(type, object, source, options = {})
  event = new(type, object, source, options)
  observers.each do |observer|
    observer = observer.constantize if observer.is_a?(String)
    callback = :"handle_#{event.type}!"

    if observer.respond_to?(callback)
      observer.send(callback, event)
    elsif observer.respond_to?(:handle_event!)
      observer.handle_event!(event)
    end
  end
end