Sha256: 1b37e6ba9ee365565da56a7dec72fe104e1b8dafbb1c370d298cb9eccb11aaa8

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

require 'rom/commands/composite'
require 'rom/commands/graph'

module ROM
  module Commands
    # Lazy command wraps another command and evaluates its input when called
    #
    # @api private
    class Lazy
      # @attr_reader [Command] command The wrapped command
      attr_reader :command

      # @attr_reader [Proc] evaluator The proc that will evaluate the input
      attr_reader :evaluator

      # @api private
      def initialize(command, evaluator)
        @command = command
        @evaluator = evaluator
      end

      # Evaluate command's input using the input proc and pass to command
      #
      # @return [Array,Hash]
      #
      # @api public
      def call(*args)
        first = args.first
        last = args.last
        size = args.size

        if size > 1 && last.is_a?(Array)
          last.map.with_index do |item, index|
            command.call(evaluator[first, index], item)
          end.reduce(:concat)
        else
          command.call(evaluator[first], *args[1..size-1])
        end
      end

      # Compose a lazy command with another one
      #
      # @see Commands::Abstract#>>
      #
      # @return [Composite]
      #
      # @api public
      def >>(other)
        Composite.new(self, other)
      end

      # @see Abstract#combine
      #
      # @api public
      def combine(*others)
        Graph.new(self, others)
      end

      # @api private
      def lazy?
        true
      end

      # @api private
      def respond_to_missing?(name, include_private = false)
        super || command.respond_to?(name)
      end

      private

      # @api private
      def method_missing(name, *args, &block)
        if command.respond_to?(name)
          response = command.public_send(name, *args, &block)

          if response.instance_of?(command.class)
            self.class.new(response, evaluator)
          else
            response
          end
        else
          super
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-0.9.0.beta1 lib/rom/commands/lazy.rb
rom-0.8.1 lib/rom/commands/lazy.rb
rom-0.8.0 lib/rom/commands/lazy.rb