Class: Switcher::Listener
- Inherits:
-
Object
- Object
- Switcher::Listener
- Defined in:
- lib/switcher/listener.rb
Instance Attribute Summary collapse
-
#event_names ⇒ Object
readonly
Returns the value of attribute event_names.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #after(event, options = {}, &block) ⇒ Object
- #before(event, options = {}, &block) ⇒ Object
-
#initialize(name) ⇒ Listener
constructor
A new instance of Listener.
- #on(event, options = {}, &block) ⇒ Object
- #trigger(event, facade, instance, args) ⇒ Object
Constructor Details
#initialize(name) ⇒ Listener
Returns a new instance of Listener.
3 4 5 6 7 |
# File 'lib/switcher/listener.rb', line 3 def initialize(name) @events = {} @event_names = [] @name = name end |
Instance Attribute Details
#event_names ⇒ Object (readonly)
Returns the value of attribute event_names.
9 10 11 |
# File 'lib/switcher/listener.rb', line 9 def event_names @event_names end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
9 10 11 |
# File 'lib/switcher/listener.rb', line 9 def events @events end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/switcher/listener.rb', line 9 def name @name end |
Instance Method Details
#after(event, options = {}, &block) ⇒ Object
24 25 26 27 |
# File 'lib/switcher/listener.rb', line 24 def after(event, ={}, &block) data = { callback: block, options: } @events["after_#{event}"] = data end |
#before(event, options = {}, &block) ⇒ Object
11 12 13 14 15 |
# File 'lib/switcher/listener.rb', line 11 def before(event, ={}, &block) [:allow_switch] = true data = { callback: block, options: } @events["before_#{event}"] = data end |
#on(event, options = {}, &block) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/switcher/listener.rb', line 17 def on(event, ={}, &block) [:allow_switch] = true data = { callback: block, options: } @event_names << event.to_sym @events[event.to_s] = data end |
#trigger(event, facade, instance, args) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/switcher/listener.rb', line 29 def trigger(event, facade, instance, args) if ev = @events[event] if ev[:options][:call] instance.instance_exec(facade, *args) { |facad, *arguments| self.send(ev[:options][:call], facad, *arguments) } end instance.instance_exec(facade, *args, &ev[:callback]) if ev[:callback].respond_to?(:call) if !facade.stopped && ev[:options][:allow_switch] && switch_to = ev[:options][:switch_to] facade.switch_to(switch_to) end end end |