Class: DataMetaDom::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/dataMetaDom/ref.rb

Overview

A reference to another entity on this Model, any of:

  • Record

  • Enum

  • BitSet

  • Map

For command line details either check the new method’s source or the README.rdoc file, the usage section.

Constant Summary collapse

UNRESOLVED =

Type of a reference - unresolved.

:u
RECORD =

Type of a reference - to a Record.

:r
ENUM_REF =

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

:e

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sourceEntity, sourceField, targetEntitySpec, sourceRef = nil) ⇒ Reference

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



78
79
80
81
82
83
# 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

Instance Attribute Details

#fromEntityObject

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



46
47
48
# File 'lib/dataMetaDom/ref.rb', line 46

def fromEntity
  @fromEntity
end

#fromFieldObject

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



41
42
43
# File 'lib/dataMetaDom/ref.rb', line 41

def fromField
  @fromField
end

#keyObject

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



36
37
38
# File 'lib/dataMetaDom/ref.rb', line 36

def key
  @key
end

#sourceRefObject

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



63
64
65
# File 'lib/dataMetaDom/ref.rb', line 63

def sourceRef
  @sourceRef
end

#toEntityObject

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.



58
59
60
# File 'lib/dataMetaDom/ref.rb', line 58

def toEntity
  @toEntity
end

#toFieldsObject

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



52
53
54
# File 'lib/dataMetaDom/ref.rb', line 52

def toFields
  @toFields
end

#typeObject

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



68
69
70
# File 'lib/dataMetaDom/ref.rb', line 68

def type
  @type
end

Instance Method Details

#resolve(model) ⇒ Object

Resolve the target entity and the field on the given Model.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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_sObject

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.



117
118
119
120
121
122
123
124
# 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