Class: Mxrb::Semantic::AddEntityPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/semantic/domain_mutator.rb

Overview

Plan that adds a new entity to a module's domain model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, module_name:, domain_unit_id:, entity_def:) ⇒ AddEntityPlan

Returns a new instance of AddEntityPlan.



176
177
178
179
180
181
182
183
# File 'lib/mxrb/semantic/domain_mutator.rb', line 176

def initialize(project:, module_name:, domain_unit_id:, entity_def:)
  @project        = project
  @module_name    = module_name
  @domain_unit_id = domain_unit_id
  @entity_def     = entity_def
  @applied        = false
  @changes = ["add entity #{module_name}.#{entity_def[:name]}"].freeze
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



174
175
176
# File 'lib/mxrb/semantic/domain_mutator.rb', line 174

def changes
  @changes
end

#entity_defObject (readonly)

Returns the value of attribute entity_def.



174
175
176
# File 'lib/mxrb/semantic/domain_mutator.rb', line 174

def entity_def
  @entity_def
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



174
175
176
# File 'lib/mxrb/semantic/domain_mutator.rb', line 174

def module_name
  @module_name
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


186
# File 'lib/mxrb/semantic/domain_mutator.rb', line 186

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mxrb/semantic/domain_mutator.rb', line 188

def apply!
  raise ArgumentError, "plan was already applied" if @applied

  @project.mpr.transaction do
    raw = @project.raw_unit(@domain_unit_id)
    raise ArgumentError, "domain model unit not found" unless raw

    doc      = @project.parse_bson(raw)
    entities = DomainMutator.extract_entities(doc)
    existing_names = entities.map { (_1["name"] || _1["Name"]).to_s }
    raise ArgumentError, "entity #{@entity_def[:name]} already exists in #{@module_name}" \
      if existing_names.include?(@entity_def[:name].to_s)

    entity                    = Model::Entity.new
    entity.id                 = SecureRandom.uuid
    entity.name               = @entity_def[:name].to_s
    entity.qualified_name     = "#{@module_name}.#{@entity_def[:name]}"
    entity.documentation      = @entity_def.fetch(:documentation, "").to_s
    entity.data_storage_guid  = SecureRandom.uuid
    entity.export_level       = "Hidden"
    entity.persistable        = !@entity_def[:non_persistent]
    entity.location           = { x: (entities.size % 4) * 220, y: (entities.size / 4) * 160 }
    entity.access_rules       = []

    attrs = Array(@entity_def[:attributes]).map.with_index do |attr_def, i|
      a                   = Model::Attribute.new
      a.id                = SecureRandom.uuid
      a.name              = attr_def[:name].to_s
      a.documentation     = attr_def.fetch(:documentation, "").to_s
      a.type              = attr_def.fetch(:type, :string).to_sym
      a.default_value     = attr_def[:default]&.to_s || ""
      a.data_storage_guid = SecureRandom.uuid
      a.export_level      = "Hidden"
      a
    end
    entity.instance_variable_set(:@attributes, attrs)

    entities_key = DomainMutator.entities_key(doc)
    doc[entities_key] = IO::BsonCodec.build_array(entities + [entity.to_bson])
    @project.mpr.update_unit(@domain_unit_id, doc)
  end

  @project.refresh!
  @applied = true
  self
end

#empty?Boolean

Returns:

  • (Boolean)


185
# File 'lib/mxrb/semantic/domain_mutator.rb', line 185

def empty?   = false