Class: Houston::Observer
- Inherits:
-
Object
- Object
- Houston::Observer
- Defined in:
- lib/houston_observer.rb
Instance Attribute Summary collapse
-
#async ⇒ Object
Returns the value of attribute async.
Instance Method Summary collapse
- #clear! ⇒ Object
- #fire(event, *args) ⇒ Object
-
#initialize ⇒ Observer
constructor
A new instance of Observer.
- #observed?(event) ⇒ Boolean
- #on(event, &block) ⇒ Object
- #once(event, &block) ⇒ Object
Constructor Details
#initialize ⇒ Observer
Returns a new instance of Observer.
6 7 8 9 |
# File 'lib/houston_observer.rb', line 6 def initialize @async = true clear! end |
Instance Attribute Details
#async ⇒ Object
Returns the value of attribute async.
11 12 13 |
# File 'lib/houston_observer.rb', line 11 def async @async end |
Instance Method Details
#clear! ⇒ Object
38 39 40 |
# File 'lib/houston_observer.rb', line 38 def clear! @observers = {} end |
#fire(event, *args) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/houston_observer.rb', line 30 def fire(event, *args) invoker = async ? method(:invoke_callback_async) : method(:invoke_callback) observers_of(event).each do |block| invoker.call(event, block, *args) end nil end |
#observed?(event) ⇒ Boolean
26 27 28 |
# File 'lib/houston_observer.rb', line 26 def observed?(event) observers_of(event).any? end |
#on(event, &block) ⇒ Object
13 14 15 16 |
# File 'lib/houston_observer.rb', line 13 def on(event, &block) observers_of(event).push(block) nil end |
#once(event, &block) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/houston_observer.rb', line 18 def once(event, &block) wrapped_block = Proc.new do |*args| block.call(*args) observers_of(event).delete wrapped_block end on(event, &wrapped_block) end |