Module: Dynomite::Item::Write

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynomite/item/write.rb,
lib/dynomite/item/write/base.rb,
lib/dynomite/item/write/put_item.rb,
lib/dynomite/item/write/delete_item.rb,
lib/dynomite/item/write/update_item.rb

Defined Under Namespace

Classes: Base, DeleteItem, PutItem, UpdateItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_touchingObject (readonly)

Returns the value of attribute _touching.



98
99
100
# File 'lib/dynomite/item/write.rb', line 98

def _touching
  @_touching
end

Instance Method Details

#call_update_strategy(options) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/dynomite/item/write.rb', line 46

def call_update_strategy(options)
  if Dynomite.config.update_strategy == :update_item
    # Note: fields assigned directly with brackets are not tracked as changed
    # IE: post[:title] = "test"
    UpdateItem.call(self, options)
  else # default
    PutItem.call(self, options)
  end
end

#delete(options = {}) ⇒ Object



94
95
96
# File 'lib/dynomite/item/write.rb', line 94

def delete(options={})
  DeleteItem.call(self, options)
end

#destroy(options = {}) ⇒ Object



88
89
90
91
92
# File 'lib/dynomite/item/write.rb', line 88

def destroy(options={})
  run_callbacks(:destroy) do
    DeleteItem.call(self, options)
  end
end

#increment(attribute, by = 1) ⇒ Object

Examples:

user.increment(:likes)
user.increment(:likes, 2)


122
123
124
125
126
# File 'lib/dynomite/item/write.rb', line 122

def increment(attribute, by = 1)
  self[attribute] ||= 0
  self[attribute] += by
  self
end

#increment!(attribute, by = 1, touch: nil) ⇒ Object

Increment counter. Validations and callbacks are skipped.

Examples:

user.increment!(:likes)
user.increment!(:likes, 2)
user.increment!(:likes, touch: true)
user.increment!(:likes, touch: :created_at)
user.increment!(:likes, touch: [:viewed_at, :created_at])


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dynomite/item/write.rb', line 138

def increment!(attribute, by = 1, touch: nil)
  increment(attribute, by)

  now = Time.now
  attrs = Array(touch).inject({}) do |attrs, field|
    attrs.merge!(field => now)
  end if touch

  run_callbacks :touch do
    UpdateItem.new(self).save_changes(attrs: attrs, count_changes: { attribute => by })
  end

  self
end

#put(options = {}) ⇒ Object Also known as: replace

When you add an item, the primary key attributes are the only required attributes. docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/DynamoDB/Client.html#put_item-instance_method



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dynomite/item/write.rb', line 64

def put(options={})
  found_primary_keys = self.attrs.keys.map(&:to_s) & primary_key_fields
  unless primary_key_fields.sort == found_primary_keys
    raise Dynomite::Error::InvalidPut.new("Invalid put. The primary key fields #{primary_key_fields} must be present in the attrs #{attrs}")
  end

  options.reverse_merge!(validate: true)
  return self if options[:validate] && !valid? # return self so can grab errors in invalid. save does the same thing

  # Run callbacks for put so id is also set
  run_callbacks(:save) do
    run_callbacks(:update) do
      PutItem.call(self, options.merge(put: true))
    end
  end
end

#put!(options = {}) ⇒ Object Also known as: replace!



82
83
84
85
# File 'lib/dynomite/item/write.rb', line 82

def put!(options={})
  raise_error_if_invalid
  put(options)
end

#raise_error_if_invalidObject



169
170
171
# File 'lib/dynomite/item/write.rb', line 169

def raise_error_if_invalid
  raise Dynomite::Error::Validation, "Validation failed: #{errors.full_messages.join(', ')}" unless valid?
end

#save(options = {}) ⇒ Object

Not using method_missing to allow usage of dot notation and assign DynamoDB attrs can go many levels deep so it makes less make sense to use to dot notation.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dynomite/item/write.rb', line 10

def save(options={})
  options.reverse_merge!(validate: true)
  return self if options[:validate] && !valid?

  action = new_record? ? :create : :update
  run_callbacks(:save) do
    run_callbacks(action) do
      if action == :create
        PutItem.call(self, options)
      else # :update
        call_update_strategy(options)
      end
    end
  end
end

#save!(options = {}) ⇒ Object

Similar to save, but raises an error on failed validation.



27
28
29
30
# File 'lib/dynomite/item/write.rb', line 27

def save!(options={})
  raise_error_if_invalid
  save(options)
end

#toggle(attribute) ⇒ Object

Example:

user = User.first
user.banned? # => false
user.toggle(:banned)
user.banned? # => true


160
161
162
163
# File 'lib/dynomite/item/write.rb', line 160

def toggle(attribute)
  self[attribute] = !public_send("#{attribute}?")
  self
end

#toggle!(attribute) ⇒ Object



165
166
167
# File 'lib/dynomite/item/write.rb', line 165

def toggle!(attribute)
  toggle(attribute).update_attribute(attribute, self[attribute])
end

#touch(*names, **options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dynomite/item/write.rb', line 99

def touch(*names, **options)
  if new_record?
    raise Dynomite::Error, 'cannot touch on a new item'
  end

  time_to_assign = options.delete(:time) || Time.now

  self.updated_at = time_to_assign
  names.each do |name|
    attrs.send("#{name}=", time_to_assign)
  end

  @_touching = true
  run_callbacks :touch do
    UpdateItem.call(self, options)
  end

  self
end

#update(attrs = {}, options = {}) ⇒ Object

post.update(title: “test”, body: “body”) post.update(“test”, body: “body”, false)



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dynomite/item/write.rb', line 34

def update(attrs={}, options={})
  self.attrs.merge!(attrs)
  options.reverse_merge!(validate: true)
  return false if options[:validate] && !valid?

  run_callbacks(:save) do
    run_callbacks(:update) do
      call_update_strategy(options)
    end
  end
end

#update!(attrs = {}, options = {}) ⇒ Object

Similar to update, but raises an error on failed validation.



57
58
59
60
# File 'lib/dynomite/item/write.rb', line 57

def update!(attrs={}, options={})
  raise_error_if_invalid
  update(attrs, options)
end