Class: Dynamoid::Persistence::Upsert

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, partition_key:, sort_key:, attributes:, conditions:) ⇒ Upsert

Returns a new instance of Upsert.



11
12
13
14
15
16
17
# File 'lib/dynamoid/persistence/upsert.rb', line 11

def initialize(model_class, partition_key:, sort_key:, attributes:, conditions:)
  @model_class = model_class
  @partition_key = partition_key
  @sort_key = sort_key
  @attributes = attributes.symbolize_keys
  @conditions = conditions
end

Class Method Details

.call(*args, **options) ⇒ Object



7
8
9
# File 'lib/dynamoid/persistence/upsert.rb', line 7

def self.call(*args, **options)
  new(*args, **options).call
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dynamoid/persistence/upsert.rb', line 19

def call
  UpdateValidations.validate_attributes_exist(@model_class, @attributes)

  if @model_class.timestamps_enabled?
    @attributes[:updated_at] ||= DateTime.now.in_time_zone(Time.zone)
  end

  raw_attributes = update_item
  @model_class.new(undump_attributes(raw_attributes))
rescue Dynamoid::Errors::ConditionalCheckFailedException
end