Module: Cura::Attributes::HasEvents::ClassMethods
- Defined in:
- lib/cura/attributes/has_events.rb
Overview
The class methods to be mixed in when included.
Instance Method Summary collapse
-
#callbacks ⇒ Hash<Symbol,Array<Proc>>
The callbacks stored on this class.
-
#inherited(subclass) ⇒ Object
Register this classes callbacks onto the subclass, when inherited.
-
#on_event(event_name = :default) { ... } ⇒ Proc
Store a callback on this class.
Instance Method Details
#callbacks ⇒ Hash<Symbol,Array<Proc>>
The callbacks stored on this class.
20 21 22 |
# File 'lib/cura/attributes/has_events.rb', line 20 def callbacks @callbacks ||= {} end |
#inherited(subclass) ⇒ Object
Register this classes callbacks onto the subclass, when inherited.
37 38 39 40 41 42 43 |
# File 'lib/cura/attributes/has_events.rb', line 37 def inherited(subclass) callbacks.each do |event_name, blocks| blocks.each do |block| subclass.on_event(event_name, &block) end end end |
#on_event(event_name = :default) { ... } ⇒ Proc
Store a callback on this class. Stored callbacks will be registered on the event handler on initialization.
30 31 32 33 34 |
# File 'lib/cura/attributes/has_events.rb', line 30 def on_event(event_name=:default, &block) (callbacks[event_name.to_sym] ||= []) << block block end |