Sha256: d46f89e2592a7a83f51f3e305b2566290604bfe9a398baac2744b8c4f6e01b2c

Contents?: true

Size: 1.62 KB

Versions: 28

Compression:

Stored size: 1.62 KB

Contents

module ROM
  module Commands
    class Graph
      class InputEvaluator
        include Dry::Equalizer(:tuple_path, :excluded_keys)

        attr_reader :tuple_path

        attr_reader :excluded_keys

        attr_reader :exclude_proc

        def self.build(tuple_path, nodes)
          new(tuple_path, extract_excluded_keys(nodes))
        end

        def self.extract_excluded_keys(nodes)
          return unless nodes

          nodes
            .map { |item| item.is_a?(Array) && item.size > 1 ? item.first : item }
            .compact
            .map { |item| item.is_a?(Hash) ? item.keys.first : item }
            .reject { |item| item.is_a?(Array) }
        end

        def self.exclude_proc(excluded_keys)
          -> input { input.reject { |k, _| excluded_keys.include?(k) } }
        end

        def initialize(tuple_path, excluded_keys)
          @tuple_path = tuple_path
          @excluded_keys = excluded_keys
          @exclude_proc = self.class.exclude_proc(excluded_keys)
        end

        def call(*args)
          input, index = args

          value =
            begin
              if index
                tuple_path[0..tuple_path.size-2]
                  .reduce(input) { |a, e| a.fetch(e) }
                  .at(index)[tuple_path.last]
              else
                tuple_path.reduce(input) { |a, e| a.fetch(e) }
              end
            rescue KeyError => e
              raise KeyMissing, e.message
            end

          if excluded_keys
            value.is_a?(Array) ? value.map(&exclude_proc) : exclude_proc[value]
          else
            value
          end
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
rom-3.3.3 lib/rom/commands/graph/input_evaluator.rb
rom-3.3.2 lib/rom/commands/graph/input_evaluator.rb
rom-core-4.0.0.beta3 lib/rom/commands/graph/input_evaluator.rb
rom-3.3.1 lib/rom/commands/graph/input_evaluator.rb
rom-core-4.0.0.beta2 lib/rom/commands/graph/input_evaluator.rb
rom-3.3.0 lib/rom/commands/graph/input_evaluator.rb
rom-core-4.0.0.beta1 lib/rom/commands/graph/input_evaluator.rb
rom-3.2.3 lib/rom/commands/graph/input_evaluator.rb
rom-3.2.2 lib/rom/commands/graph/input_evaluator.rb
rom-3.2.1 lib/rom/commands/graph/input_evaluator.rb
rom-3.2.0 lib/rom/commands/graph/input_evaluator.rb
rom-3.1.0 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.3 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.2 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.1 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.0 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.0.rc2 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.0.rc1 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.0.beta3 lib/rom/commands/graph/input_evaluator.rb
rom-3.0.0.beta2 lib/rom/commands/graph/input_evaluator.rb