Class: Dynamoid::TransactionWrite

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/transaction_write.rb,
lib/dynamoid/transaction_write/action.rb,
lib/dynamoid/transaction_write/create.rb,
lib/dynamoid/transaction_write/delete.rb,
lib/dynamoid/transaction_write/update.rb,
lib/dynamoid/transaction_write/upsert.rb,
lib/dynamoid/transaction_write/destroy.rb,
lib/dynamoid/transaction_write/update_upsert.rb

Defined Under Namespace

Classes: Action, Create, Delete, Destroy, Update, UpdateUpsert, Upsert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_options = {}) ⇒ TransactionWrite

Returns a new instance of TransactionWrite.



15
16
17
18
# File 'lib/dynamoid/transaction_write.rb', line 15

def initialize(_options = {})
  @action_inputs = []
  @models = []
end

Instance Attribute Details

#action_inputsObject

Returns the value of attribute action_inputs.



13
14
15
# File 'lib/dynamoid/transaction_write.rb', line 13

def action_inputs
  @action_inputs
end

#modelsObject

Returns the value of attribute models.



13
14
15
# File 'lib/dynamoid/transaction_write.rb', line 13

def models
  @models
end

Class Method Details

.execute(options = {}) {|transaction| ... } ⇒ Object

Yields:

  • (transaction)


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

def self.execute(options = {})
  transaction = new(options)
  yield(transaction)
  transaction.commit
end

Instance Method Details

#commitObject



26
27
28
29
30
31
# File 'lib/dynamoid/transaction_write.rb', line 26

def commit
  return unless @action_inputs.present? # nothing to commit

  Dynamoid.adapter.transact_write_items(@action_inputs)
  models.each { |model| model.new_record = false }
end

#create(model_or_model_class, attributes = {}, options = {}, &block) ⇒ Object



45
46
47
# File 'lib/dynamoid/transaction_write.rb', line 45

def create(model_or_model_class, attributes = {}, options = {}, &block)
  add_action_and_validate Dynamoid::TransactionWrite::Create.new(model_or_model_class, attributes, options, &block)
end

#create!(model_or_model_class, attributes = {}, options = {}, &block) ⇒ Object



41
42
43
# File 'lib/dynamoid/transaction_write.rb', line 41

def create!(model_or_model_class, attributes = {}, options = {}, &block)
  create(model_or_model_class, attributes, options.reverse_merge(raise_validation_error: true), &block)
end

#delete(model_or_model_class, key_or_attributes = {}, options = {}) ⇒ Object



63
64
65
# File 'lib/dynamoid/transaction_write.rb', line 63

def delete(model_or_model_class, key_or_attributes = {}, options = {})
  add_action_and_validate Dynamoid::TransactionWrite::Delete.new(model_or_model_class, key_or_attributes, options)
end

#destroy(model_or_model_class, key_or_attributes = {}, options = {}) ⇒ Object



71
72
73
# File 'lib/dynamoid/transaction_write.rb', line 71

def destroy(model_or_model_class, key_or_attributes = {}, options = {})
  add_action_and_validate Dynamoid::TransactionWrite::Destroy.new(model_or_model_class, key_or_attributes, options)
end

#destroy!(model_or_model_class, key_or_attributes = {}, options = {}) ⇒ Object



67
68
69
# File 'lib/dynamoid/transaction_write.rb', line 67

def destroy!(model_or_model_class, key_or_attributes = {}, options = {})
  destroy(model_or_model_class, key_or_attributes, options.reverse_merge(raise_validation_error: true))
end

#save(model, options = {}) ⇒ Object



37
38
39
# File 'lib/dynamoid/transaction_write.rb', line 37

def save(model, options = {})
  model.new_record? ? create(model, {}, options) : update(model, {}, options)
end

#save!(model, options = {}) ⇒ Object



33
34
35
# File 'lib/dynamoid/transaction_write.rb', line 33

def save!(model, options = {})
  save(model, options.reverse_merge(raise_validation_error: true))
end

#update(model_or_model_class, attributes = {}, options = {}, &block) ⇒ Object



59
60
61
# File 'lib/dynamoid/transaction_write.rb', line 59

def update(model_or_model_class, attributes = {}, options = {}, &block)
  add_action_and_validate Dynamoid::TransactionWrite::Update.new(model_or_model_class, attributes, options, &block)
end

#update!(model_or_model_class, attributes = {}, options = {}, &block) ⇒ Object



55
56
57
# File 'lib/dynamoid/transaction_write.rb', line 55

def update!(model_or_model_class, attributes = {}, options = {}, &block)
  update(model_or_model_class, attributes, options.reverse_merge(raise_validation_error: true), &block)
end

#upsert(model_or_model_class, attributes = {}, options = {}, &block) ⇒ Object

upsert! does not exist because upserting instances that can raise validation errors is not officially supported



51
52
53
# File 'lib/dynamoid/transaction_write.rb', line 51

def upsert(model_or_model_class, attributes = {}, options = {}, &block)
  add_action_and_validate Dynamoid::TransactionWrite::Upsert.new(model_or_model_class, attributes, options, &block)
end