Class: Dynamoid::TransactionWrite::Upsert

Inherits:
UpdateUpsert show all
Defined in:
lib/dynamoid/transaction_write/upsert.rb

Constant Summary

Constants inherited from Action

Action::VALID_OPTIONS

Instance Attribute Summary

Attributes inherited from Action

#additions, #attributes, #deletions, #model, #options, #removals

Instance Method Summary collapse

Methods inherited from UpdateUpsert

#to_h

Methods inherited from Action

#add, #add_timestamps, #changes_applied, #delete, #find_from_attributes, #hash_key, #model_class, #raise_validation_error?, #range_key, #run_callbacks, #sanitize_item, #set, #skip_callbacks?, #skip_validation?, #to_h, #touch_model_timestamps, #write_attributes_to_model

Constructor Details

#initialize(model_or_model_class, attributes, options = {}) ⇒ Upsert

Upsert is just like Update buts skips the existence check so a new record can be created when it’s missing. No callbacks.



10
11
12
13
14
15
# File 'lib/dynamoid/transaction_write/upsert.rb', line 10

def initialize(model_or_model_class, attributes, options = {})
  options = options.reverse_merge(skip_existence_check: true)
  super(model_or_model_class, attributes, options)
  raise Dynamoid::Errors::MissingHashKey unless hash_key.present?
  raise Dynamoid::Errors::MissingRangeKey unless !model_class.range_key? || range_key.present?
end

Instance Method Details

#valid?Boolean

only run validations if a model has been provided

Returns:

  • (Boolean)


20
21
22
# File 'lib/dynamoid/transaction_write/upsert.rb', line 20

def valid?
  model ? model.valid? : true
end