Class: Dynamoid::TransactionWrite::Save
- Defined in:
- lib/dynamoid/transaction_write/save.rb
Instance Method Summary collapse
- #aborted? ⇒ Boolean
- #action_request ⇒ Object
-
#initialize(model, **options) ⇒ Save
constructor
A new instance of Save.
- #observable_by_user_result ⇒ Object
- #on_commit ⇒ Object
- #on_registration ⇒ Object
- #on_rollback ⇒ Object
- #skipped? ⇒ Boolean
Constructor Details
#initialize(model, **options) ⇒ Save
Returns a new instance of Save.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/dynamoid/transaction_write/save.rb', line 8 def initialize(model, **) super() @model = model @model_class = model.class = @aborted = false @was_new_record = model.new_record? @valid = nil end |
Instance Method Details
#aborted? ⇒ Boolean
69 70 71 |
# File 'lib/dynamoid/transaction_write/save.rb', line 69 def aborted? @aborted end |
#action_request ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/dynamoid/transaction_write/save.rb', line 81 def action_request if @was_new_record action_request_to_create else action_request_to_update end end |
#observable_by_user_result ⇒ Object
77 78 79 |
# File 'lib/dynamoid/transaction_write/save.rb', line 77 def observable_by_user_result !@aborted end |
#on_commit ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dynamoid/transaction_write/save.rb', line 53 def on_commit return if @aborted @model.changes_applied if @was_new_record @model.new_record = false end @model.run_callbacks(:commit) end |
#on_registration ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dynamoid/transaction_write/save.rb', line 20 def on_registration validate_model! if [:validate] != false && !(@valid = @model.valid?) if [:raise_error] raise Dynamoid::Errors::DocumentNotValid, @model else @aborted = true return end end @aborted = true callback_name = @was_new_record ? :create : :update @model.run_callbacks(:save) do @model.run_callbacks(callback_name) do @model.run_callbacks(:validate) do @aborted = false true end end end if @aborted && [:raise_error] raise Dynamoid::Errors::RecordNotSaved, @model end if @was_new_record && @model.hash_key.nil? @model.hash_key = SecureRandom.uuid end end |
#on_rollback ⇒ Object
65 66 67 |
# File 'lib/dynamoid/transaction_write/save.rb', line 65 def on_rollback @model.run_callbacks(:rollback) end |
#skipped? ⇒ Boolean
73 74 75 |
# File 'lib/dynamoid/transaction_write/save.rb', line 73 def skipped? @model.persisted? && !@model.changed? end |