Sha256: 78be10ea695926c8c47c2b7cf2b1535fda32a92594c0c2b515860f7f7490378d

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# encoding: utf-8

module Informator

  # Class Subscriber wraps the [#object] along with its [#callback] method
  # to receive event notifications.
  #
  # @example The method [#notify] sends event to the [#object] via [#callback]
  #   object     = Struct.new(:event).new
  #   subscriber = Subscriber.new object, :event=
  #   subscriber.frozen? # => true
  #
  #   event  = Informator::Event.new :success
  #   # => #<Event @type=:success @attributes={} @messages=[]>
  #
  #   subscriber.notify event
  #   object.event
  #   # => #<Event @type=:success @attributes={} @messages=[]>
  #
  # @api private
  #
  class Subscriber

    include Equalizer.new(:object, :callback)

    # @!attribute [r] object
    #
    # @return [Object] the object to send events to
    #
    attr_reader :object

    # @!attribute [r] callback
    #
    # @return [Symbol] the name of the object methods to listen to events
    #
    attr_reader :callback

    # @!scope class
    # @!method new(object, callback)
    # Builds the subscriber for given object and callback
    #
    # @param [Object] object
    # @param [#to_sym] callback (:receive)
    #
    # @return [Informator::Subscriber]

    # @private
    def initialize(object, callback = :receive)
      @object   = object
      @callback = callback.to_sym
      IceNine.deep_freeze(self)
    end

    # Sends the event to the subscriber object via its callback
    #
    # @param [Informator::Event] event
    #
    # @return [Informator::Event] published event
    #
    def notify(event)
      object.public_send callback, event

      event
    end

  end # class Subscriber

end # module Informator

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
informator-0.1.0 lib/informator/subscriber.rb