class Bismas::Mapping

Constants

DEFAULT_MAPPING
LITERALS
NULL

Public Class Methods

[](mapping) click to toggle source
   # File lib/bismas/mapping.rb
37 def self.[](mapping)
38   mapping ? new(mapping) : NULL
39 end
new(mapping) click to toggle source
   # File lib/bismas/mapping.rb
41 def initialize(mapping)
42   @mapping = default_hash.update(default: Array(DEFAULT_MAPPING))
43 
44   mapping.each { |key, value|
45     value = Array(value.is_a?(String) ? range(value) : value)
46 
47     !key.is_a?(String) ? @mapping[key] = value :
48       range(key) { |m| @mapping[m].concat(value) }
49   }
50 
51   @mapping.each_value(&:uniq!)
52 end

Public Instance Methods

[](key) click to toggle source
   # File lib/bismas/mapping.rb
62 def [](key)
63   map(key).to_a
64 end
apply(hash, new_hash = default_hash) click to toggle source
   # File lib/bismas/mapping.rb
54 def apply(hash, new_hash = default_hash)
55   hash.each { |key, value| map(key) { |new_key|
56     new_hash[new_key].concat(Array(value))
57   } }
58 
59   new_hash
60 end

Private Instance Methods

default_hash() click to toggle source
   # File lib/bismas/mapping.rb
68 def default_hash
69   Hash.new { |h, k| h[k] = [] }
70 end
fetch(key) click to toggle source
   # File lib/bismas/mapping.rb
83 def fetch(key)
84   @mapping.key?(key) ? @mapping[key] : @mapping[:default]
85 end
map(key) { |new_key == true ? key : new_key| ... } click to toggle source
   # File lib/bismas/mapping.rb
87 def map(key)
88   return enum_for(__method__, key) unless block_given?
89 
90   fetch(key).each { |new_key|
91     yield new_key == true ? key : new_key if new_key }
92 end
range(list, &block) click to toggle source
   # File lib/bismas/mapping.rb
72 def range(list, &block)
73   return enum_for(__method__, list) unless block
74 
75   list.split(/\s*,\s*/).each { |part|
76     LITERALS.key?(part) ? block[LITERALS[part]] : begin
77       from, to = part.split('-')
78       from.upto(to || from, &block)
79     end
80   }
81 end