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, &iterator) ⇒ State

Selects the value at the path #segments

Parameters:

Returns:



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

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

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

#segmentsArray<Selector>

Returns:



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

attribute :segments, [Selector]