Class: Dynamoid::Persistence::Save

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/persistence/save.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Save

Returns a new instance of Save.



10
11
12
# File 'lib/dynamoid/persistence/save.rb', line 10

def initialize(model)
  @model = model
end

Class Method Details

.call(model) ⇒ Object



6
7
8
# File 'lib/dynamoid/persistence/save.rb', line 6

def self.call(model)
  new(model).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dynamoid/persistence/save.rb', line 14

def call
  @model.hash_key = SecureRandom.uuid if @model.hash_key.blank?

  # Add an optimistic locking check if the lock_version column exists
  if @model.class.attributes[:lock_version]
    @model.lock_version = (@model.lock_version || 0) + 1
  end

  attributes_dumped = Dumping.dump_attributes(@model.attributes, @model.class.attributes)
  Dynamoid.adapter.write(@model.class.table_name, attributes_dumped, conditions_for_write)

  @model.new_record = false
  true
rescue Dynamoid::Errors::ConditionalCheckFailedException => e
  if @model.new_record?
    raise Dynamoid::Errors::RecordNotUnique.new(e, @model)
  else
    raise Dynamoid::Errors::StaleObjectError.new(@model, 'persist')
  end
end