Class: Render::Extensions::DottableHash
Class Method Summary
collapse
Instance Method Summary
collapse
#initialize, #recursively_symbolize_keys!, #symbolize_keys, #symbolize_keys!
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/render/extensions/dottable_hash.rb', line 65
def method_missing(method, *arguments)
if method.match(/\=$/)
self[method.to_s.chop] = arguments.first
elsif has_key?(method)
self[method]
else
super
end
end
|
Class Method Details
.initialize_element(value) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/render/extensions/dottable_hash.rb', line 20
def initialize_element(value)
case value
when Hash
new(value)
when Array
values = value.collect do |v|
initialize_element(v)
end
SymbolizableArray.new(values)
else
value
end
end
|
.new(element_to_hash = {}) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/render/extensions/dottable_hash.rb', line 8
def new(element_to_hash = {})
symbolize_hash = SymbolizableHash.new.merge!(element_to_hash)
symbolize_hash.symbolize_keys!
hash = super().merge!(symbolize_hash)
hash.each do |key, value|
hash[key] = initialize_element(value)
end
hash
end
|
Instance Method Details
#[](key) ⇒ Object
41
42
43
44
|
# File 'lib/render/extensions/dottable_hash.rb', line 41
def [](key)
key = key.to_sym
super
end
|
#[]=(key, value) ⇒ Object
35
36
37
38
39
|
# File 'lib/render/extensions/dottable_hash.rb', line 35
def []=(key, value)
key = key.to_sym
value = self.class.initialize_element(value)
super
end
|
#delete(key) ⇒ Object
46
47
48
49
|
# File 'lib/render/extensions/dottable_hash.rb', line 46
def delete(key)
key = key.to_sym
super
end
|
#fetch(key, *args) ⇒ Object
75
76
77
78
|
# File 'lib/render/extensions/dottable_hash.rb', line 75
def fetch(key, *args)
key = key.to_sym
super
end
|
#has_key?(key) ⇒ Boolean
51
52
53
|
# File 'lib/render/extensions/dottable_hash.rb', line 51
def has_key?(key)
super(key.to_sym)
end
|
#merge(other_hash) ⇒ Object
60
61
62
63
|
# File 'lib/render/extensions/dottable_hash.rb', line 60
def merge(other_hash)
other_hash = SymbolizableHash.new().merge!(other_hash)
super(other_hash.recursively_symbolize_keys!)
end
|
#merge!(other_hash) ⇒ Object
55
56
57
58
|
# File 'lib/render/extensions/dottable_hash.rb', line 55
def merge!(other_hash)
other_hash = SymbolizableHash.new().merge!(other_hash)
super(other_hash.recursively_symbolize_keys!)
end
|