Class: Mxrb::Semantic::AddAttributePlan

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

Overview

Plan that adds a new attribute to an existing entity.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:, attribute_def:) ⇒ AddAttributePlan

Returns a new instance of AddAttributePlan.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mxrb/semantic/domain_mutator.rb', line 11

def initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:, attribute_def:)
  @project        = project
  @module_name    = module_name
  @entity_name    = entity_name
  @entity_id      = entity_id
  @domain_unit_id = domain_unit_id
  @attribute_def  = attribute_def
  @applied        = false
  @changes = ["add #{attribute_def[:name]} (#{attribute_def[:type]}) to " \
              "#{module_name}.#{entity_name}"].freeze
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



9
10
11
# File 'lib/mxrb/semantic/domain_mutator.rb', line 9

def changes
  @changes
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


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

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


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
55
56
57
58
59
60
# File 'lib/mxrb/semantic/domain_mutator.rb', line 26

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)
    entity_doc = entities.find { IO::BsonCodec.extract_id(_1["$ID"] || _1["\$ID"]) == @entity_id }
    raise ArgumentError, "entity #{@entity_name.inspect} not found in domain model" unless entity_doc

    attrs = DomainMutator.extract_attributes(entity_doc)
    existing = attrs.map { (_1["name"] || _1["Name"]).to_s }
    raise ArgumentError, "attribute #{@attribute_def[:name]} already exists on #{@entity_name}" \
      if existing.include?(@attribute_def[:name].to_s)

    new_attr                 = Model::Attribute.new
    new_attr.id              = SecureRandom.uuid
    new_attr.name            = @attribute_def[:name].to_s
    new_attr.documentation   = @attribute_def.fetch(:documentation, "").to_s
    new_attr.type            = @attribute_def.fetch(:type, :string).to_sym
    new_attr.default_value   = @attribute_def[:default]&.to_s || ""
    new_attr.data_storage_guid = SecureRandom.uuid
    new_attr.export_level    = "Hidden"

    DomainMutator.put_attributes(entity_doc, attrs + [new_attr.to_bson])
    DomainMutator.put_entity(doc, @entity_id, entity_doc)
    @project.mpr.update_unit(@domain_unit_id, doc)
  end

  @project.refresh!
  @applied = true
  self
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?   = false