Class: Bowline::Observable
Defined Under Namespace
Classes: Event
Instance Method Summary collapse
- #append(event, method = nil, &block) ⇒ Object
- #call(event, *args) ⇒ Object
- #clear ⇒ Object
-
#initialize ⇒ Observable
constructor
A new instance of Observable.
- #remove(event, value = nil) ⇒ Object
Constructor Details
#initialize ⇒ Observable
Returns a new instance of Observable.
33 34 35 |
# File 'lib/bowline/observer.rb', line 33 def initialize @listeners = {} end |
Instance Method Details
#append(event, method = nil, &block) ⇒ Object
37 38 39 40 41 |
# File 'lib/bowline/observer.rb', line 37 def append(event, method = nil, &block) event = Event.new(self, event, method||block) (@listeners[event] ||= []) << event event end |
#call(event, *args) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/bowline/observer.rb', line 43 def call(event, *args) return unless @listeners[event] @listeners[event].each do |callback| callback.call(*args) end end |
#clear ⇒ Object
62 63 64 |
# File 'lib/bowline/observer.rb', line 62 def clear @listeners = {} end |
#remove(event, value = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bowline/observer.rb', line 50 def remove(event, value=nil) return unless @listeners[event] if value @listeners[event].delete(value) if @listeners[event].empty? @listeners.delete(event) end else @listeners.delete(event) end end |