Class: Mxrb::Model::Entity

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

Overview

Entities are embedded in the DomainModel BSON — NOT separate Unit rows. $Type: DomainModels$Entity (read) / DomainModels$EntityImpl (write)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_rulesObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def access_rules
  @access_rules
end

#data_storage_guidObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def data_storage_guid
  @data_storage_guid
end

#documentationObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def documentation
  @documentation
end

#export_levelObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def export_level
  @export_level
end

#generalizationObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def generalization
  @generalization
end

#idObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def id
  @id
end

#indexesObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def indexes
  @indexes
end

#locationObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def location
  @location
end

#nameObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def name
  @name
end

#persistableObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def persistable
  @persistable
end

#qualified_nameObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def qualified_name
  @qualified_name
end

#system_membersObject

rubocop:disable Metrics/ClassLength



10
11
12
# File 'lib/mxrb/model/entity.rb', line 10

def system_members
  @system_members
end

Class Method Details

.apply_validation_rules(attributes, rules) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mxrb/model/entity.rb', line 89

def self.apply_validation_rules(attributes, rules)
  by_name = attributes.to_h { [_1.name, _1] }
  rules.each do |rule|
    attribute = by_name[rule['Attribute'].to_s.split('.').last]
    next unless attribute

    kind = rule.dig('RuleInfo', '$Type').to_s
    attribute.required = true if kind.end_with?('RequiredRuleInfo')
    attribute.unique = true if kind.end_with?('UniqueRuleInfo')
  end
end

.from_bson(doc, _domain_model_id, mpr) ⇒ Object

Build from a BSON hash (embedded in DomainModel's "entities" array).



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mxrb/model/entity.rb', line 16

def self.from_bson(doc, _domain_model_id, mpr)
  e                    = new
  e.id                 = IO::BsonCodec.extract_id(doc["$ID"] || doc["\$ID"])
  e.name               = doc["name"]  || doc["Name"]
  e.qualified_name     = doc["$QualifiedName"] || doc["\$QualifiedName"]
  e.documentation      = doc["documentation"] || doc["Documentation"] || ""
  e.data_storage_guid  = doc["dataStorageGuid"] || doc["DataStorageGuid"]
  e.export_level       = doc["exportLevel"] || doc["ExportLevel"] || "Hidden"
  e.location           = parse_location(doc["location"] || doc["Location"])

  # Persistable lives inside generalization.NoGeneralization.persistable
  gen = doc["generalization"] || doc["Generalization"] ||
        doc["maybeGeneralization"] || doc["MaybeGeneralization"]
  e.generalization = gen
  persistable = if gen.is_a?(Hash)
                  gen.fetch('persistable', gen.fetch('Persistable', true))
                else
                  true
                end
  e.persistable = persistable != false
  e.system_members = parse_system_members(gen)

  # Attributes (embedded array with marker)
  attr_arr   = IO::BsonCodec.parse_array(doc["attributes"] || doc["Attributes"])[:items]
  e.instance_variable_set(:@attributes, attr_arr.map { Attribute.from_bson(_1) })
  rules = IO::BsonCodec.parse_array(doc['validationRules'] || doc['ValidationRules'])[:items]
  apply_validation_rules(e.attributes, rules)
  raw_indexes = IO::BsonCodec.parse_array(doc['indexes'] || doc['Indexes'])[:items]
  e.indexes = normalize_indexes(raw_indexes, e.attributes, e.qualified_name)

  # Access rules (embedded array with marker 3)
  rule_arr = IO::BsonCodec.parse_array(doc["accessRules"] || doc["AccessRules"])[:items]
  e.access_rules = rule_arr.map { parse_access_rule(_1) }

  e
end

.normalize_indexes(indexes, attributes, qualified_name) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/mxrb/model/entity.rb', line 101

def self.normalize_indexes(indexes, attributes, qualified_name)
  names_by_id = attributes.to_h { [_1.id, _1.name] }
  indexes.each do |index|
    members = IO::BsonCodec.parse_array(index['Attributes'])[:items]
    members.each do |member|
      next if member['Attribute'] || member['attribute']

      id = IO::BsonCodec.extract_id(member['AttributePointer'])
      name = names_by_id[id]
      member['Attribute'] = [qualified_name, name].compact.join('.') if name
    end
  end
end

.parse_access_rule(doc) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mxrb/model/entity.rb', line 132

def self.parse_access_rule(doc)
  roles = IO::BsonCodec.parse_array(doc["ModuleRoles"] || doc["AllowedModuleRoles"])[:items]
  default_rights = doc["DefaultMemberAccessRights"] || "None"
  member_items = IO::BsonCodec.parse_array(doc["MemberAccesses"])[:items]
  members = member_items.map do |m|
    association_ref = m["Association"].to_s
    attr_ref = association_ref.empty? ? m["Attribute"] : association_ref
    kind = association_ref.empty? ? :attribute : :association
    { name: attr_ref.to_s.split(%r{[/.]}).last, reference: attr_ref.to_s,
      rights: m["AccessRights"] || "None", kind: kind }
  end
  {
    roles: roles,
    create: doc["AllowCreate"] == true,
    delete: doc["AllowDelete"] == true,
    default_rights: default_rights,
    members: members,
    xpath: doc["XPathConstraint"] || ""
  }
end

.parse_location(loc) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/mxrb/model/entity.rb', line 153

def self.parse_location(loc)
  if loc.is_a?(String)
    x, y = loc.split(';', 2).map(&:to_i)
    return { x: x, y: y }
  end
  return { x: 0, y: 0 } unless loc.is_a?(Hash)
  { x: loc["x"] || loc[:x] || 0, y: loc["y"] || loc[:y] || 0 }
end

.parse_system_members(generalization) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mxrb/model/entity.rb', line 115

def self.parse_system_members(generalization)
  return {} unless generalization.is_a?(Hash)

  {
    owner: generalization['hasOwner'] == true || generalization['HasOwnerAttr'] == true,
    created_date: generalization.values_at(
      'hasCreatedDate', 'HasCreatedDateAttr'
    ).include?(true),
    changed_date: generalization.values_at(
      'hasChangedDate', 'HasChangedDateAttr'
    ).include?(true),
    changed_by: generalization.values_at(
      'hasChangedBy', 'HasChangedByAttr'
    ).include?(true)
  }
end

Instance Method Details

#attributesObject



53
# File 'lib/mxrb/model/entity.rb', line 53

def attributes = @attributes || []

#generalization_targetObject



55
56
57
58
59
# File 'lib/mxrb/model/entity.rb', line 55

def generalization_target
  return unless @generalization.is_a?(Hash)

  @generalization['generalization'] || @generalization['Generalization']
end

#inspectObject



83
84
85
# File 'lib/mxrb/model/entity.rb', line 83

def inspect
  "#<Mxrb::Entity name=#{@name.inspect} attrs=#{attributes.size} persistable=#{@persistable}>"
end

#to_bsonObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mxrb/model/entity.rb', line 61

def to_bson
  {
    "$ID"             => @id,
    "$Type"           => "DomainModels$EntityImpl",
    "$QualifiedName"  => @qualified_name,
    "name"            => @name,
    "documentation"   => @documentation || "",
    "dataStorageGuid" => @data_storage_guid || SecureRandom.uuid,
    "location"        => serialize_location(@location),
    "generalization"  => serialize_generalization,
    "attributes"      => IO::BsonCodec.build_array(@attributes.map(&:to_bson)),
    "validationRules" => IO::BsonCodec.build_array([]),  # must come after attributes
    "eventHandlers"   => IO::BsonCodec.build_array([]),
    "indexes"         => IO::BsonCodec.build_array([]),
    "accessRules"     => IO::BsonCodec.build_array(@access_rules.to_a),
    "source"          => nil,
    "exportLevel"     => @export_level || "Hidden",
    "image"           => "",
    "imageData"       => "",
  }
end