Class: Adva::Event
- Inherits:
-
Object
- Object
- Adva::Event
- Defined in:
- lib/adva/event.rb
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.
25 26 27 |
# File 'lib/adva/event.rb', line 25 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
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
#object ⇒ Object (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 |
#options ⇒ Object (readonly)
optional options for the event
9 10 11 |
# File 'lib/adva/event.rb', line 9 def @options end |
#source ⇒ Object (readonly)
the origin or the event, e.g. payment processor
8 9 10 |
# File 'lib/adva/event.rb', line 8 def source @source end |
#type ⇒ Object (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, = {}) 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 |