class Hash

Constants

Inflector

Public Class Methods

inflect(method, *args) click to toggle source
# File lib/core_extended/hash.rb, line 22
def self.inflect(method, *args)
  inflectors << Inflector.new(method, args)
end
inflections(key) click to toggle source
# File lib/core_extended/hash.rb, line 26
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
inflectors() click to toggle source
# File lib/core_extended/hash.rb, line 18
def self.inflectors
  @inflectors ||= []
end

Public Instance Methods

dictionary(parent_key=nil) click to toggle source
# File lib/core_extended/hash.rb, line 5
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
get(key) click to toggle source
# File lib/core_extended/hash.rb, line 39
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
inflections() click to toggle source
# File lib/core_extended/hash.rb, line 35
def inflections
  keys.flat_map{ |k| self.class.inflections(k).map(&:to_sym) }.uniq
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/core_extended/hash.rb, line 53
def method_missing(method, *args, &block)
  return get(method) if inflections.include?(method.to_sym)
  super
end