module Cura::Attributes::HasEvents

Adds an `event_handler` attribute as well as `callbacks` and `on_event` class methods. When subclassed, the callbacks are inherited. TODO: Rename to HasEventHandler

Attributes

event_handler[R]

Get the event handler for this object.

@return [Event::Handler]

Public Class Methods

included(base) click to toggle source
# File lib/cura/attributes/has_events.rb, line 44
def included(base)
  base.send(:extend, ClassMethods)
end
new(attributes={}) click to toggle source
Calls superclass method
# File lib/cura/attributes/has_events.rb, line 49
def initialize(attributes={})
  @event_handler = Event::Handler.new(self)
  register_class_callbacks

  super
end

Public Instance Methods

on_event(event_name=:default, *arguments, &block) click to toggle source

Register a callback for an event to this instance.

@param [nil, to_sym] event_name The event name. @yield The callback block. @return [Proc] The callback block.

# File lib/cura/attributes/has_events.rb, line 66
def on_event(event_name=:default, *arguments, &block)
  event_handler.register(event_name, *arguments, &block)
end

Protected Instance Methods

register_class_callbacks() click to toggle source
# File lib/cura/attributes/has_events.rb, line 72
def register_class_callbacks
  self.class.callbacks.each do |event_name, blocks|
    blocks.each do |block|
      @event_handler.register(event_name, &block)
    end
  end
end