class Remap::Rule::Map::Enum

Public Class Methods

call(&block) click to toggle source

Builds an enumeration using the block as context

@example

enum = Remap::Rule::Map::Enum.call do
  from "B", to: "C"
  value "A"
  otherwise "D"
end

enum.get("A") # => "A"
enum.get("B") # => "C"
enum.get("C") # => "C"
enum.get("MISSING") # => "D"

@return [Any]

# File lib/remap/rule/map/enum.rb, line 28
def self.call(&block)
  unless block
    raise ArgumentError, "no block given"
  end

  new.tap { _1.execute(&block) }
end

Public Instance Methods

call(key, &error)
Alias for: get
from(*keys, to:) click to toggle source

@return [void]

# File lib/remap/rule/map/enum.rb, line 60
def from(*keys, to:)
  keys.each do |key|
    table[key] = to
    table[to] = to
  end
end
get(key, &error) click to toggle source

Translates key into a value using predefined table

@param key [#hash]

@yield [String]

If the key is not found & no default value is set

@return [Any]

# File lib/remap/rule/map/enum.rb, line 44
def get(key, &error)
  unless error
    return get(key) { raise Error, _1 }
  end

  table.fetch(key) do
    unless default == Undefined
      return default
    end

    error["Enum key [#{key}] not found among [#{table.keys.inspect}]"]
  end
end
Also aliased as: call
otherwise(value) click to toggle source

@return [void]

# File lib/remap/rule/map/enum.rb, line 75
def otherwise(value)
  @default = value
end
value(*ids) click to toggle source

@return [void]

# File lib/remap/rule/map/enum.rb, line 68
def value(*ids)
  ids.each do |id|
    from(id, to: id)
  end
end