Sha256: 66daccb15c2cd0fcd5ed2e6b5b7c98e04ba7dd0bcb22586f97444c6d24fab0ce

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

module Substation
  module Processor

    # A processor to evaluate a chain's request input data
    class Evaluator

      include Incoming

      # Initialize a new instance
      #
      # @param [Environment] env
      #   the substation environment used to build chains
      #
      # @param [#call] handler
      #   the handler to perform evaluation
      #
      # @param [Proc] block
      #   a block to construct a failure chain
      #
      # @return [undefined]
      #
      # @api private
      def initialize(env, handler, &block)
        @env, @handler = env, handler
        @failure_chain = block ? @env.chain(&block) : Undefined
      end

      # Evaluate a chain's request input data
      #
      # @param [Request] request
      #   the request to process
      #
      # @return [Response]
      #
      # @api private
      def call(request)
        result = handler.call(request.input)
        output = result.output
        if result.success?
          request.success(output)
        else
          response = request.error(output)
          if fail_safe?
            failure_chain.call(response)
          else
            response
          end
        end
      end

      protected

      # The handler used to perform evaluation
      #
      # @return [#call]
      #
      # @api private
      attr_reader :handler

      private

      # The chain to invoke if evaluation returned an error
      #
      # @return [Chain]
      #
      # @api private
      attr_reader :failure_chain

      # Test wether this evaluator has a failure chain
      #
      # @return [TrueClass] if a failure chain is registered
      # @return [FalseClass] otherwise
      #
      # @api private
      def fail_safe?
        !@failure_chain.equal?(Undefined)
      end
    end # class Evaluator
  end # module Processor
end # module Substation

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
substation-0.0.10.beta2 lib/substation/processor/evaluator.rb