Class: Remap::Selector::Index

Inherits:
Unit
  • Object
show all
Defined in:
lib/remap/selector/index.rb

Overview

Selects value at given index

Examples:

Select the value at index 1 from a array

state = Remap::State.call([:one, :two, :tree])
result = Remap::Selector::Index.new(1).call(state)
result.fetch(:value) # => :two

Instance Method Summary collapse

Instance Method Details

#call(outer_state) {|| ... } ⇒ State<U>

Selects the #indexth element from state and passes it to block

Parameters:

  • outer_state (State<Array<T>>)

Yield Parameters:

Yield Returns:

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/remap/selector/index.rb', line 27

def call(outer_state, &block)
  return call(outer_state, &:itself) unless block

  outer_state.bind(index: index) do |array, state|
    requirement[array] do
      state.fatal!("Expected array but got %p (%s)", array, array.class)
    end

    element = array.fetch(index) do
      state.ignore!("Index %s in array %p (%s) not found",
                    index,
                    array,
                    array.class)
    end

    state.set(element, index: index).then(&block)
  end
end

#indexInteger

Returns:

  • (Integer)


15
# File 'lib/remap/selector/index.rb', line 15

attribute :index, Integer