Sha256: 9865bb22026d437ffc92d25f2edea7004d1359df4a7b88879280e3d591ba6da0

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 KB

Contents

module Substation

  # Abstract observer base class
  #
  # @abstract
  class Observer

    include AbstractType
    include Adamantium::Flat

    # Notify the observer
    #
    # @param [Response] response
    #   the response returned when calling the observed action
    #
    # @return [self]
    #
    # @api private
    abstract_method :call

    # Coerce +input+ to an instance of {Observer}
    #
    # @param [NilClass, String, Array<String>] input
    #   0..n observer class names
    #
    # @return [Observer::NULL, Object, Observer::Chain]
    #   a null observer, an observer object, or a chain of observers
    #
    # @api private
    def self.coerce(input)
      case input
      when NilClass
        NULL
      when Array
        Chain.new(input.map { |item| coerce(item) })
      else
        Utils.coerce_callable(input)
      end
    end

    # Null observer
    NULL = Class.new(self) { def call(_response); self; end; }.new.freeze

    # Chain of observers
    class Chain < self

      include Concord.new(:observers)

      # Notify the observer
      #
      # @param [Response] response
      #   the response returned when calling the observed action
      #
      # @return [self]
      #
      # @api private
      def call(response)
        observers.each do |observer|
          observer.call(response)
        end
        self
      end

    end # Chain
  end # Observer
end # Substation

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
substation-0.0.8 lib/substation/observer.rb
substation-0.0.7 lib/substation/observer.rb
substation-0.0.6 lib/substation/observer.rb
substation-0.0.5 lib/substation/observer.rb
substation-0.0.4 lib/substation/observer.rb
substation-0.0.3 lib/substation/observer.rb
substation-0.0.2 lib/substation/observer.rb
substation-0.0.1 lib/substation/observer.rb