Class: Arrow::Config::ConfigStruct
Overview
Hash-wrapper that allows struct-like accessor calls on nested hashes.
Constant Summary
Constants included from HashUtilities
HashUtilities::HashMergeFunction
Instance Attribute Summary collapse
-
#modified ⇒ Object
writeonly
Modification flag.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
(also: #each_section)
Call into the given block for each member of the receiver.
-
#initialize(hash) ⇒ ConfigStruct
constructor
Create a new ConfigStruct from the given
hash
. -
#member?(name) ⇒ Boolean
(also: #key?)
Returns
true
if the givenname
is the name of a member of the receiver. -
#members ⇒ Object
Returns an Array of Symbols, on for each of the struct’s members.
-
#merge(other) ⇒ Object
Return a new ConfigStruct which is the result of merging the receiver with the given
other
object (a Hash or another ConfigStruct). -
#merge!(other) ⇒ Object
Merge the specified
other
object with this config struct. -
#modified? ⇒ Boolean
Returns
true
if the ConfigStruct or any of its sub-structs have changed since it was created. -
#respond_to?(sym, priv = false) ⇒ Boolean
Return
true
if the receiver responds to the given method. -
#to_hash ⇒ Object
(also: #to_h)
Return the receiver’s values as a (possibly multi-dimensional) Hash with String keys.
Methods included from HashUtilities
stringify_keys, symbolify_keys
Methods inherited from Object
deprecate_class_method, deprecate_method, inherited
Constructor Details
#initialize(hash) ⇒ ConfigStruct
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
#method_missing(sym, *args) ⇒ Object (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
#modified=(value) ⇒ Object (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
#each(&block) ⇒ Object 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 |
#member?(name) ⇒ Boolean 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 |
#members ⇒ Object
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 |
#merge(other) ⇒ Object
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 |
#merge!(other) ⇒ Object
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 |
#modified? ⇒ Boolean
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 |
#respond_to?(sym, priv = false) ⇒ Boolean
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 |
#to_hash ⇒ Object 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 |