class Remap::Rule::Map

Maps an input path to an output path

@example Map { name: “Ford” } to { person: { name: “Ford” } }

class Mapper < Remap::Base
  define do
    map :name, to: [:person, :name]
  end
end

Mapper.call({ name: "Ford" }) # => { person: { name: "Ford" } }

Public Instance Methods

add(&block) click to toggle source

@return [self]

# File lib/remap/rule/map.rb, line 173
def add(&block)
  tap { fn << block }
end
adjust(&block) click to toggle source

A post-processor method

@example Up-case mapped value

state = Remap::State.call("Hello World")
map = Remap::Rule::Map.call(backtrace: caller)
upcase = map.adjust(&:upcase)
error = -> failure { raise failure.exception }
upcase.call(state, &error).fetch(:value) # => "HELLO WORLD"

@return [Map]

# File lib/remap/rule/map.rb, line 59
def adjust(&block)
  add do |state|
    state.execute(&block)
  end
end
Also aliased as: then
call(state) click to toggle source

Represents a required or optional mapping rule

@param state [State]

@return [State]

@abstract

# File lib/remap/rule/map.rb, line 45
def call(state)
  raise NotImplementedError, "#{self.class}#call not implemented"
end
enum(&block) click to toggle source

An enumeration processor

@example A mapped enum

enum = Remap::Rule::Map.call(backtrace: caller).enum do
  value "A", "B"
  otherwise "C"
end

error = -> failure { raise failure.exception }

a = Remap::State.call("A")
enum.call(a, &error).fetch(:value) # => "A"

b = Remap::State.call("B")
enum.call(b, &error).fetch(:value) # => "B"

c = Remap::State.call("C")
enum.call(c, &error).fetch(:value) # => "C"

d = Remap::State.call("D")
enum.call(d, &error).fetch(:value) # => "C"

@return [Map]

# File lib/remap/rule/map.rb, line 107
def enum(&block)
  add do |outer_state|
    outer_state.fmap do |id, state|
      Enum.call(&block).get(id) do
        state.ignore!("Enum value %p (%s) not defined", id, id.class)
      end
    end
  end
end
if(&block) click to toggle source

Keeps map, only if block is true

@example Keep if value contains “A”

map = Remap::Rule::Map::Optional.call(backtrace: caller).if do |value|
  value.include?("A")
end

error = -> failure { raise failure.exception }

a = Remap::State.call("A")
map.call(a, &error).fetch(:value) # => "A"

b = Remap::State.call("BA")
map.call(b, &error).fetch(:value) # => "BA"

c = Remap::State.call("C")
map.call(c, &error).key?(:value) # => false

@return [Map]

# File lib/remap/rule/map.rb, line 136
def if(&block)
  add do |outer_state|
    outer_state.execute(&block).fmap do |bool, state|
      bool ? outer_state.value : state.ignore!("#if returned false")
    end
  end
end
if_not(&block) click to toggle source

@example Keep unless value contains “A”

map = Remap::Rule::Map::Optional.new(backtrace: caller).if_not do |value|
  value.include?("A")
end

error = -> failure { raise failure.exception(caller) }

a = Remap::State.call("A")
map.call(a, &error).key?(:value) # => false

b = Remap::State.call("BA")
map.call(b, &error).key?(:value) # => false

c = Remap::State.call("C")
map.call(c, &error).fetch(:value) # => "C"

@return [Map]

# File lib/remap/rule/map.rb, line 164
def if_not(&block)
  add do |outer_state|
    outer_state.execute(&block).fmap do |bool, state|
      bool ? state.ignore!("#if_not returned false") : outer_state.value
    end
  end
end
pending(reason = "Pending mapping") click to toggle source

A pending rule

@param reason [String]

@example Pending mapping

state = Remap::State.call(:value)
map = Remap::Rule::Map::Optional.call(backtrace: caller)
pending = map.pending("this is pending")
error = -> failure { raise failure.exception }
pending.call(state, &error).key?(:value) # => false

@return [Map]

# File lib/remap/rule/map.rb, line 78
def pending(reason = "Pending mapping")
  add do |state|
    state.ignore!(reason)
  end
end
then(&block)
Alias for: adjust

Private Instance Methods

callback(state) click to toggle source

@return [Proc]

# File lib/remap/rule/map.rb, line 185
def callback(state)
  fn.reduce(state) { |s1, f1| f1[s1] }
end
fn() click to toggle source

@return [Array<Proc>]

# File lib/remap/rule/map.rb, line 180
def fn
  @fn ||= []
end