| Class | MUES::Config::ConfigStruct |
| In: |
lib/mues/config.rb
(CVS)
|
| Parent: | MUES::Object |
Hash-wrapper that allows struct-like accessor calls on nested hashes.
| modified | [W] | Modification flag. Set to true to indicate the contents of the Struct have changed since it was created. |
Create a new ConfigStruct from the given hash.
# File lib/mues/config.rb, line 473 def initialize( hash ) @hash = hash.dup @modified = false end
Returns the number of items in the struct.
# File lib/mues/config.rb, line 488 def length return @hash.length end
Returns true if the given name is the name of a member of the receiver.
# File lib/mues/config.rb, line 538 def member?( name ) return @hash.key?( name.to_s.intern ) end
Return a new ConfigStruct which is the result of merging the receiver with the given other object (a Hash or another ConfigStruct).
# File lib/mues/config.rb, line 573 def merge( other ) self.dup.merge!( other ) end
Merge the specified other object with this config struct. The other object can be either a Hash or another ConfigStruct.
# File lib/mues/config.rb, line 551 def merge!( other ) case other when Hash @hash = self.to_h.merge( other, &MUES::HashMergeFunction ) when ConfigStruct @hash = self.to_h.merge( other.to_h, &MUES::HashMergeFunction ) else raise TypeError, "Don't know how to merge with a %p" % other.class end return self end
Returns true if the ConfigStruct or any of its sub-structs have changed since it was created.
# File lib/mues/config.rb, line 496 def modified? return @modified || @hash.values.find {|obj| obj.is_a?( ConfigStruct ) && obj.modified? } end
Return true if the receiver responds to the given method. Overridden to grok autoloaded methods.
# File lib/mues/config.rb, line 523 def respond_to?( sym, priv=false ) key = sym.to_s.sub( /(=|\?)$/, '' ).intern return true if @hash.key?( key ) super end
Return the receiver‘s values as a (possibly multi-dimensional) Hash with String keys.
# File lib/mues/config.rb, line 505 def to_h rhash = {} @hash.each {|k,v| case v when ConfigStruct rhash[k] = v.to_h when NilClass, FalseClass, TrueClass, Numeric, Symbol rhash[k] = v else rhash[k] = v.dup end } return rhash end
Handle calls to key-methods
# File lib/mues/config.rb, line 583 def method_missing( sym, *args ) key = sym.to_s.sub( /(=|\?)$/, '' ).intern super unless @hash.key?( key ) self.class.class_eval { define_method( key ) { if @hash[ key ].is_a?( Hash ) @hash[ key ] = ConfigStruct::new( @hash[key] ) end @hash[ key ] } define_method( "#{key}?" ) {@hash[key] ? true : false} define_method( "#{key}=" ) {|val| @hash[key] = val} } self.send( sym, *args ) end