Class: ROM::Commands::Graph::InputEvaluator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/commands/graph/input_evaluator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Evaluator for lazy commands which extracts values for commands from nested hashes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tuple_path, excluded_keys) ⇒ InputEvaluator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new input evaluator



59
60
61
62
63
# File 'lib/rom/commands/graph/input_evaluator.rb', line 59

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

Instance Attribute Details

#exclude_procObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
# File 'lib/rom/commands/graph/input_evaluator.rb', line 22

def exclude_proc
  @exclude_proc
end

#excluded_keysObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
# File 'lib/rom/commands/graph/input_evaluator.rb', line 18

def excluded_keys
  @excluded_keys
end

#tuple_pathObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/rom/commands/graph/input_evaluator.rb', line 14

def tuple_path
  @tuple_path
end

Class Method Details

.build(tuple_path, nodes) ⇒ InputEvaluator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build an input evaluator

Parameters:

  • tuple_path (Array<Symbol>)

    The tuple path

  • nodes (Array)

Returns:



32
33
34
# File 'lib/rom/commands/graph/input_evaluator.rb', line 32

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

.exclude_proc(excluded_keys) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return default exclude_proc



50
51
52
# File 'lib/rom/commands/graph/input_evaluator.rb', line 50

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

.extract_excluded_keys(nodes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
44
45
# File 'lib/rom/commands/graph/input_evaluator.rb', line 37

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

Instance Method Details

#call(input, index = nil) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluate input hash

Parameters:

  • input (Hash)

    The input hash

  • index (Integer) (defaults to: nil)

    Optional index

Returns:

  • (Hash)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rom/commands/graph/input_evaluator.rb', line 71

def call(input, index = nil)
  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