Class Dsadmin::ConfigSection
In: lib/dsadmin/base_config_manager.rb
Parent: OpenStruct

Derivative of OpenStruct for use by BaseConfigManager.

Adds a to_a method, so that all parts of the config tree can be transparently treated as arrays.

Methods

coerce_into   get   get_child   to_a  

Included Modules

Contractor

Public Instance methods

Get a subtree (ConfigSection / ConfigValue / Array / …).

Identical to BaseConfigManager#get , but paths are considered relative to this ConfigSection.

[Source]

     # File lib/dsadmin/base_config_manager.rb, line 429
429:     def get(xpath, klass = nil, required = true)
430:       if(xpath.kind_of?(String))
431:         xpath = xpath.sub(%r{^/+}, '')
432:         xpath = xpath.split(%r{/+})
433:       end
434:       
435:       raise ArgumentError.new("Empty xpath given -- That's confusing as hell!") if(xpath.empty?)
436:       
437:       val = get_child(xpath[0])
438:       if(xpath.size > 1)
439:         raise BadConfigurationError.new("'#{xpath[0]}' should be an inner node") unless(val.kind_of?(ConfigSection))
440:         # Recursively try to get the result. The remaining path is passed on as array
441:         # to avoid costly re-parsing. "required" is set to false, because we handle the
442:         # "nil" case anyway.
443:         val = val.get(xpath[1..-1], klass, false)
444:       end
445:       
446:       if(val.nil? && required)    
447:         raise BadConfigurationError.new("Missing in configfile: '#{xpath.join('/')}'")
448:       end
449:       
450:       return coerce_into(val, klass)
451:     end

[Source]

     # File lib/dsadmin/base_config_manager.rb, line 420
420:     def to_a
421:       [self]
422:     end

Private Instance methods

Handle potential type mangling for get()

[Source]

     # File lib/dsadmin/base_config_manager.rb, line 482
482:     def coerce_into(val, klass)
483:       return val if(klass.nil?)
484:       return nil if(val.nil?)
485:       return val if(val.kind_of?(klass))
486:       
487:       if(klass == Array)
488:         return (val.kind_of?(Hash)) ? val.values : val.to_a
489:       elsif(klass <= Integer)
490:         unless(val.kind_of?(String))
491:           raise BadConfigurationError.new("'#{val}' expected to be a String, but is a #{val.class.name}")
492:         end
493:         return val.to_i
494:       elsif(klass <= Float)
495:         unless(val.kind_of?(String))
496:           raise BadConfigurationError.new("'#{val}' expected to be a String, but is a #{val.class.name}")
497:         end
498:         return val.to_f
499:       else
500:         raise BadConfigurationError.new("'#{val}' expected to be a #{klass.name}, but is a #{val.class.name}")
501:       end
502:     end

Get a direct child of this node, as specified by the given xpath element. An index can be given in the path element. See BaseConfigManager#get for some more info

[Source]

     # File lib/dsadmin/base_config_manager.rb, line 458
458:     def get_child(path_element)
459:       requireKindOf(String, path_element)
460:       
461:       if(path_element =~ /^(\w+)\[(.*)\]$/)
462:         cname = $1
463:         index = $2
464:         val = send(cname)
465:         return nil unless val
466:         if(val.class <= Hash)
467:           return val[index]
468:         elsif(index =~ /^\d+$/)
469:           return val.to_a[index.to_i]
470:         end
471:       elsif(path_element =~ /^[a-zA-Z_]\w*$/)
472:         return send(path_element)
473:       else
474:         raise ArgumentError.new("Bad xpath element: '#{path_element}'")
475:       end
476:       
477:       return nil
478:     end

[Validate]