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

#native_typeObject

rubocop:disable Metrics/ClassLength



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

def native_type
  @native_type
end

#oql_queryObject

rubocop:disable Metrics/ClassLength



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

def oql_query
  @oql_query
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

#sourceObject

rubocop:disable Metrics/ClassLength



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

def source
  @source
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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mxrb/model/entity.rb', line 105

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
52
53
54
# 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.native_type        = doc["$Type"]
  e.source             = doc["source"] || doc["Source"]
  e.oql_query          = doc["oqlQuery"] || doc["OqlQuery"] || doc["OQLQuery"]
  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



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

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mxrb/model/entity.rb', line 148

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



169
170
171
172
173
174
175
176
# File 'lib/mxrb/model/entity.rb', line 169

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



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

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



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

def attributes = @attributes || []

#generalization_targetObject



58
59
60
61
62
# File 'lib/mxrb/model/entity.rb', line 58

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

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

#inspectObject



99
100
101
# File 'lib/mxrb/model/entity.rb', line 99

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

#oql_source_documentObject



71
72
73
74
75
# File 'lib/mxrb/model/entity.rb', line 71

def oql_source_document
  return unless @source.is_a?(Hash)

  @source['SourceDocument'] || @source['sourceDocument']
end

#oql_view?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'lib/mxrb/model/entity.rb', line 64

def oql_view?
  source_type = @source.is_a?(Hash) ? @source.fetch('$Type', '') : ''
  @native_type.to_s.match?(/ViewEntity/i) ||
    source_type.match?(/OqlViewEntitySource/i) ||
    !@oql_query.to_s.empty?
end

#to_bsonObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mxrb/model/entity.rb', line 77

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