Class: Bismas::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/bismas/mapping.rb

Constant Summary collapse

DEFAULT_MAPPING =
true
LITERALS =
{ '~' => nil, 'false' => false, 'true' => true }
NULL =
Object.new.tap { |null| def null.apply(hash); hash; end }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapping) ⇒ Mapping

Returns a new instance of Mapping.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bismas/mapping.rb', line 41

def initialize(mapping)
  @mapping = default_hash.update(default: Array(DEFAULT_MAPPING))

  mapping.each { |key, value|
    value = Array(value.is_a?(String) ? range(value) : value)

    !key.is_a?(String) ? @mapping[key] = value :
      range(key) { |m| @mapping[m].concat(value) }
  }

  @mapping.each_value(&:uniq!)
end

Class Method Details

.[](mapping) ⇒ Object



37
38
39
# File 'lib/bismas/mapping.rb', line 37

def self.[](mapping)
  mapping ? new(mapping) : NULL
end

Instance Method Details

#[](key) ⇒ Object



62
63
64
# File 'lib/bismas/mapping.rb', line 62

def [](key)
  map(key).to_a
end

#apply(hash, new_hash = default_hash) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/bismas/mapping.rb', line 54

def apply(hash, new_hash = default_hash)
  hash.each { |key, value| map(key) { |new_key|
    new_hash[new_key].concat(Array(value))
  } }

  new_hash
end