Class: Ekylibre::Hook
- Inherits:
-
Object
- Object
- Ekylibre::Hook
- Defined in:
- lib/ekylibre/hook.rb
Class Method Summary collapse
-
.publish(message, data = {}) ⇒ Object
Publish a given event/message.
-
.subscribe(message, proc = nil, &block) ⇒ Object
Subscribe to a given event/message.
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(, data = {}) Rails.logger.info("Publish: #{.to_s.yellow}") if @subscriptions[] && @subscriptions[].any? @subscriptions[].each_with_index do |block, index| Rails.logger.info "Push to #{}##{index}".yellow if block.arity >= 1 block.call(data) else block.call end Rails.logger.info "Push to #{}##{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(, proc = nil, &block) @subscriptions[] ||= [] if proc.respond_to?(:call) @subscriptions[] << proc elsif block_given? @subscriptions[] << block else raise 'Need block or proc' end end |