Class: Remap::Rule::Map

Inherits:
Abstract
  • Object
show all
Defined in:
lib/remap/rule/map.rb,
lib/remap/rule/map/optional.rb,
lib/remap/rule/map/required.rb

Overview

Maps an input path to an output path

Examples:

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" } }

Defined Under Namespace

Classes: Optional, Path, Required

Instance Method Summary collapse

Instance Method Details

#adjust(&block) ⇒ Map Also known as: then

A post-processor method

Examples:

Upcase mapped value

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


71
72
73
74
75
# File 'lib/remap/rule/map.rb', line 71

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

#backtraceArray<String>



34
# File 'lib/remap/rule/map.rb', line 34

attribute? :backtrace, Types::Backtrace, default: EMPTY_ARRAY

#call(state, &error) ⇒ State

This method is abstract.

Represents a required or optional mapping rule



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/remap/rule/map.rb', line 45

def call(state, &error)
  unless error
    raise ArgumentError, "Map#call(state, &error) requires error handler block"
  end

  notice = catch :fatal do
    return path.input.call(state) do |inner_state|
      rule.call(inner_state) do |failure|
        return error[failure]
      end.then(&callback)
    end.then(&path.output)
  end

  raise notice.traced(backtrace).exception
end

#enum(&block) ⇒ Map

An enumeration processor

Examples:

A mapped enum

enum = Remap::Rule::Map.call({}).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"


119
120
121
122
123
124
125
126
127
# File 'lib/remap/rule/map.rb', line 119

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) ⇒ Map

Keeps map, only if block is true

Examples:

Keep if value contains “A”

map = Remap::Rule::Map.call({}).if do
  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


148
149
150
151
152
153
154
# File 'lib/remap/rule/map.rb', line 148

def if(&block)
  add do |outer_state|
    outer_state.execute(&block).fmap do |bool, state|
      bool ? outer_state.value : state.notice!("#if returned false")
    end
  end
end

#if_not(&block) ⇒ Map

Examples:

Keep unless value contains “A”

map = Remap::Rule::Map.call({}).if_not do
  value.include?("A")
end

error = -> failure { raise failure.exception }

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"


176
177
178
179
180
181
182
# File 'lib/remap/rule/map.rb', line 176

def if_not(&block)
  add do |outer_state|
    outer_state.execute(&block).fmap do |bool, state|
      bool ? state.notice!("#if_not returned false") : outer_state.value
    end
  end
end

#pathHash



28
# File 'lib/remap/rule/map.rb', line 28

attribute? :path, Path.default { Path.call(EMPTY_HASH) }

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

A pending rule

Examples:

Pending mapping

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


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

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

#ruleRule



31
# File 'lib/remap/rule/map.rb', line 31

attribute :rule, Rule.default { Void.call(EMPTY_HASH) }