Class: Hash
- Inherits:
-
Object
show all
- Defined in:
- lib/klient/hash_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth, *args, &block) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/klient/hash_methods.rb', line 21
def method_missing(mth, *args, &block)
m = mth.to_s
if keys.include?(m)
self[m]
elsif m =~ /\S+=/
deep_set(m.gsub(/=/, ''), self, args[0])
else
deep_get(m, self)
end
end
|
Instance Method Details
#deep_get(key, obj, found = nil) ⇒ Object
2
3
4
5
6
7
8
9
|
# File 'lib/klient/hash_methods.rb', line 2
def deep_get(key, obj, found = nil)
if obj.respond_to?(:key?) && obj.key?(key)
return obj[key]
elsif obj.respond_to?(:each)
obj.find { |*a| found = deep_get(key, a.last) }
return found
end
end
|
#deep_set(key, obj, value, found = nil) ⇒ Object
11
12
13
14
15
16
17
18
19
|
# File 'lib/klient/hash_methods.rb', line 11
def deep_set(key, obj, value, found = nil)
if obj.respond_to?(:key?) && obj.key?(key)
obj[key] = value
return value
elsif obj.respond_to?(:each)
obj.find { |*a| found = deep_set(key, a.last, value) }
return found
end
end
|