Sha256: c7b434e1c192753fa817b88e5ef3c2b2b2b9c79b89854bad878325435b16008c

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

module ROM
  module Commands
    class Graph
      class InputEvaluator
        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

          begin
            value =
              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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-0.9.1 lib/rom/commands/graph/input_evaluator.rb
rom-0.9.0 lib/rom/commands/graph/input_evaluator.rb
rom-0.9.0.rc1 lib/rom/commands/graph/input_evaluator.rb