Class: Dynamoid::Transactions::Mutation::Touch

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

Instance Method Summary collapse

Methods inherited from Base

validate_attribute_names!

Constructor Details

#initialize(model, *names, time: nil) ⇒ Touch

Returns a new instance of Touch.



11
12
13
14
15
16
17
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 11

def initialize(model, *names, time: nil)
  super()

  @model = model
  @names = names
  @time = time
end

Instance Method Details

#aborted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 47

def aborted?
  false
end

#action_requestsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 59

def action_requests
  builder = Builders::UpdateRequestBuilder.new(@model.class)
  builder.hash_key = dump(@model.class.hash_key, @model.hash_key)
  builder.range_key = dump(@model.class.range_key, @model.range_value) if @model.class.range_key?

  # require primary key to exist
  builder.add_expression_attribute_name('#_h', @model.class.hash_key)
  condition_expression = 'attribute_exists(#_h)'

  if @model.class.range_key?
    builder.add_expression_attribute_name('#_r', @model.class.range_key)
    condition_expression += ' AND attribute_exists(#_r)'
  end
  builder.condition_expression = condition_expression

  attributes_to_set = {}
  attributes_to_set[:updated_at] = dump(:updated_at, @model[:updated_at]) if @model.class.timestamps_enabled?

  @names.each do |name|
    attributes_to_set[name] = dump(name, @model[name])
  end

  builder.set_attributes(attributes_to_set)
  [builder.request]
end

#observable_by_user_resultObject



55
56
57
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 55

def observable_by_user_result
  @model
end

#on_commitObject



36
37
38
39
40
41
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 36

def on_commit
  attribute_names = @names.map(&:to_s)
  attribute_names << 'updated_at' if @model.class.timestamps_enabled?
  @model.clear_attribute_changes(attribute_names)
  @model.run_callbacks(:commit)
end

#on_registrationObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 19

def on_registration
  if @model.new_record?
    raise Dynamoid::Errors::Error, 'cannot touch on a new or destroyed record object'
  end

  validate_primary_key!

  @model.run_callbacks(:touch) do
    @time_to_assign = @time || DateTime.now.in_time_zone(Time.zone)

    @model.updated_at = @time_to_assign if @model.class.timestamps_enabled?
    @names.each do |name|
      @model.write_attribute(name, @time_to_assign)
    end
  end
end

#on_rollbackObject



43
44
45
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 43

def on_rollback
  @model.run_callbacks(:rollback)
end

#skipped?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/dynamoid/transactions/mutation/touch.rb', line 51

def skipped?
  !@model.class.timestamps_enabled? && @names.empty?
end