Class: Mxrb::Semantic::RemoveEntityPlan

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

Overview

Plan that removes an entity (and all its embedded attributes) from the domain model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:, incoming:) ⇒ RemoveEntityPlan

Returns a new instance of RemoveEntityPlan.



240
241
242
243
244
245
246
247
248
249
# File 'lib/mxrb/semantic/domain_mutator.rb', line 240

def initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:, incoming:)
  @project        = project
  @module_name    = module_name
  @entity_name    = entity_name
  @entity_id      = entity_id
  @domain_unit_id = domain_unit_id
  @incoming       = incoming.freeze
  @applied        = false
  @changes = ["remove entity #{module_name}.#{entity_name}"].freeze
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



238
239
240
# File 'lib/mxrb/semantic/domain_mutator.rb', line 238

def changes
  @changes
end

#entity_nameObject (readonly)

Returns the value of attribute entity_name.



238
239
240
# File 'lib/mxrb/semantic/domain_mutator.rb', line 238

def entity_name
  @entity_name
end

#incomingObject (readonly)

Returns the value of attribute incoming.



238
239
240
# File 'lib/mxrb/semantic/domain_mutator.rb', line 238

def incoming
  @incoming
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


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

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/mxrb/semantic/domain_mutator.rb', line 255

def apply!
  raise ArgumentError, "plan was already applied" if @applied
  unless safe?
    raise ArgumentError,
          "cannot remove #{@module_name}.#{@entity_name}: " \
          "#{@incoming.size} incoming reference(s)"
  end

  @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)
    before_size = entities.size
    entities.reject! { IO::BsonCodec.extract_id(_1["$ID"] || _1["\$ID"]) == @entity_id }
    raise ArgumentError, "entity #{@entity_name.inspect} not found" if entities.size == before_size

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

  @project.refresh!
  @applied = true
  self
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?   = false

#safe?Boolean

Returns:

  • (Boolean)


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

def safe?    = @incoming.empty?