Module: Signals::Subscriber::ClassMethods

Defined in:
lib/signals/subscriber.rb

Instance Method Summary collapse

Instance Method Details

#add_event(e, actions) ⇒ void

This method returns an undefined value.

Parameters:

  • e (Symbol)

    the event

  • actions (Array|Symbol)

    the actions to be taken



44
45
46
47
# File 'lib/signals/subscriber.rb', line 44

def add_event(e, actions)
  event(e).concat(actions.is_a?(Array) ? actions : [actions])
  nil
end

#eventsHash

Returns:

  • (Hash)


50
51
52
# File 'lib/signals/subscriber.rb', line 50

def events
  @events ||= Hash.new
end

#listen_for(params = {}) ⇒ void

This method returns an undefined value.

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

Parameters:

  • params (Hash) (defaults to: {})


30
31
32
33
34
35
36
37
38
39
# 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