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.



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

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)


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

def aborted?
  @aborted
end

#action_requests ⇒ Object



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

def action_requests
  request = if @was_new_record
              action_request_to_create
            else
              action_request_to_update
            end

  [request]
end

#observable_by_user_result ⇒ Object



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

def observable_by_user_result
  !@aborted
end

#on_commit ⇒ Object



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

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



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
65
# File 'lib/dynamoid/transactions/mutation/save.rb', line 24

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
        if @was_new_record && @model.hash_key.nil? && hash_key_string?
          @model.hash_key = SecureRandom.uuid
        end

        validate_primary_key!

        @aborted = false
        true
      end
    end
  end

  if @aborted && @options[:raise_error]
    raise Dynamoid::Errors::RecordNotSaved, @model
  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_rollback ⇒ Object



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

def on_rollback
  @model.run_callbacks(:rollback)
end

#skipped? ⇒ Boolean

Returns:

  • (Boolean)


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

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