class Wisper::ObjectRegistration

Attributes

allowed_classes[R]
broadcaster[R]
prefix[R]
with[R]

Public Class Methods

new(listener, options) click to toggle source
Calls superclass method
# File lib/wisper/registration/object.rb, line 7
def initialize(listener, options)
  super(listener, options)
  @with   = options[:with]
  @prefix = ValueObjects::Prefix.new options[:prefix]
  @allowed_classes = Array(options[:scope]).map(&:to_s).to_set
  @broadcaster = map_broadcaster
end

Public Instance Methods

broadcast(event, publisher, *args) click to toggle source
# File lib/wisper/registration/object.rb, line 15
def broadcast(event, publisher, *args)
  method_to_call = map_event_to_method(event)
  if should_broadcast?(event) && listener.respond_to?(method_to_call) && publisher_in_scope?(publisher)
    broadcaster.broadcast(listener, publisher, method_to_call, args)
  end
end

Private Instance Methods

broadcaster_key() click to toggle source

@return [Symbol] key to fetch broadcaster by

@example (setup => key)

publisher.subscribe(Subscriber, async: Wisper::SidekiqBroadcaster.new)       # => :async
publisher.subscribe(Subscriber, async: true)                                 # => :async
publisher.subscribe(Subscriber, sidekiq: { queue: 'custom' })                # => :sidekiq
publisher.subscribe(Subscriber)                                              # => :default
publisher.subscribe(Subscriber, broadcaster: Wisper::SidekiqBroadcaster.new) # => :broadcaster
publisher.subscribe(Subscriber, broadcaster: :custom)                        # => :custom
# File lib/wisper/registration/object.rb, line 51
def broadcaster_key
  return :async if options.has_key?(:async) && options[:async]
  return :default unless options.has_key?(:broadcaster)
  options[:broadcaster].is_a?(Symbol) ? options[:broadcaster] : :broadcaster
end
broadcaster_with_options(key, value) click to toggle source

@param [Symbol] key - param to fetch broadcaster by @param [Boolean, Nil, Hash, Object] value - broadcaster value. Allowed values are the following:

nil                 # => default broadcaster
false               # => default broadcaster
true                # => async broadcaster
Broadcaster.new     # => returns the provided broadcaster instance
{ queue: 'custom' } # => is used when broadcaster is configured as a callable object. In this case
                    #    the given options are passed to broadcaster initializer

@return [Object] broadcaster instance for the given key / value pair

# File lib/wisper/registration/object.rb, line 68
def broadcaster_with_options(key, value)
  result = configuration.broadcasters.fetch(key)
  result.respond_to?(:call) ? result.call(value) : result
end
configuration() click to toggle source
# File lib/wisper/registration/object.rb, line 73
def configuration
  Wisper.configuration
end
map_broadcaster() click to toggle source

@return [Object] broadcaster instance

# File lib/wisper/registration/object.rb, line 33
def map_broadcaster
  key = broadcaster_key
  value = options[key]
  return value if value.respond_to?(:broadcast)

  broadcaster_with_options(key, value)
end
map_event_to_method(event) click to toggle source
# File lib/wisper/registration/object.rb, line 28
def map_event_to_method(event)
  prefix + (with || event).to_s
end
publisher_in_scope?(publisher) click to toggle source
# File lib/wisper/registration/object.rb, line 24
def publisher_in_scope?(publisher)
  allowed_classes.empty? || publisher.class.ancestors.any? { |ancestor| allowed_classes.include?(ancestor.to_s) }
end