Sha256: ddbdfdc2da119052fc4ae3023a71f8902124a03bfc38a87aa6516a4784d74426
Contents?: true
Size: 1.18 KB
Versions: 3
Compression:
Stored size: 1.18 KB
Contents
class Hash Inflector = Struct.new :method, :arguments def dictionary(parent_key=nil) Hash.new.tap do |hash| each do |k,v| key = [parent_key, k].compact.join('.') if v.is_a? Hash hash.merge! v.dictionary(key) else hash[key] = v end end end end def self.inflectors @inflectors ||= [] end def self.inflect(method, *args) inflectors << Inflector.new(method, args) end def self.inflections(key) [key.to_s, key.to_s.to_sym].tap do |list| inflectors.each do |i| k = key.to_s.send i.method, *i.arguments list.concat [k, k.to_sym] end end.uniq end def inflections keys.flat_map{ |k| self.class.inflections(k).map(&:to_sym) }.uniq end def get(key) indiferent_access = [key, key.to_s, key.to_s.to_sym].uniq indiferent_access.each do |k| return self[k] if key?(k) end Hash[keys.map{ |k| [k, self.class.inflections(k)] }].each do |k,v| return self[k] if (v & indiferent_access).any? end nil end def method_missing(method, *args, &block) return get(method) if inflections.include?(method.to_sym) super end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
core_extended-0.0.10 | lib/core_extended/hash.rb |
core_extended-0.0.9 | lib/core_extended/hash.rb |
core_extended-0.0.8 | lib/core_extended/hash.rb |