Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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