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

Instance Method Details

#callbacksHash<Symbol,Array<Proc>>

The callbacks stored on this class.

Returns:

  • (Hash<Symbol,Array<Proc>>)


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.

Parameters:

  • event_name (nil, #to_sym) (defaults to: :default)

    The event name.

Yields:

  • The callback block.

Returns:

  • (Proc)

    The callback block.



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