Class: P4::Spec
- Inherits:
-
Hash
- Object
- Hash
- P4::Spec
- Defined in:
- lib/P4.rb
Overview
***************************************************************************** The P4::Spec class holds the fields in a Perforce spec *****************************************************************************
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Override the default assignment method.
-
#initialize(fieldmap = nil) ⇒ Spec
constructor
A new instance of Spec.
-
#method_missing(m, *a) ⇒ Object
Implement accessor methods for the fields in the spec.
-
#permitted_fields ⇒ Object
Return the list of the fields that are permitted in this spec.
Constructor Details
#initialize(fieldmap = nil) ⇒ Spec
Returns a new instance of Spec.
466 467 468 |
# File 'lib/P4.rb', line 466 def initialize( fieldmap = nil ) @fields = fieldmap end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a) ⇒ Object
Implement accessor methods for the fields in the spec. The accessor methods are all prefixed with ‘_’ to avoid conflicts with the Hash class’ namespace. This is a little ugly, but we gain a lot by subclassing Hash so it’s worth it.
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/P4.rb', line 498 def method_missing( m, *a ) k = m.to_s.downcase # Check if we're being asked for 'to_ary'. If so, raise 'NoMethodError'. raise NoMethodError if( k == "to_ary" ) if( k[0..0] != "_" ) raise( RuntimeError, "undefined method `#{m.to_s}' for object of " + "class #{self.class.to_s}" ) end k = k[ 1..-1 ] if( k =~ /(.*)=$/ ) if( a.length() == 0 ) raise( P4Exception, "Method P4##{m} requires an argument" ); end k = $1 if( @fields == nil || @fields.has_key?( k ) ) return self[ @fields[ k ] ] = a.shift end elsif( self.has_key?( m.to_s ) ) return self[ m.to_s ] elsif( @fields.has_key?( k ) ) return self[ @fields[ k ] ] end raise( P4Exception, "Invalid field: #{$1}" ) end |
Instance Method Details
#[]=(key, value) ⇒ Object
Override the default assignment method. This implementation ensures that any fields defined are valid ones for this type of spec.
475 476 477 478 479 480 481 482 483 |
# File 'lib/P4.rb', line 475 def []=( key, value ) if( self.has_key?( key ) || @fields == nil ) super( key, value ) elsif( @fields.has_key?( key.downcase ) ) super( @fields[ key.downcase ], value ) else raise( P4Exception, "Invalid field: #{key}" ) end end |
#permitted_fields ⇒ Object
Return the list of the fields that are permitted in this spec
488 489 490 |
# File 'lib/P4.rb', line 488 def permitted_fields @fields.values end |