Sha256: f06cc52f8113539025e821cbbc5493da1cec89128480f1671626dfdf9d0b9db9

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 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 [nil, 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 nil
        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

1 entries across 1 versions & 1 rubygems

Version Path
substation-0.0.10.beta2 lib/substation/observer.rb