Class: Remap::Rule::Map

Inherits:
Remap::Rule show all
Defined in:
lib/remap/rule/map.rb

Instance Method Summary collapse

Instance Method Details

#adjust {|value| ... } ⇒ void Also known as: then

This method returns an undefined value.

Post-processor for #call

Examples:

Add 5 to mapped value

class Mapper < Remap
  define do
    map.adjust do |value|
      value + 5
    end
  end
end

Mapper.call(10) # => 15

Yield Parameters:

  • value (Any)

    Mapped value

Yield Returns:

  • (Monad::Result, Any)


61
62
63
64
65
# File 'lib/remap/rule/map.rb', line 61

def adjust(&block)
  add do |state|
    state.execute(&block)
  end
end

#call(state) ⇒ Monad::Result

Examples:

Map :a, to :b and add 5

map = Map.new({
  path: { map: :a, to: :b },
  rule: Void.new,
})

map.adjust do |value|
  value + 5
end

map.call(a: 10) # => Success({ b: 15 })

Parameters:

  • input (Any)

    Value to be mapped

  • state (State)

    Current state

Returns:

  • (Monad::Result)


34
35
36
37
38
39
40
41
42
# File 'lib/remap/rule/map.rb', line 34

def call(state)
  path.call(state) do |inner_state|
    rule.call(inner_state).then do |init|
      fn.reduce(init) do |inner, fn|
        fn[inner]
      end
    end
  end
end

#enum(&block) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/remap/rule/map.rb', line 74

def enum(&block)
  add do |state|
    state.fmap do |id, &error|
      Enum.call(&block).get(id, &error)
    end
  end
end

#if(&block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/remap/rule/map.rb', line 82

def if(&block)
  add do |state|
    state.execute(&block).fmap do |bool, &error|
      bool ? state.value : error["#if returned false"]
    end
  end
end

#if_not(&block) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/remap/rule/map.rb', line 90

def if_not(&block)
  add do |state|
    state.execute(&block).fmap do |bool, &error|
      bool ? error["#if_not returned true"] : state.value
    end
  end
end

#pending(reason = "Pending mapping") ⇒ Object



68
69
70
71
72
# File 'lib/remap/rule/map.rb', line 68

def pending(reason = "Pending mapping")
  add do |state|
    state.problem(reason)
  end
end