Class: Ekylibre::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/ekylibre/hook.rb

Class Method Summary collapse

Class Method Details

.publish(message, data = {}) ⇒ Object

Publish a given event/message


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ekylibre/hook.rb', line 7

def publish(message, data = {})
  Rails.logger.info("Publish: #{message.to_s.yellow}")
  if @subscriptions[message] && @subscriptions[message].any?
    @subscriptions[message].each_with_index do |block, index|
      Rails.logger.info "Push to #{message}##{index}".yellow
      if block.arity >= 1
        block.call(data)
      else
        block.call
      end
      Rails.logger.info "Push to #{message}##{index}: terminated".yellow
    end
  end
end

.subscribe(message, proc = nil, &block) ⇒ Object

Subscribe to a given event/message


23
24
25
26
27
28
29
30
31
32
# File 'lib/ekylibre/hook.rb', line 23

def subscribe(message, proc = nil, &block)
  @subscriptions[message] ||= []
  if proc.respond_to?(:call)
    @subscriptions[message] << proc
  elsif block_given?
    @subscriptions[message] << block
  else
    raise 'Need block or proc'
  end
end