Sha256: abf4664a94e22b96584f47ddbe05fbf52bcc4c5fda97d324867917443fad38fa

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module Configr
  module HashExtensions
    def symbolize_keys!
      self.each { |k, v|
        self.delete(k.to_s);
        self[k.to_sym] = v
      }
    end
    
    def recursive_symbolize_keys!
      self.symbolize_keys!
      self.values.select{ |value|
        value.is_a?(::Hash) || value.is_a?(Hash)
      }.each{ |hash|
        hash.recursive_symbolize_keys!
      }
    end
  end
  
  class Hash < ::Hash
    include HashExtensions
    
    def initialize(hash)
      hash ? hash.each_pair { |key, val| self.delete(key.to_s); self[key] = val } : self
    end
    
    def normalize!
      recursive_symbolize_keys!
      
      each_pair do |key, val|
        self[key] = Hash.new(val) if val.is_a?(::Hash)
      end
    end
    
    def recursive_normalize!
      normalize!
      values.select { |val| val.is_a?(::Hash) }.each { |hash| hash.recursive_normalize! }
    end
    
    def method_missing(method, *args, &block)
      self[method]
    end
  end
end

class Hash
  include Configr::HashExtensions
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
configr-0.2.0 lib/configr/hash.rb