Class: P4::Spec

Inherits:
Hash
  • Object
show all
Defined in:
lib/P4.rb

Overview

***************************************************************************** The P4::Spec class holds the fields in a Perforce spec *****************************************************************************

Instance Method Summary collapse

Constructor Details

#initialize(fieldmap = nil) ⇒ Spec

Returns a new instance of Spec.



351
352
353
# File 'lib/P4.rb', line 351

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.

Raises:

  • (P4Exception)


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/P4.rb', line 383

def method_missing( m, *a )
  k = m.to_s.downcase
  
  # Check if we're being asked for 'to_ary'.  If so, raise 'NoMethodError'.
  return :to_ary ? raise( NoMethodError ) : super 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.



360
361
362
363
364
365
366
367
368
# File 'lib/P4.rb', line 360

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_fieldsObject

Return the list of the fields that are permitted in this spec



373
374
375
# File 'lib/P4.rb', line 373

def permitted_fields
  @fields.values
end