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.
77
78
79
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 77
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
166
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
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 166
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
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 48
def from_hash hash
symbolized_hash = symbolize_hash hash
self.new(symbolized_hash)
end
|
.ordered? ⇒ Boolean
53
54
55
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 53
def ordered?
@@ordered
end
|
.structurize(hash) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 57
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
72
73
74
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 72
def symbolize key
(key.to_s.gsub(/\s+/,'_').to_sym rescue key.to_sym) || key
end
|
.symbolize_hash(hash) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 65
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
81
82
83
84
85
86
87
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 81
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
89
90
91
92
93
94
95
96
97
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 89
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
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 99
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
112
113
114
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 112
def deep_merge hash
do_deep_merge! hash, self.deep_copy
end
|
#deep_merge!(hash) ⇒ Object
116
117
118
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 116
def deep_merge! hash
do_deep_merge! hash, self
end
|
#deferred!(&block) ⇒ Object
120
121
122
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 120
def deferred! &block
Confstruct.deferred(&block)
end
|
#has?(key_path) ⇒ Boolean
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 124
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
138
139
140
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 138
def i18n! key=nil, &block
Confstruct.i18n(key,&block)
end
|
#inspect ⇒ Object
142
143
144
145
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 142
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?
147
148
149
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 147
def kind_of? klazz
@@hash_class.ancestors.include?(klazz) or super
end
|
#lookup!(key_path, fallback = nil) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 152
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
199
200
201
202
203
204
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 199
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
206
207
208
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 206
def ordered?
self.class.ordered?
end
|
#respond_to?(*args) ⇒ Boolean
210
211
212
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 210
def respond_to? *args
super(*args) || keys.include?(symbolize!(args[0].to_s.sub(/=$/,'')))
end
|
#structurize!(hash) ⇒ Object
214
215
216
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 214
def structurize! hash
self.class.structurize(hash)
end
|
#symbolize!(key) ⇒ Object
218
219
220
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 218
def symbolize! key
self.class.symbolize(key)
end
|
#values ⇒ Object
222
223
224
|
# File 'lib/confstruct/hash_with_struct_access.rb', line 222
def values
keys.collect { |k| self[k] }
end
|