Class: Event

Inherits:
Object show all
Defined in:
app/models/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.



26
27
28
# File 'app/models/event.rb', line 26

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



30
31
32
33
# File 'app/models/event.rb', line 30

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



6
7
8
# File 'app/models/event.rb', line 6

def object
  @object
end

#optionsObject (readonly)

optional options for the event



8
9
10
# File 'app/models/event.rb', line 8

def options
  @options
end

#sourceObject (readonly)

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



7
8
9
# File 'app/models/event.rb', line 7

def source
  @source
end

#typeObject (readonly)

what happened



5
6
7
# File 'app/models/event.rb', line 5

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 'app/models/event.rb', line 11

def trigger(type, object, source, options = {})
  event = 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