class DataMetaDom::Reference
A reference to another entity on this Model
, any of:
For command line details either check the new method's source or the README.rdoc file, the usage section.
Constants
Attributes
The field the reference originates from, an instance of a Field
The unique key for the reference to use in a hashmap.
Reference
to the source where this reference has been defined, if any.
The target Field
of the reference, must be the only one field in the target Record's identity, determined by the resolve method.
The type of the reference, UNRESOLVED
or RECORD
or ENUM_REF
.
Public Class Methods
Creates an instance with the given parameters.
-
Parameters:
-
sourceEntity
- the instance ofRecord
the reference originates from. -
sourceField
- the instance ofField
the reference originates from -
targetEntitySpec
- a string, specification of the target entity to be resolved -
sourceRef
- an instance ofSourceFile
-
# File lib/dataMetaDom/ref.rb, line 78 def initialize(sourceEntity, sourceField, targetEntitySpec, sourceRef = nil) @sourceRef = sourceRef @targetEntitySpec = targetEntitySpec.to_sym; @fromEntity = sourceEntity; @fromField = sourceField @type = UNRESOLVED self end
Public Instance Methods
Resolve the target entity and the field on the given Model
.
# File lib/dataMetaDom/ref.rb, line 88 def resolve(model) #@fromField = @fromEntity[@sourceFieldSpec.to_sym] #raise "The field #@sourceFieldSpec is not defined on this entity: #@fromEntity, #@sourceRef" unless @fromField @toEntity = model.records[@targetEntitySpec] || model.enums[@targetEntitySpec] raise "Target entity #{@targetEntitySpec} is not defined; #{@sourceRef}" unless @toEntity case # IMPORTANT!! make sure that you do not inspect and do not use Entity.to_s - this will blow up the stack when @toEntity.kind_of?(Enum), @toEntity.kind_of?(BitSet), @toEntity.kind_of?(Mappings) @type = ENUM_REF @key= "#{@type}/#{@fromEntity.name}.#{@fromField.name}->#{@toEntity.name}" when @toEntity.kind_of?(Record) idy = @toEntity.identity # raise "#@targetEntitySpec does not have an identity, can not be referenced to in IDL; #@sourceRef" unless idy # # raise "Invalid ref #{@fromEntity.name}.#{@toField.name} -> #@toEntity"\ # "- it has no singular ID; #@sourceRef" unless idy.args.length == 1 @type = RECORD @toFields = idy ? idy.args : @toEntity.fields.keys # raise "The field #{idy.args[0]} is not defined on this entity: #@toEntity; #@sourceRef" unless @toField @key = "#{@type}/#{@fromEntity.name}.#{@fromField.name}->#{@toEntity.name}.#{@toFields.join(',')}" else raise "Unsupported target entity: #{@toEntity.name}, for #{@sourceRef}" end end
Textual representation of this Reference
. Need to be careful because the to_s
on the Record
includes a list of references and if you include a Record
in this textual, this will cause infinite recursion with the stack failure.
# File lib/dataMetaDom/ref.rb, line 117 def to_s case @type when UNRESOLVED; "Unresolved: #{@fromEntity.name}.#{@fromField.name} ==> #@targetEntitySpec; #@sourceRef" when RECORD; "Record Ref: #@key" when ENUM_REF; "Enum ref: #@key" else; raise "Unsupported reference type #@type; #@sourceRef" end end