Class MUES::Config::ConfigStruct
In: lib/mues/config.rb  (CVS)
Parent: MUES::Object

Hash-wrapper that allows struct-like accessor calls on nested hashes.

Methods

each   length   member?   members   merge   merge!   method_missing   modified?   new   nitems   respond_to?   to_h  

Included Modules

Enumerable

Attributes

modified  [W]  Modification flag. Set to true to indicate the contents of the Struct have changed since it was created.

Public Class methods

Create a new ConfigStruct from the given hash.

[Source]

# File lib/mues/config.rb, line 473
            def initialize( hash )
                @hash = hash.dup
                @modified = false
            end

Public Instance methods

Call into the given block for each member of the receiver.

[Source]

# File lib/mues/config.rb, line 544
            def each( &block ) # :yield: member, value
                @hash.each( &block )
            end

Returns the number of items in the struct.

[Source]

# 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.

[Source]

# File lib/mues/config.rb, line 538
            def member?( name )
                return @hash.key?( name.to_s.intern )
            end

Returns an Array of the names of the struct‘s members.

[Source]

# File lib/mues/config.rb, line 531
            def members
                @hash.keys.collect {|sym| sym.to_s}
            end

Return a new ConfigStruct which is the result of merging the receiver with the given other object (a Hash or another ConfigStruct).

[Source]

# 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.

[Source]

# 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.

[Source]

# File lib/mues/config.rb, line 496
            def modified?
                return @modified || @hash.values.find {|obj|
                    obj.is_a?( ConfigStruct ) && obj.modified?
                }
            end
nitems()

Alias for length

Return true if the receiver responds to the given method. Overridden to grok autoloaded methods.

[Source]

# 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.

[Source]

# 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

Protected Instance methods

Handle calls to key-methods

[Source]

# 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

[Validate]