Class: Bowline::Watcher
Defined Under Namespace
Modules: Base Classes: Callback
Instance Method Summary collapse
-
#append(event, method = nil, oneshot = false, &block) ⇒ Object
Add new method/proc to a specific event.
-
#call(event, *args) ⇒ Object
Call an event’s callbacks with provided arguments.
-
#clear ⇒ Object
Clear all events and callbacks.
-
#initialize ⇒ Watcher
constructor
A new instance of Watcher.
-
#remove(event, value = nil) ⇒ Object
Remove an specific callback on an event, or all an event’s callbacks.
Constructor Details
#initialize ⇒ Watcher
Returns a new instance of Watcher.
90 91 92 |
# File 'lib/bowline/watcher.rb', line 90 def initialize @listeners = {} end |
Instance Method Details
#append(event, method = nil, oneshot = false, &block) ⇒ Object
Add new method/proc to a specific event.
95 96 97 98 99 |
# File 'lib/bowline/watcher.rb', line 95 def append(event, method = nil, oneshot = false, &block) callback = Callback.new(self, event, method||block, oneshot) (@listeners[event] ||= []) << callback callback end |
#call(event, *args) ⇒ Object
Call an event’s callbacks with provided arguments.
102 103 104 105 106 107 |
# File 'lib/bowline/watcher.rb', line 102 def call(event, *args) return unless @listeners[event] @listeners[event].each do |callback| callback.call(*args) end end |
#clear ⇒ Object
Clear all events and callbacks.
124 125 126 |
# File 'lib/bowline/watcher.rb', line 124 def clear @listeners = {} end |
#remove(event, value = nil) ⇒ Object
Remove an specific callback on an event, or all an event’s callbacks.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bowline/watcher.rb', line 111 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 |