Class: Confstruct::HashWithStructAccess
- Inherits:
-
Hash
- Object
- Hash
- Confstruct::HashWithStructAccess
show all
- Defined in:
- lib/confstruct/hash_with_struct_access.rb,
lib/confstruct/hash_with_struct_access.rb
Constant Summary
collapse
- @@ordered =
true
- @@hash_class =
Hash
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of HashWithStructAccess.
78
79
80
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 78
def initialize hash = @@hash_class.new
super(hash)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 167
def method_missing sym, *args, &block
name = sym.to_s.chomp('=').to_sym
result = nil
if name.to_s =~ /^add_(.+)!$/
name = $1.to_sym
self[name] = [] unless self.has_key?(name)
unless self[name].is_a?(Array)
raise TypeError, "Cannot #add! to a #{self[name].class}"
end
if args.length > 0
local_args = args.collect { |a| structurize! a }
result = self[name].push *local_args
elsif block_given?
result = HashWithStructAccess.new(@@hash_class.new)
self[name].push result
end
elsif args.length == 1
result = self[name] = args[0]
elsif args.length > 1
super(sym,*args,&block)
else
result = self[name]
if result.nil? and block_given?
result = self[name] = HashWithStructAccess.new(@@hash_class.new)
end
end
if block_given?
eval_or_yield result, &block
end
result
end
|
Instance Attribute Details
#default_values ⇒ Object
Returns the value of attribute default_values.
44
45
46
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 44
def default_values
@default_values
end
|
Class Method Details
.from_hash(hash) ⇒ Object
48
49
50
51
52
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 48
def from_hash hash
return hash if hash.is_a?(self)
symbolized_hash = symbolize_hash hash
self.new(symbolized_hash)
end
|
.ordered? ⇒ Boolean
54
55
56
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 54
def ordered?
@@ordered
end
|
.structurize(hash) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 58
def structurize hash
result = hash
if result.is_a?(Hash) and not result.is_a?(HashWithStructAccess)
result = HashWithStructAccess.new(result)
end
result
end
|
.symbolize(key) ⇒ Object
73
74
75
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 73
def symbolize key
(key.to_s.gsub(/\s+/,'_').to_sym rescue key.to_sym) || key
end
|
.symbolize_hash(hash) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 66
def symbolize_hash hash
hash.inject(@@hash_class.new) do |h,(k,v)|
h[symbolize k] = v.is_a?(Hash) ? symbolize_hash(v) : v
h
end
end
|
Instance Method Details
#[](key) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 82
def [] key
result = structurize! super(symbolize!(key))
if result.is_a?(Deferred)
result = eval_or_yield self, &result.block
end
result
end
|
#[]=(key, value) ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 90
def []= key,value
k = symbolize!(key)
v = structurize! value
if v.is_a?(Hash) and self[k].is_a?(Hash)
self[k].replace(v)
else
super(k, v)
end
end
|
#deep_copy ⇒ Object
Also known as:
inheritable_copy
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 100
def deep_copy
result = self.class.new(@@hash_class.new)
self.each_pair do |k,v|
if v.respond_to?(:deep_copy)
result[k] = v.deep_copy
else
result[k] = Marshal.load(Marshal.dump(v)) rescue v.dup
end
end
result
end
|
#deep_merge(hash) ⇒ Object
113
114
115
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 113
def deep_merge hash
do_deep_merge! hash, self.deep_copy
end
|
#deep_merge!(hash) ⇒ Object
117
118
119
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 117
def deep_merge! hash
do_deep_merge! hash, self
end
|
#deferred!(&block) ⇒ Object
121
122
123
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 121
def deferred! &block
Confstruct.deferred(&block)
end
|
#has?(key_path) ⇒ Boolean
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 125
def has? key_path
val = self
keys = key_path.split(/\./)
keys.each do |key|
return false if val.nil?
if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
val = val[key.to_sym]
else
return false
end
end
return true
end
|
#i18n!(key = nil, &block) ⇒ Object
139
140
141
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 139
def i18n! key=nil, &block
Confstruct.i18n(key,&block)
end
|
#inspect ⇒ Object
143
144
145
146
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 143
def inspect
r = self.keys.collect { |k| "#{k.inspect}=>#{self.fetch(k).inspect}" }
"{#{r.compact.join(', ')}}"
end
|
#kind_of?(klazz) ⇒ Boolean
Also known as:
is_a?
148
149
150
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 148
def kind_of? klazz
@@hash_class.ancestors.include?(klazz) or super
end
|
#lookup!(key_path, fallback = nil) ⇒ Object
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 153
def lookup! key_path, fallback = nil
val = self
keys = key_path.split(/\./)
keys.each do |key|
return fallback if val.nil?
if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
val = val[key.to_sym]
else
return fallback
end
end
return val
end
|
#methods ⇒ Object
200
201
202
203
204
205
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 200
def methods
key_methods = keys.collect do |k|
self[k].is_a?(Deferred) ? k.to_s : [k.to_s, "#{k}="]
end
super + key_methods.compact.flatten
end
|
#ordered? ⇒ Boolean
207
208
209
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 207
def ordered?
self.class.ordered?
end
|
#respond_to?(*args) ⇒ Boolean
211
212
213
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 211
def respond_to? *args
super(*args) || keys.include?(symbolize!(args[0].to_s.sub(/=$/,'')))
end
|
#structurize!(hash) ⇒ Object
215
216
217
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 215
def structurize! hash
self.class.structurize(hash)
end
|
#symbolize!(key) ⇒ Object
219
220
221
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 219
def symbolize! key
self.class.symbolize(key)
end
|
#values ⇒ Object
223
224
225
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 223
def values
keys.collect { |k| self[k] }
end
|