Class: Mxrb::Semantic::RemoveAttributePlan

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

Overview

Plan that removes an attribute from an entity, with incoming reference check.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RemoveAttributePlan.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mxrb/semantic/domain_mutator.rb', line 67

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

Instance Attribute Details

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



65
66
67
# File 'lib/mxrb/semantic/domain_mutator.rb', line 65

def attribute_name
  @attribute_name
end

#changesObject (readonly)

Returns the value of attribute changes.



65
66
67
# File 'lib/mxrb/semantic/domain_mutator.rb', line 65

def changes
  @changes
end

#incomingObject (readonly)

Returns the value of attribute incoming.



65
66
67
# File 'lib/mxrb/semantic/domain_mutator.rb', line 65

def incoming
  @incoming
end

Instance Method Details

#applied?Boolean

Returns:

  • (Boolean)


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

def applied? = @applied

#apply!Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mxrb/semantic/domain_mutator.rb', line 84

def apply!
  raise ArgumentError, "plan was already applied" if @applied
  unless safe?
    raise ArgumentError,
          "cannot remove #{@module_name}.#{@entity_name}/#{@attribute_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)
    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)
    before_size = attrs.size
    attrs.reject! { (_1["name"] || _1["Name"]).to_s == @attribute_name }
    raise ArgumentError, "attribute #{@attribute_name.inspect} not found on #{@entity_name}" \
      if attrs.size == before_size

    DomainMutator.put_attributes(entity_doc, 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)


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

def empty?   = false

#safe?Boolean

Returns:

  • (Boolean)


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

def safe?    = @incoming.empty?