Class: Wedge::IndifferentHash
- Inherits:
-
Hash
show all
- Defined in:
- lib/wedge/utilis/indifferent_hash.rb
Instance Method Summary
collapse
Methods inherited from Hash
#deep_dup, #deep_merge, #extract!, #indifferent, #slice, #slice!, #to_obj
Constructor Details
#initialize(constructor = {}, &block) ⇒ IndifferentHash
Returns a new instance of IndifferentHash.
4
5
6
7
8
9
10
11
12
13
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 4
def initialize(constructor = {}, &block)
if block_given?
yield self
elsif constructor.is_a?(Hash)
super()
update(constructor)
else
super(constructor)
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 195
def method_missing(m, *args, &block)
m = m.to_s
if m.chomp!('=') && args.count == 1
set_and_cache_value(m, *args)
elsif args.empty? && block_given?
self.navigate_hash_from_block m, &block
elsif args.empty?
get_and_cache_value(m)
else
fail ArgumentError, "wrong number of arguments (#{args.count} for 0)"
end
end
|
Instance Method Details
#==(other_hash) ⇒ Object
18
19
20
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 18
def ==(other_hash)
to_hash == self.class.new(other_hash).to_hash
end
|
33
34
35
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 33
def [](key)
get_value convert_key(key)
end
|
#[]=(key, value) ⇒ Object
Also known as:
store
27
28
29
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 27
def []=(key, value)
set_value convert_key(key), navigate(value)
end
|
#convert_array_for_to_hash(value) ⇒ Object
142
143
144
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 142
def convert_array_for_to_hash(value)
value.map { |item| convert_for_to_hash item }
end
|
#convert_for_to_hash(value) ⇒ Object
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 123
def convert_for_to_hash(value)
case value
when IndifferentHash
convert_indifferent_hash_for_to_hash value
when Array
convert_array_for_to_hash value
else
convert_value_for_to_hash value
end
end
|
#convert_indifferent_hash_for_to_hash(value) ⇒ Object
138
139
140
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 138
def convert_indifferent_hash_for_to_hash(value)
value.to_hash
end
|
#convert_key(key) ⇒ Object
134
135
136
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 134
def convert_key(key)
key.kind_of?(Symbol) ? key.to_s : key
end
|
#convert_value_for_to_hash(value) ⇒ Object
146
147
148
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 146
def convert_value_for_to_hash(value)
value
end
|
#delete(key) ⇒ Object
Removes a specified key from the hash.
38
39
40
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 38
def delete(key)
super(convert_key(key))
end
|
Returns an exact copy of the hash.
43
44
45
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 43
def dup
self.class.new to_hash
end
|
#fetch(key, *extras) ⇒ Object
Same as Hash#fetch
where the key passed as argument can be either a string or a symbol:
counters = IndifferentHash.new
counters[:foo] = 1
counters.fetch("foo") counters.fetch(:bar, 0) counters.fetch(:bar) {|key| 0} counters.fetch(:zoo)
58
59
60
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 58
def fetch(key, *)
super(convert_key(key), *)
end
|
#key?(key) ⇒ Boolean
Also known as:
include?, has_key?, member?
Checks the hash for a key matching the argument passed in:
hash = IndifferentHash.new
hash["key"] = "value"
hash.key? :key hash.key? "key"
69
70
71
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 69
def key?(key)
super(convert_key(key))
end
|
#merge(hash) ⇒ Object
Merges the instantiated and the specified hashes together, giving precedence to the values from the second hash. Does not overwrite the existing hash.
79
80
81
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 79
def merge(hash)
self.dup.update(hash)
end
|
#navigate(value) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 150
def navigate value
case value
when self.class
value
when Hash
navigate_hash value
when Array
navigate_array value
else
navigate_value value
end
end
|
#navigate_array(value) ⇒ Object
167
168
169
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 167
def navigate_array(value)
value.map { |item| navigate item }
end
|
#navigate_hash(value) ⇒ Object
163
164
165
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 163
def navigate_hash(value)
self.class.new value
end
|
#navigate_hash_from_block(key, &block) ⇒ Object
175
176
177
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 175
def navigate_hash_from_block(key, &block)
self[key] = self.class.new &block
end
|
#navigate_value(value) ⇒ Object
171
172
173
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 171
def navigate_value(value)
value
end
|
#respond_to?(m, include_private = false) ⇒ Boolean
83
84
85
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 83
def respond_to?(m, include_private = false)
has_key?(m) || super
end
|
#to_hash ⇒ Object
Also known as:
to_h
87
88
89
90
91
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 87
def to_hash
reduce({}) do |hash, (key, value)|
hash.merge key.to_sym => convert_for_to_hash(value)
end
end
|
#update(other_hash) ⇒ Object
Also known as:
merge!
Updates the instantized hash with values from the second:
hash_1 = IndifferentHash.new
hash_1[:key] = "value"
hash_2 = IndifferentHash.new
hash_2[:key] = "New Value!"
hash_1.update(hash_2)
104
105
106
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 104
def update(other_hash)
other_hash.reduce(self) { |hash, (k, v)| hash[k] = navigate(v) ; hash }
end
|
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:
hash = IndifferentHash.new
hash[:a] = "x"
hash[:b] = "y"
hash.values_at("a", "b")
117
118
119
|
# File 'lib/wedge/utilis/indifferent_hash.rb', line 117
def values_at(*indices)
indices.collect {|key| self[convert_key(key)]}
end
|