Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 152 lines (128 sloc) 4.078 kb
19cecc9 @spastorino HWIA relies on Hash#symbolize_keys and #stringify_keys extensions.
spastorino authored
1 require 'active_support/core_ext/hash/keys'
2
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
3 # This class has dubious semantics and we only have it so that
4 # people can write params[:key] instead of params['key']
5 # and they get the same value for both keys.
6
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
7 module ActiveSupport
8 class HashWithIndifferentAccess < Hash
a24a888 @wycats Limit Array#extract_options! to directl instances of Hash and HWIA. Add ...
wycats authored
9 def extractable_options?
10 true
11 end
12
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
13 def initialize(constructor = {})
14 if constructor.is_a?(Hash)
15 super()
16 update(constructor)
17 else
18 super(constructor)
19 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
20 end
21
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
22 def default(key = nil)
23 if key.is_a?(Symbol) && include?(key = key.to_s)
24 self[key]
25 else
26 super
27 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
28 end
29
eab4860 @fxn avoids a ton o warnings activesupport/lib/active_support/dependencies.rb...
fxn authored
30 def self.new_from_hash_copying_default(hash)
31 ActiveSupport::HashWithIndifferentAccess.new(hash).tap do |new_hash|
32 new_hash.default = hash.default
33 end
34 end
35
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
36 alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
37 alias_method :regular_update, :update unless method_defined?(:regular_update)
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
38
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
39 # Assigns a new value to the hash:
40 #
41 # hash = HashWithIndifferentAccess.new
42 # hash[:key] = "value"
43 #
44 def []=(key, value)
45 regular_writer(convert_key(key), convert_value(value))
46 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
47
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
48 # Updates the instantized hash with values from the second:
49 #
50 # hash_1 = HashWithIndifferentAccess.new
51 # hash_1[:key] = "value"
52 #
53 # hash_2 = HashWithIndifferentAccess.new
54 # hash_2[:key] = "New Value!"
55 #
56 # hash_1.update(hash_2) # => {"key"=>"New Value!"}
57 #
58 def update(other_hash)
59 other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
60 self
61 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
62
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
63 alias_method :merge!, :update
64
65 # Checks the hash for a key matching the argument passed in:
66 #
67 # hash = HashWithIndifferentAccess.new
68 # hash["key"] = "value"
69 # hash.key? :key # => true
70 # hash.key? "key" # => true
71 #
72 def key?(key)
73 super(convert_key(key))
74 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
75
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
76 alias_method :include?, :key?
77 alias_method :has_key?, :key?
78 alias_method :member?, :key?
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
79
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
80 # Fetches the value for the specified key, same as doing hash[key]
81 def fetch(key, *extras)
82 super(convert_key(key), *extras)
83 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
84
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
85 # Returns an array of the values at the specified indices:
86 #
87 # hash = HashWithIndifferentAccess.new
88 # hash[:a] = "x"
89 # hash[:b] = "y"
90 # hash.values_at("a", "b") # => ["x", "y"]
91 #
92 def values_at(*indices)
93 indices.collect {|key| self[convert_key(key)]}
94 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
95
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
96 # Returns an exact copy of the hash.
97 def dup
98 HashWithIndifferentAccess.new(self)
99 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
100
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
101 # Merges the instantized and the specified hashes together, giving precedence to the values from the second hash
102 # Does not overwrite the existing hash.
103 def merge(hash)
104 self.dup.update(hash)
105 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
106
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
107 # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second.
108 # This overloaded definition prevents returning a regular hash, if reverse_merge is called on a HashWithDifferentAccess.
109 def reverse_merge(other_hash)
110 super other_hash.with_indifferent_access
111 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
112
0920e69 @methodmissing ActiveSupport Hash optimizations [#2902 state:resolved]
methodmissing authored
113 def reverse_merge!(other_hash)
114 replace(reverse_merge( other_hash ))
115 end
116
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
117 # Removes a specified key from the hash.
118 def delete(key)
119 super(convert_key(key))
120 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
121
d692e6b @jeremy Restore HWIA#stringify_keys! and update changelog
jeremy authored
122 def stringify_keys!; self end
c976784 @jeremy Change HWIA#stringify_keys to return a HWIA not a Hash
jeremy authored
123 def stringify_keys; dup end
2472f10 @spastorino HWIA delegates to to_hash symbolize_keys and stringify_keys and bang met...
spastorino authored
124 undef :symbolize_keys!
125 def symbolize_keys; to_hash.symbolize_keys end
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
126 def to_options!; self end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
127
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
128 # Convert to a Hash with String keys.
129 def to_hash
0920e69 @methodmissing ActiveSupport Hash optimizations [#2902 state:resolved]
methodmissing authored
130 Hash.new(default).merge!(self)
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
131 end
132
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
133 protected
134 def convert_key(key)
135 key.kind_of?(Symbol) ? key.to_s : key
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
136 end
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
137
138 def convert_value(value)
139 case value
140 when Hash
141 value.with_indifferent_access
142 when Array
143 value.collect { |e| e.is_a?(Hash) ? e.with_indifferent_access : e }
144 else
145 value
146 end
147 end
148 end
b4a1718 @jeremy Convert hash extension modules to class reopens
jeremy authored
149 end
0bd668f @jeremy Namespace HashWithIndifferentAccess
jeremy authored
150
151 HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess
Something went wrong with that request. Please try again.