Sha256: e9ed8cb2ba30f5f40ddbc835435d784d4c43930cd8229f6f2943074fd6931bfc

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'rom/initializer'
require 'rom/pipeline'
require 'rom/commands/graph/class_interface'

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

      extend ClassInterface

      include Pipeline
      include Pipeline::Proxy

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

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

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

      # @attr_reader [Symbol] root's relation name
      option :name, default: -> { root.name }

      option :mappers, default: -> { MapperRegistry.new }

      # Calls root and all nodes with the result from root
      #
      # Graph results are mappable through `combine` operation in mapper DSL
      #
      # @example
      #   create_user = rom.commands[:users].create
      #   create_task = rom.commands[:tasks].create
      #
      #   command = create_user
      #     .curry(name: 'Jane')
      #     .combine(create_task.curry(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

      # @api private
      def graph?
        true
      end

      private

      # @api public
      def composite_class
        Command::Composite
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rom-core-5.3.2 lib/rom/commands/graph.rb
rom-core-5.3.1 lib/rom/commands/graph.rb
rom-core-5.3.0 lib/rom/commands/graph.rb
rom-core-5.2.6 lib/rom/commands/graph.rb
rom-core-5.2.5 lib/rom/commands/graph.rb
rom-core-5.2.4 lib/rom/commands/graph.rb
rom-core-5.2.3 lib/rom/commands/graph.rb
rom-core-5.2.2 lib/rom/commands/graph.rb
rom-core-5.2.1 lib/rom/commands/graph.rb
rom-core-5.1.2 lib/rom/commands/graph.rb
rom-core-5.1.1 lib/rom/commands/graph.rb
rom-core-5.1.0 lib/rom/commands/graph.rb
rom-core-5.0.2 lib/rom/commands/graph.rb
rom-core-5.0.1 lib/rom/commands/graph.rb
rom-core-5.0.0 lib/rom/commands/graph.rb