Class: Mxrb::Model::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/model/association.rb

Overview

Associations are embedded in DomainModel BSON — NOT separate Unit rows. $Type: DomainModels$Association

CRITICAL GOTCHA (from reverse engineering):

ParentID = entity FROM (the one with the FK) — counter-intuitive naming!
ChildID  = entity TO  (the referenced entity)

MemberAccess should be added ONLY to the FROM entity (ParentPointer), not ChildID.

Constant Summary collapse

TYPES =
%i[Reference ReferenceSet].freeze
OWNERS =
%i[Default Both].freeze
STORAGE_FMTS =
%i[Column Table].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#association_typeObject

Returns the value of attribute association_type.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def association_type
  @association_type
end

#delete_behaviorObject

Returns the value of attribute delete_behavior.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def delete_behavior
  @delete_behavior
end

#documentationObject

Returns the value of attribute documentation.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def documentation
  @documentation
end

#export_levelObject

Returns the value of attribute export_level.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def export_level
  @export_level
end

#from_entity_idObject

Returns the value of attribute from_entity_id.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def from_entity_id
  @from_entity_id
end

#idObject

Returns the value of attribute id.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def id
  @id
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def owner
  @owner
end

#storage_formatObject

Returns the value of attribute storage_format.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def storage_format
  @storage_format
end

#to_entity_idObject

Returns the value of attribute to_entity_id.



15
16
17
# File 'lib/mxrb/model/association.rb', line 15

def to_entity_id
  @to_entity_id
end

Class Method Details

.from_bson(doc, _domain_model_id = nil, _mpr = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mxrb/model/association.rb', line 25

def self.from_bson(doc, _domain_model_id = nil, _mpr = nil)
  a                  = new
  a.id               = IO::BsonCodec.extract_id(doc["$ID"] || doc["\$ID"])
  a.name             = doc["Name"] || doc["name"]
  a.documentation    = doc["Documentation"] || doc["documentation"] || ""
  # ParentID = FROM entity (counter-intuitive — see class comment)
  a.from_entity_id   = IO::BsonCodec.extract_id(
    doc["ParentPointer"] || doc["ParentID"] || doc["parentId"]
  )
  child = doc['Child'] || doc["ChildPointer"] || doc["ChildID"] || doc["childId"]
  a.to_entity_id = if child.is_a?(String) && child.include?('.')
                     child
                   else
                     IO::BsonCodec.extract_id(child)
                   end
  a.association_type = (doc["Type"] || doc["type"] || "Reference").to_sym
  a.owner            = (doc["Owner"] || doc["owner"] || "Default").to_sym
  a.storage_format   = (doc["StorageFormat"] || "Column").to_sym
  a.delete_behavior  = doc["DeleteBehavior"] || doc["deleteBehavior"]
  a.export_level     = doc["ExportLevel"] || "Hidden"
  a
end

Instance Method Details

#child_delete_behaviorObject



74
75
76
# File 'lib/mxrb/model/association.rb', line 74

def child_delete_behavior
  behavior_value('childDeleteBehavior', 'ChildDeleteBehavior')
end

#inspectObject



65
66
67
68
# File 'lib/mxrb/model/association.rb', line 65

def inspect
  "#<Mxrb::Association name=#{@name.inspect} type=#{@association_type} " \
    "from=#{@from_entity_id.to_s.slice(0, 8)} to=#{@to_entity_id.to_s.slice(0, 8)}>"
end

#parent_delete_behaviorObject



70
71
72
# File 'lib/mxrb/model/association.rb', line 70

def parent_delete_behavior
  behavior_value('parentDeleteBehavior', 'ParentDeleteBehavior')
end

#to_bsonObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mxrb/model/association.rb', line 48

def to_bson
  {
    "$ID"           => @id || SecureRandom.uuid,
    "$Type"         => "DomainModels$Association",
    "Name"          => @name,
    "Documentation" => @documentation || "",
    # ParentID = FROM entity (the one that carries the FK)
    "ParentID"      => @from_entity_id,
    "ChildID"       => @to_entity_id,
    "Type"          => (@association_type || :Reference).to_s,
    "Owner"         => (@owner || :Default).to_s,
    "StorageFormat" => (@storage_format || :Column).to_s,
    "DeleteBehavior" => @delete_behavior || default_delete_behavior,
    "ExportLevel"   => @export_level || "Hidden",
  }
end