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

ENUM_REF

Type of a reference - to an Enum, a BitSet or a Map.

RECORD

Type of a reference - to a Record.

UNRESOLVED

Type of a reference - unresolved.

Attributes

fromEntity[RW]

The Record the reference originates from, an instance of a Record

fromField[RW]

The field the reference originates from, an instance of a Field

key[RW]

The unique key for the reference to use in a hashmap.

sourceRef[RW]

Reference to the source where this reference has been defined, if any.

toEntity[RW]

The target entity for this Reference, determined by the resolve method. If it is a Record, the record must have an identity consisting from only one field.

toFields[RW]

The target Field of the reference, must be the only one field in the target Record's identity, determined by the resolve method.

type[RW]

The type of the reference, UNRESOLVED or RECORD or ENUM_REF.

Public Class Methods

new(sourceEntity, sourceField, targetEntitySpec, sourceRef = nil) click to toggle source

Creates an instance with the given parameters.

  • Parameters:

    • sourceEntity - the instance of Record the reference originates from.

    • sourceField - the instance of Field the reference originates from

    • targetEntitySpec - a string, specification of the target entity to be resolved

    • sourceRef - an instance of SourceFile

# 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(model) click to toggle source

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
to_s() click to toggle source

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