Class: Dynamoid::TransactionWrite::Save

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

Instance Method Summary collapse

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, **options)
  super()

  @model = model
  @model_class = model.class
  @options = options

  @aborted = false
  @was_new_record = model.new_record?
  @valid = nil
end

Instance Method Details

#aborted?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/dynamoid/transaction_write/save.rb', line 69

def aborted?
  @aborted
end

#action_requestObject



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_resultObject



77
78
79
# File 'lib/dynamoid/transaction_write/save.rb', line 77

def observable_by_user_result
  !@aborted
end

#on_commitObject



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_registrationObject



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 @options[:validate] != false && !(@valid = @model.valid?)
    if @options[: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 && @options[:raise_error]
    raise Dynamoid::Errors::RecordNotSaved, @model
  end

  if @was_new_record && @model.hash_key.nil?
    @model.hash_key = SecureRandom.uuid
  end
end

#on_rollbackObject



65
66
67
# File 'lib/dynamoid/transaction_write/save.rb', line 65

def on_rollback
  @model.run_callbacks(:rollback)
end

#skipped?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/dynamoid/transaction_write/save.rb', line 73

def skipped?
  @model.persisted? && !@model.changed?
end