class Bismas::Mapping
Constants
- DEFAULT_MAPPING
- LITERALS
- NULL
Public Class Methods
[](mapping)
click to toggle source
# File lib/bismas/mapping.rb, line 37 def self.[](mapping) mapping ? new(mapping) : NULL end
new(mapping)
click to toggle source
# 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
Public Instance Methods
[](key)
click to toggle source
# File lib/bismas/mapping.rb, line 62 def [](key) map(key).to_a end
apply(hash, new_hash = default_hash)
click to toggle source
# 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
Private Instance Methods
default_hash()
click to toggle source
# File lib/bismas/mapping.rb, line 68 def default_hash Hash.new { |h, k| h[k] = [] } end
fetch(key)
click to toggle source
# File lib/bismas/mapping.rb, line 83 def fetch(key) @mapping.key?(key) ? @mapping[key] : @mapping[:default] end
map(key) { |new_key == true ? key : new_key| ... }
click to toggle source
# File lib/bismas/mapping.rb, line 87 def map(key) return enum_for(__method__, key) unless block_given? fetch(key).each { |new_key| yield new_key == true ? key : new_key if new_key } end
range(list, &block)
click to toggle source
# File lib/bismas/mapping.rb, line 72 def range(list, &block) return enum_for(__method__, list) unless block list.split(/\s*,\s*/).each { |part| LITERALS.key?(part) ? block[LITERALS[part]] : begin from, to = part.split('-') from.upto(to || from, &block) end } end