Class: Remap::Rule::Map

Inherits:
Abstract
  • Object
show all
Defined in:
lib/remap/rule/map.rb,
lib/remap/rule/map/enum.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: Enum, Optional, Path, Required

Instance Method Summary collapse

Instance Method Details

#add(&block) ⇒ self

Returns:

  • (self)


173
174
175
# File 'lib/remap/rule/map.rb', line 173

def add(&block)
  tap { fn << block }
end

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

A post-processor method

Examples:

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"

Returns:



59
60
61
62
63
# File 'lib/remap/rule/map.rb', line 59

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

#backtraceArray<String>

Returns:

  • (Array<String>)


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

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

#call(state) ⇒ State

This method is abstract.

Represents a required or optional mapping rule

Parameters:

Returns:

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/remap/rule/map.rb', line 45

def call(state)
  raise NotImplementedError, "#{self.class}#call not implemented"
end

#enum(&block) ⇒ Map

An enumeration processor

Examples:

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"

Returns:



107
108
109
110
111
112
113
114
115
# 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) ⇒ Map

Keeps map, only if block is true

Examples:

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

Returns:



136
137
138
139
140
141
142
# 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) ⇒ Map

Examples:

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"

Returns:



164
165
166
167
168
169
170
# 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

#pathHash

Returns:

  • (Hash)


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::Optional.call(backtrace: caller)
pending = map.pending("this is pending")
error = -> failure { raise failure.exception }
pending.call(state, &error).key?(:value) # => false

Parameters:

  • reason (String) (defaults to: "Pending mapping")

Returns:



78
79
80
81
82
# File 'lib/remap/rule/map.rb', line 78

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

#ruleRule

Returns:



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

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