Class: Ostructer::OsOpenStruct
- Inherits:
-
OsBase
show all
- Includes:
- Comparable
- Defined in:
- lib/ostructer.rb
Instance Attribute Summary
Attributes inherited from OsBase
#field, #logger, #parent
Instance Method Summary
collapse
Methods inherited from OsBase
#full_dot_path
Constructor Details
#initialize(hash_in, logger) ⇒ OsOpenStruct
Returns a new instance of OsOpenStruct.
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/ostructer.rb', line 182
def initialize( hash_in, logger )
@parent = nil
@field = "[!unknown_hash]"
@logger = logger
@hash = {}
hash_in.each do |key,value|
key = key[0...1].downcase + key[1..-1]
@hash[ key ] = value
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mn, *a) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/ostructer.rb', line 239
def method_missing(mn,*a)
mn = mn.to_s
if mn[-1].chr == "?"
mn = mn[0..-2] if @hash.has_key?(mn)
value = @hash.fetch( mn ).fetch( 'text' )
logger.debug "OsOpenStruct - boolean feld '#{mn}' with value '#{value}' returns #{value=='1'}"
return value == "1" else
logger.warn "OsOpenStruct - boolean feld '#{mn}' nicht gefunden in Hash using path '#{full_dot_path}'; returning OsOpenStructNil"
OsOpenStructNil.new( self, mn+"?", logger ) end
else
if @hash.has_key?(mn)
@hash[mn]
else
logger.warn "OsOpenStruct - feld '#{mn}' nicht gefunden in Hash using path '#{full_dot_path}'; returning OsOpenStructNil"
OsOpenStructNil.new( self, mn, logger )
end
end
end
|
Instance Method Details
#<=>(other) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/ostructer.rb', line 222
def <=>(other)
if other.is_a? Numeric
logger.debug "OsOpenStruct <=> Numeric: #{self.to_i} <=> #{other}"
self.to_i <=> other
elsif other.is_a? String
logger.debug "OsOpenStruct <=> String: #{self.to_s} <=> #{other}"
self.to_s <=> other
else
logger.debug "OsOpenStruct - no <=> defined for type #{other.class}"
nil
end
end
|
#fetch(key, default = nil) ⇒ Object
216
217
218
|
# File 'lib/ostructer.rb', line 216
def fetch( key, default = nil )
@hash.fetch( key, default)
end
|
#has_key?(key) ⇒ Boolean
211
212
213
214
|
# File 'lib/ostructer.rb', line 211
def has_key?( key )
@hash.has_key?( key.to_s )
end
|
292
293
294
295
296
|
# File 'lib/ostructer.rb', line 292
def to_bool
internal_value == 'true'
end
|
287
288
289
290
|
# File 'lib/ostructer.rb', line 287
def to_date
logger.debug "calling MyOpenStruct#to_date"
internal_value end
|
281
282
283
284
285
|
# File 'lib/ostructer.rb', line 281
def to_i
value = internal_value
value.nil? ? nil : value.to_i
end
|
#to_openstruct_pass2 ⇒ Object
link up: set parent and field
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/ostructer.rb', line 200
def to_openstruct_pass2 @hash.each do |key,value|
if value.is_a?( OsOpenStruct ) || value.is_a?( OsArray ) || value.is_a?( OsValue )
value.field = key.to_s
value.parent = self
end
value.to_openstruct_pass2
end
end
|
275
276
277
278
279
|
# File 'lib/ostructer.rb', line 275
def to_s
value = internal_value
value.nil? ? nil : value.to_s
end
|
268
269
270
271
272
273
|
# File 'lib/ostructer.rb', line 268
def to_xml
logger.debug "calling OsOpenStruct#to_xml"
CGI.unescapeHTML( internal_value )
end
|