Module: HashWiaModule
- Included in:
- HashWia
- Defined in:
- lib/hash_wia/module.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/hash_wia/module.rb', line 91
def method_missing name, *args, &block
strname = name.to_s
if strname.sub!(/\?$/, '')
!!self[strname]
elsif strname.sub!(/=$/, '')
self[strname] = args.first
else
value = self[strname]
value = self[strname.to_sym] if value.nil?
if value.nil?
if block
self[strname.to_s] = block
else
if key?(strname) || key(strname.to_sym)
nil
else
raise NoMethodError.new('%s not defined in HashWia' % strname)
end
end
else
if value.class == Array
value.map! {|el| el.class == Hash ? HashWia.new(el) : el }
end
value
end
end
end
|
Instance Method Details
#[](key) ⇒ Object
15
16
17
18
19
20
|
# File 'lib/hash_wia/module.rb', line 15
def [] key
key = key.to_s unless key.respond_to?(:to_sym)
data = super(key.to_s) || super(key.to_sym)
data.extend HashWiaModule if data.is_a?(Hash)
data
end
|
#[]=(key, value) ⇒ Object
22
23
24
25
26
|
# File 'lib/hash_wia/module.rb', line 22
def []= key, value
key = key.to_s unless key.class == Symbol
delete(key.to_s).nil? && delete(key.to_sym)
super key, value
end
|
#clone ⇒ Object
true clone of the hash with 0 references to the old one
48
49
50
|
# File 'lib/hash_wia/module.rb', line 48
def clone
Marshal.load(Marshal.dump(self))
end
|
#delete(key) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/hash_wia/module.rb', line 28
def delete key
data = super(key)
skey = key.to_s
data = super(skey) if data.nil?
data = super(skey.to_sym) if data.nil? && key.class != Symbol
data
end
|
#each(&block) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/hash_wia/module.rb', line 62
def each &block
to_a.each do |key, data|
if data.is_a?(Hash)
data.extend HashWiaModule
end
block.call key, data
end
self
end
|
#initialize(hash = nil) ⇒ Object
2
3
4
5
6
|
# File 'lib/hash_wia/module.rb', line 2
def initialize hash = nil
if hash
hash.each { |k,v| self[k] = v }
end
end
|
#key(name = nil) ⇒ Object
key is common id direct access allow direct get or fuction the same if name given
43
44
45
|
# File 'lib/hash_wia/module.rb', line 43
def key name = nil
self[name.nil? ? :key : name]
end
|
#merge(hash) ⇒ Object
52
53
54
55
56
|
# File 'lib/hash_wia/module.rb', line 52
def merge hash
dup.tap do |h|
hash.each { |k, v| h[k.to_s] = v }
end
end
|
#merge!(hash) ⇒ Object
58
59
60
|
# File 'lib/hash_wia/module.rb', line 58
def merge! hash
hash.each { |k, v| self[k.to_s] = v }
end
|
#to_ary ⇒ Object
we never return array from hash, ruby internals
37
38
39
|
# File 'lib/hash_wia/module.rb', line 37
def to_ary
nil
end
|
#values ⇒ Object
85
86
87
88
89
|
# File 'lib/hash_wia/module.rb', line 85
def values
super.map do |el|
el.class == Hash ? HashWia.new(el) : el
end
end
|