class WisperNext::Subscriber

Constants

EmptyHash
NoMethodError

Public Class Methods

new(*args) click to toggle source
# File lib/wisper_next/subscriber.rb, line 7
def initialize(*args)
  options = CastToOptions.(args)

  strict      = options.fetch(:strict, true)
  prefix      = options.fetch(:prefix, false)
  broadcaster = resolve_broadcaster(options)

  # maps event to another method
  #
  # @param [Symbol] event name
  #
  # @return [Object] payload
  #
  # @api public
  #
  define_method :on_event do |name, payload|
    method_name = ResolveMethod.(name, prefix)

    if respond_to?(method_name)
      broadcaster.call(self, method_name, payload)
    else
      if strict
        raise NoMethodError.new(name)
      else
        # no-op
      end
    end
  end
end

Private Instance Methods

broadcaster_for(name, opts = EmptyHash) click to toggle source
# File lib/wisper_next/subscriber.rb, line 61
def broadcaster_for(name, opts = EmptyHash)
  self.class.const_get("#{name.to_s.capitalize}Broadcaster").new(opts)
end
resolve_broadcaster(options) click to toggle source
# File lib/wisper_next/subscriber.rb, line 46
def resolve_broadcaster(options)
  value = options[:async] || options[:broadcaster] || SendBroadcaster.new

  case value
  when Hash
    broadcaster_for(:async, value)
  when TrueClass
    broadcaster_for(:async)
  when Symbol, String
    broadcaster_for(value)
  else
    value
  end
end