Class: Dynamoid::Transactions::Mutation::Save

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

Instance Method Summary collapse

Methods inherited from Base

validate_attribute_names!

Constructor Details

#initialize(model, **options) ⇒ Save

Returns a new instance of Save.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dynamoid/transactions/mutation/save.rb', line 11

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)


82
83
84
# File 'lib/dynamoid/transactions/mutation/save.rb', line 82

def aborted?
  @aborted
end

#action_requestsObject



94
95
96
97
98
99
100
101
102
# File 'lib/dynamoid/transactions/mutation/save.rb', line 94

def action_requests
  request = if @was_new_record
              action_request_to_create
            else
              action_request_to_update
            end

  [request]
end

#observable_by_user_resultObject



90
91
92
# File 'lib/dynamoid/transactions/mutation/save.rb', line 90

def observable_by_user_result
  !@aborted
end

#on_commitObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dynamoid/transactions/mutation/save.rb', line 66

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



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
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamoid/transactions/mutation/save.rb', line 23

def on_registration
  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
        validate_primary_key!

        @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

  if @model.class.attributes[:lock_version]
    if @model.lock_version.nil? && @model.new_record?
      @model.lock_version = 1
    end

    if @model.lock_version && !@model.changes[:lock_version]
      @model.lock_version += 1
    end
  end
end

#on_rollbackObject



78
79
80
# File 'lib/dynamoid/transactions/mutation/save.rb', line 78

def on_rollback
  @model.run_callbacks(:rollback)
end

#skipped?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dynamoid/transactions/mutation/save.rb', line 86

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