Class: Remap::Path::Input

Inherits:
Unit
  • Object
show all
Defined in:
lib/remap/path/input.rb

Overview

Returns the value at a given path

Examples:

Select “A” from { a: { b: { c: [“A”] } } }

state = Remap::State.call({ a: { b: { c: ["A"] } } })
first = Remap::Selector::Index.new(index: 0)
path = Remap::Path::Input.new([:a, :b, :c, first])

path.call(state) do |state|
  state.fetch(:value)
end

Instance Method Summary collapse

Instance Method Details

#call(state) {|| ... } ⇒ State

Parameters:

Yield Parameters:

Yield Returns:

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/remap/path/input.rb', line 27

def call(state, &iterator)
  unless iterator
    raise ArgumentError, "Input path requires an iterator block"
  end

  selectors.reverse.reduce(iterator) do |inner_iterator, selector|
    -> inner_state { selector.call(inner_state, &inner_iterator) }
  end.call(state)
end

#selectorsArray<Selector>

Returns:



19
# File 'lib/remap/path/input.rb', line 19

attribute :selectors, [Selector]