module Signals::Subscriber::ClassMethods

Public Instance Methods

add_event(e, actions) click to toggle source

@param [Symbol] e the event @param [Array|Symbol] actions the actions to be taken @return [void]

# File lib/signals/subscriber.rb, line 44
def add_event(e, actions)
  event(e).concat(actions.is_a?(Array) ? actions : [actions])
  nil
end
events() click to toggle source

@return [Hash]

# File lib/signals/subscriber.rb, line 50
def events
  @events ||= Hash.new
end
listen_for(params={}) click to toggle source

Register the class to listen for specific events and react accordingly. Any combination can be used. Listen for only accepts a hash as the parameter.

Example

class Something
  include Signals::Subscriber

  listen_for :one => :action
  listen_for :one => [:action, :another]
  listen_for :one => [:and_another]
  listen_for [:one, :two] => :action
  listen_for [:one, :two] => [:action]
  listen_for [:one] => [:and_another]
end

@param [Hash] params @return [void]

# File lib/signals/subscriber.rb, line 30
def listen_for(params={})
  params.each do |k, v|
    if k.is_a?(Array)
      k.each { |e| add_event(e, v) }
    else
      add_event(k, v)
    end
  end
  nil
end

Private Instance Methods

event(key) click to toggle source

@param [Symbol] key @return [Array]

# File lib/signals/subscriber.rb, line 58
def event(key)
  events[key] ||= Array.new
end