Sha256: 3a70cc8ff9fd9c805a8a4d863277039847472652950fb70784b8c4f12b7e7ab5

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 KB

Contents

require 'rom/pipeline'
require 'rom/support/options'
require 'rom/commands/graph/class_interface'

module ROM
  module Commands
    # Command graph
    #
    # @api private
    class Graph
      include Dry::Equalizer(:root, :nodes)

      extend ClassInterface

      include Options
      include Pipeline
      include Pipeline::Proxy

      # @attr_reader [Command] root The root command
      attr_reader :root

      # @attr_reader [Array<Command>] nodes The child commands
      attr_reader :nodes

      alias_method :left, :root
      alias_method :right, :nodes

      option :mappers, reader: true, default: proc { MapperRegistry.new }

      # @api private
      def initialize(root, nodes, options = EMPTY_HASH)
        super
        @root = root
        @nodes = nodes
      end

      # Calls root and all nodes with the result from root
      #
      # Graph results are mappable through `combine` operation in mapper DSL
      #
      # @example
      #   create_user = rom.command(:users).create
      #   create_task = rom.command(:tasks).create
      #
      #   command = create_user
      #     .with(name: 'Jane')
      #     .combine(create_task.with(title: 'Task'))
      #
      #   command.call
      #
      # @return [Array] nested array with command results
      #
      # @api public
      def call(*args)
        left = root.call(*args)

        right = nodes.map { |node|
          response =
            if node.lazy?
              node.call(args.first, left)
            else
              node.call(left)
            end

          if node.one? && !node.graph?
            [response]
          else
            response
          end
        }

        if one?
          [[left], right]
        else
          [left, right]
        end
      end

      # Return a new graph with updated options
      #
      # @api private
      def with(new_options)
        self.class.new(root, nodes, options.merge(new_options))
      end

      # @api private
      def graph?
        true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-1.0.0 lib/rom/commands/graph.rb
rom-1.0.0.rc1 lib/rom/commands/graph.rb
rom-1.0.0.beta2 lib/rom/commands/graph.rb
rom-1.0.0.beta1 lib/rom/commands/graph.rb