Class: Mxrb::Semantic::ChangeAttributePlan

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

Overview

Plan that changes an attribute's type, default value, or documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:, attribute_name:, updates:) ⇒ ChangeAttributePlan

Returns a new instance of ChangeAttributePlan.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mxrb/semantic/domain_mutator.rb', line 122

def initialize(project:, module_name:, entity_name:, entity_id:, domain_unit_id:,
               attribute_name:, updates:)
  @project        = project
  @module_name    = module_name
  @entity_name    = entity_name
  @entity_id      = entity_id
  @domain_unit_id = domain_unit_id
  @attribute_name = attribute_name
  @updates        = updates.freeze
  @applied        = false
  desc = updates.map { |k, v| "#{k}: #{v}" }.join(", ")
  @changes = ["change #{module_name}.#{entity_name}/#{attribute_name} (#{desc})"].freeze
end

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



120
121
122
# File 'lib/mxrb/semantic/domain_mutator.rb', line 120

def attribute_name
  @attribute_name
end

#changesObject (readonly)

Returns the value of attribute changes.



120
121
122
# File 'lib/mxrb/semantic/domain_mutator.rb', line 120

def changes
  @changes
end

#updatesObject (readonly)

Returns the value of attribute updates.



120
121
122
# File 'lib/mxrb/semantic/domain_mutator.rb', line 120

def updates
  @updates
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


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

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/mxrb/semantic/domain_mutator.rb', line 139

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 not found" unless entity_doc

    attrs    = DomainMutator.extract_attributes(entity_doc)
    attr_doc = attrs.find { (_1["name"] || _1["Name"]).to_s == @attribute_name }
    raise ArgumentError, "attribute #{@attribute_name.inspect} not found on #{@entity_name}" unless attr_doc

    attr_model                 = Model::Attribute.from_bson(attr_doc)
    attr_model.type            = @updates[:type].to_sym       if @updates.key?(:type)
    attr_model.default_value   = @updates[:default].to_s      if @updates.key?(:default)
    attr_model.documentation   = @updates[:documentation].to_s if @updates.key?(:documentation)

    new_attrs = attrs.map { (_1["name"] || _1["Name"]).to_s == @attribute_name ? attr_model.to_bson : _1 }
    DomainMutator.put_attributes(entity_doc, new_attrs)
    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)


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

def empty?   = false