Class: CallbacksAttachable::CallbackRegistry
- Inherits:
-
Object
- Object
- CallbacksAttachable::CallbackRegistry
- Defined in:
- lib/all/callback_registry.rb
Instance Method Summary collapse
- #deregister(event, callback) ⇒ Object
-
#initialize(owner_class) ⇒ CallbackRegistry
constructor
A new instance of CallbackRegistry.
- #register(*events, opts, callback) ⇒ Object
- #registered?(event) ⇒ Boolean
- #trigger(instance, event, args) ⇒ Object
Constructor Details
#initialize(owner_class) ⇒ CallbackRegistry
Returns a new instance of CallbackRegistry.
3 4 5 6 |
# File 'lib/all/callback_registry.rb', line 3 def initialize(owner_class) @singleton_owner = owner_class.singleton_class? @callbacks = {} end |
Instance Method Details
#deregister(event, callback) ⇒ Object
25 26 27 28 |
# File 'lib/all/callback_registry.rb', line 25 def deregister(event, callback) @callbacks[event].delete callback.__id__ @callbacks.delete(event) if @callbacks[event].empty? end |
#register(*events, opts, callback) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/all/callback_registry.rb', line 8 def register(*events, opts, callback) callback = Callback.new(self, events, opts, !@singleton_owner, callback) events.each do |event| @callbacks[event] ||= {} @callbacks[event][callback.__id__] = callback end callback end |
#registered?(event) ⇒ Boolean
17 18 19 |
# File 'lib/all/callback_registry.rb', line 17 def registered?(event) @callbacks.key? event end |
#trigger(instance, event, args) ⇒ Object
21 22 23 |
# File 'lib/all/callback_registry.rb', line 21 def trigger(instance, event, args) @callbacks[event] and @callbacks[event].each_value{ |callback| callback.call(instance, args) } end |