summaryrefslogtreecommitdiff
path: root/lib/hashie/hash_extensions.rb
blob: 188c10b23568c268a9714d36e622c342bcb2e63d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Hashie
  module HashExtensions
    def self.included(base)
      # Don't tread on existing extensions of Hash by
      # adding methods that are likely to exist.
      %w(stringify_keys stringify_keys!).each do |hashie_method|
        base.send :alias_method, hashie_method, "hashie_#{hashie_method}" unless base.instance_methods.include?(hashie_method)
      end
    end
    
    # Destructively convert all of the keys of a Hash
    # to their string representations.
    def hashie_stringify_keys!
      self.keys.each do |k|
        self[k.to_s] = self.delete(k)
      end
      self
    end
  
    # Convert all of the keys of a Hash
    # to their string representations.
    def hashie_stringify_keys
      self.dup.stringify_keys!
    end
    
    # Convert this hash into a Mash
    def to_mash
      Hashie::Mash.new(self)
    end
  end
end