module Wisper::Publisher

Private Class Methods

included(base) click to toggle source
# File lib/wisper/publisher.rb, line 85
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

listeners() click to toggle source
# File lib/wisper/publisher.rb, line 3
def listeners
  registrations.map(&:listener).freeze
end
on(*events, &block) click to toggle source

subscribe a block

@example

my_publisher.on(:order_created) { |args| ... }

@return [self]

# File lib/wisper/publisher.rb, line 25
def on(*events, &block)
  raise ArgumentError, 'must give at least one event' if events.empty?
  raise ArgumentError, 'must pass a block' if !block
  local_registrations << BlockRegistration.new(block, on: events)
  self
end
subscribe(listener, options = {}) click to toggle source

subscribe a listener

@example

my_publisher.subscribe(MyListener.new)

@return [self]

# File lib/wisper/publisher.rb, line 13
def subscribe(listener, options = {})
  raise ArgumentError, "#{__method__} does not take a block, did you mean to call #on instead?" if block_given?
  local_registrations << ObjectRegistration.new(listener, options)
  self
end

Private Instance Methods

broadcast(event, *args) click to toggle source

broadcasts an event

@example

def call
  # ...
  broadcast(:finished, self)
end

@return [self]

# File lib/wisper/publisher.rb, line 41
def broadcast(event, *args)
  registrations.each do | registration |
    registration.broadcast(clean_event(event), self, *args)
  end
  self
end
Also aliased as: publish
clean_event(event) click to toggle source
# File lib/wisper/publisher.rb, line 81
def clean_event(event)
  event.to_s.gsub('-', '_')
end
global_registrations() click to toggle source
# File lib/wisper/publisher.rb, line 69
def global_registrations
  GlobalListeners.registrations
end
local_registrations() click to toggle source
# File lib/wisper/publisher.rb, line 65
def local_registrations
  @local_registrations ||= Set.new
end
publish(event, *args)
Alias for: broadcast
registrations() click to toggle source
# File lib/wisper/publisher.rb, line 77
def registrations
  local_registrations + global_registrations + temporary_registrations
end
temporary_registrations() click to toggle source
# File lib/wisper/publisher.rb, line 73
def temporary_registrations
  TemporaryListeners.registrations
end