Class: Event
Constant Summary collapse
- @@observers =
[]
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
the object that the event is about, e.g.
-
#options ⇒ Object
readonly
optional options for the event.
-
#source ⇒ Object
readonly
the origin or the event, e.g.
-
#type ⇒ Object
readonly
what happened.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(type, object, source, options = {}) ⇒ Event
constructor
A new instance of Event.
- #method_missing(name, *args) ⇒ Object
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, = {}) @type, @object, @source, @options = type, object, source, 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
#object ⇒ Object (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 |
#options ⇒ Object (readonly)
optional options for the event
8 9 10 |
# File 'app/models/event.rb', line 8 def @options end |
#source ⇒ Object (readonly)
the origin or the event, e.g. payment processor
7 8 9 |
# File 'app/models/event.rb', line 7 def source @source end |
#type ⇒ Object (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, = {}) event = Event.new(type, object, source, ) 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 |