Class: Arrow::Config::ConfigStruct
- Inherits:
-
Arrow::Object
- Object
- Arrow::Object
- Arrow::Config::ConfigStruct
- Extends:
- Forwardable
- Includes:
- Arrow::HashUtilities, Enumerable
- Defined in:
- lib/arrow/config.rb
Overview
Hash-wrapper that allows struct-like accessor calls on nested hashes.
Constant Summary
Constants included from Arrow::HashUtilities
Instance Attribute Summary
-
- (Object) modified
writeonly
Modification flag.
Instance Method Summary
-
- (Object) each(&block)
(also: #each_section)
Call into the given block for each member of the receiver.
-
- (ConfigStruct) initialize(hash)
constructor
Create a new ConfigStruct from the given hash.
-
- (Boolean) member?(name)
(also: #key?)
Returns true if the given name is the name of a member of the receiver.
-
- (Object) members
Returns an Array of Symbols, on for each of the struct’s members.
-
- (Object) merge(other)
Return a new ConfigStruct which is the result of merging the receiver with the given other object (a Hash or another ConfigStruct).
-
- (Object) merge!(other)
Merge the specified other object with this config struct.
-
- (Object) method_missing(sym, *args)
protected
Handle calls to key-methods.
-
- (Boolean) modified?
Returns true if the ConfigStruct or any of its sub-structs have changed since it was created.
-
- (Boolean) respond_to?(sym, priv = false)
Return true if the receiver responds to the given method.
-
- (Object) to_hash
(also: #to_h)
Return the receiver’s values as a (possibly multi-dimensional) Hash with String keys.
Methods included from Arrow::HashUtilities
#stringify_keys, #symbolify_keys
Methods inherited from Arrow::Object
deprecate_class_method, deprecate_method, inherited
Methods included from Arrow::Loggable
Constructor Details
- (ConfigStruct) initialize(hash)
Create a new ConfigStruct from the given hash.
406 407 408 409 |
# File 'lib/arrow/config.rb', line 406 def initialize( hash ) @hash = hash.dup @dirty = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(sym, *args) (protected)
Handle calls to key-methods
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'lib/arrow/config.rb', line 525 def method_missing( sym, *args ) key = sym.to_s.sub( /(=|\?)$/, '' ).to_sym return nil 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| @dirty = @hash[key] != val @hash[key] = val } } self.__send__( sym, *args ) end |
Instance Attribute Details
- (Object) modified=(value) (writeonly)
Modification flag. Set to true to indicate the contents of the Struct have changed since it was created.
418 419 420 |
# File 'lib/arrow/config.rb', line 418 def modified=(value) @modified = value end |
Instance Method Details
- (Object) each(&block) Also known as: each_section
Call into the given block for each member of the receiver.
477 478 479 |
# File 'lib/arrow/config.rb', line 477 def each( &block ) # :yield: member, value @hash.each( &block ) end |
- (Boolean) member?(name) Also known as: key?
Returns true if the given name is the name of a member of the receiver.
469 470 471 |
# File 'lib/arrow/config.rb', line 469 def member?( name ) return @hash.key?( name.to_sym ) end |
- (Object) members
Returns an Array of Symbols, on for each of the struct’s members.
462 463 464 |
# File 'lib/arrow/config.rb', line 462 def members @hash.keys end |
- (Object) merge(other)
Return a new ConfigStruct which is the result of merging the receiver with the given other object (a Hash or another ConfigStruct).
515 516 517 |
# File 'lib/arrow/config.rb', line 515 def merge( other ) self.dup.merge!( other ) end |
- (Object) merge!(other)
Merge the specified other object with this config struct. The other object can be either a Hash, another ConfigStruct, or an Arrow::Config.
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/arrow/config.rb', line 486 def merge!( other ) case other when Hash @hash = self.to_h.merge( other, &HashMergeFunction ) when ConfigStruct @hash = self.to_h.merge( other.to_h, &HashMergeFunction ) when Arrow::Config @hash = self.to_h.merge( other.struct.to_h, &HashMergeFunction ) else raise TypeError, "Don't know how to merge with a %p" % other.class end # :TODO: Actually check to see if anything has changed? @dirty = true return self end |
- (Boolean) modified?
Returns true if the ConfigStruct or any of its sub-structs have changed since it was created.
423 424 425 426 427 |
# File 'lib/arrow/config.rb', line 423 def modified? @dirty || @hash.values.find do |obj| obj.is_a?( ConfigStruct ) && obj.modified? end end |
- (Boolean) respond_to?(sym, priv = false)
Return true if the receiver responds to the given method. Overridden to grok autoloaded methods.
454 455 456 457 458 |
# File 'lib/arrow/config.rb', line 454 def respond_to?( sym, priv=false ) key = sym.to_s.sub( /(=|\?)$/, '' ).to_sym return true if self.member?( key ) super end |
- (Object) to_hash Also known as: to_h
Return the receiver’s values as a (possibly multi-dimensional) Hash with String keys.
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/arrow/config.rb', line 432 def to_hash rhash = {} @hash.each {|k,v| case v when ConfigStruct rhash[k] = v.to_h when NilClass, FalseClass, TrueClass, Numeric # No-op (can't dup) rhash[k] = v when Symbol rhash[k] = v.to_s else rhash[k] = v.dup end } return rhash end |