Class: Lutaml::Store::AttributeUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/store/attribute_updater.rb

Overview

Processes model updates including dot notation for nested attributes

Instance Method Summary collapse

Constructor Details

#initialize(registry, composite_handler) ⇒ AttributeUpdater

Returns a new instance of AttributeUpdater.



7
8
9
10
# File 'lib/lutaml/store/attribute_updater.rb', line 7

def initialize(registry, composite_handler)
  @registry = registry
  @composite_handler = composite_handler
end

Instance Method Details

#update_with_attributes(model, attributes) ⇒ Object

Update model with attribute array



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lutaml/store/attribute_updater.rb', line 13

def update_with_attributes(model, attributes)
  return model if attributes.nil? || attributes.empty?

  updated_model = model.dup

  attributes.each do |attr_update|
    key = attr_update[:key]
    value = attr_update[:value]

    if key.to_s.include?(".")
      update_nested_attribute(updated_model, key.to_s, value)
    else
      update_direct_attribute(updated_model, key, value)
    end
  end

  @composite_handler.update_composite_models(updated_model, attributes)

  updated_model
end

#update_with_block(model, &block) ⇒ Object

Update model with block



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/store/attribute_updater.rb', line 35

def update_with_block(model, &block)
  return model unless block_given?

  updated_model = model.dup
  block.call(updated_model)

  composite_models = @registry.find_composite_models(updated_model)
  @composite_handler.process_composite_models(updated_model) unless composite_models.empty?

  updated_model
end

#update_with_hash(model, updates_hash) ⇒ Object

Update model with hash



48
49
50
51
52
53
# File 'lib/lutaml/store/attribute_updater.rb', line 48

def update_with_hash(model, updates_hash)
  return model if updates_hash.nil? || updates_hash.empty?

  attributes = updates_hash.map { |key, value| { key: key, value: value } }
  update_with_attributes(model, attributes)
end