Module: Aws::Record::ItemOperations

Included in:
Aws::Record
Defined in:
lib/aws-record/record/item_operations.rb

Defined Under Namespace

Modules: ItemOperationsClassMethods

Instance Method Summary collapse

Instance Method Details

#assign_attributes(opts) ⇒ Object

Assigns the attributes provided onto the model.

Examples:

Usage Example

class MyModel
  include Aws::Record
  integer_attr :uuid,   hash_key: true
  string_attr  :name, range_key: true
  integer_attr :age
  float_attr   :height
end

model = MyModel.new(id: 4, name: "John", age: 4, height: 70.5)
model.age    # => 4
model.height # => 70.5
model.save
model.dirty? # => false

model.assign_attributes(age: 5, height: 150.75)
model.age    # => 5
model.height # => 150.75
model.dirty? # => true

Parameters:

  • opts (Hash)


108
109
110
111
112
113
114
115
116
# File 'lib/aws-record/record/item_operations.rb', line 108

def assign_attributes(opts)
  opts.each do |field, new_value|
    field = field.to_sym
    setter = "#{field}="
    raise ArgumentError, "Invalid field: #{field} for model" unless respond_to?(setter)

    public_send(setter, new_value)
  end
end

#delete!(opts = {}) ⇒ Object

Deletes the item instance that matches the key values of this item instance in Amazon DynamoDB. Uses the Aws::DynamoDB::Client#delete_item API.

Parameters:



207
208
209
210
211
212
213
214
# File 'lib/aws-record/record/item_operations.rb', line 207

def delete!(opts = {})
  delete_opts = {
    table_name: self.class.table_name,
    key: key_values
  }
  dynamodb_client.delete_item(opts.merge(delete_opts))
  instance_variable_get('@data').destroyed = true
end

#key_valuesObject

Validates and generates the key values necessary for API operations such as the Aws::DynamoDB::Client#delete_item operation.



219
220
221
222
223
224
225
226
227
# File 'lib/aws-record/record/item_operations.rb', line 219

def key_values
  validate_key_values
  attributes = self.class.attributes
  self.class.keys.values.each_with_object({}) do |attr_name, hash|
    db_name = attributes.storage_name_for(attr_name)
    hash[db_name] = attributes.attribute_for(attr_name)
                              .serialize(@data.raw_value(attr_name))
  end
end

#save(opts = {}) ⇒ Object

Saves this instance of an item to Amazon DynamoDB. If this item is “new” as defined by having new or altered key attributes, will attempt a conditional Aws::DynamoDB::Client#put_item call, which will not overwrite an existing item. If the item only has altered non-key attributes, will perform an Aws::DynamoDB::Client#update_item call. Uses this item instance’s attributes in order to build the request on your behalf.

You can use the :force option to perform a simple put/overwrite without conditional validation or update logic.

Parameters:

Options Hash (opts):

  • :force (Boolean)

    if true, will save as a put operation and overwrite any existing item on the remote end. Otherwise, and by default, will either perform a conditional put or an update call.

Returns:

  • false if the record is invalid as defined by an attempt to call valid? on this item, if that method exists. Otherwise, returns client call return value.



76
77
78
79
80
81
82
# File 'lib/aws-record/record/item_operations.rb', line 76

def save(opts = {})
  if _invalid_record?(opts)
    false
  else
    _perform_save(opts)
  end
end

#save!(opts = {}) ⇒ Object

Saves this instance of an item to Amazon DynamoDB. If this item is “new” as defined by having new or altered key attributes, will attempt a conditional Aws::DynamoDB::Client#put_item call, which will not overwrite an existing item. If the item only has altered non-key attributes, will perform an Aws::DynamoDB::Client#update_item call. Uses this item instance’s attributes in order to build the request on your behalf.

You can use the :force option to perform a simple put/overwrite without conditional validation or update logic.

Parameters:

Options Hash (opts):

  • :force (Boolean)

    if true, will save as a put operation and overwrite any existing item on the remote end. Otherwise, and by default, will either perform a conditional put or an update call.

Raises:



42
43
44
45
46
47
# File 'lib/aws-record/record/item_operations.rb', line 42

def save!(opts = {})
  ret = save(opts)
  raise Errors::ValidationError, 'Validation hook returned false!' unless ret

  ret
end

#save_valuesObject

Validates key values and returns a hash consisting of the parameters to save the record using the Aws::DynamoDB::Client#batch_write_item operation.



233
234
235
# File 'lib/aws-record/record/item_operations.rb', line 233

def save_values
  _build_item_for_save
end

#update(new_params, opts = {}) ⇒ Object

Mass assigns the attributes to the model and then performs a save

You can use the :force option to perform a simple put/overwrite without conditional validation or update logic.

Note that aws-record allows you to change your model’s key values, but this will be interpreted as persisting a new item to your DynamoDB table

Examples:

Usage Example

class MyModel
  include Aws::Record
  integer_attr :uuid,   hash_key: true
  string_attr  :name, range_key: true
  integer_attr :age
  float_attr   :height
end

model = MyModel.new(id: 4, name: "John", age: 4, height: 70.5)
model.age    # => 4
model.height # => 70.5
model.save
model.dirty? # => false

model.update(age: 5, height: 150.75)
model.age    # => 5
model.height # => 150.75
model.dirty? # => false

Parameters:

  • new_params (Hash)

    Contains the new parameters for the model.

  • opts (Hash) (defaults to: {})

    Options to pass through to the Aws::DynamoDB::Client#put_item call or the Aws::DynamoDB::Client#update_item call. :put_item is used when :force is true or when the item is new. :update_item is used when the item is not new.

Options Hash (opts):

  • :force (Boolean)

    if true, will save as a put operation and overwrite any existing item on the remote end. Otherwise, and by default, will either perform a conditional put or an update call.

Returns:

  • false if the record is invalid as defined by an attempt to call valid? on this item, if that method exists. Otherwise, returns client call return value.



164
165
166
167
# File 'lib/aws-record/record/item_operations.rb', line 164

def update(new_params, opts = {})
  assign_attributes(new_params)
  save(opts)
end

#update!(new_params, opts = {}) ⇒ Object

Updates model attributes and validates new values

You can use the :force option to perform a simple put/overwrite without conditional validation or update logic.

Note that aws-record allows you to change your model’s key values, but this will be interpreted as persisting a new item to your DynamoDB table

Parameters:

  • new_params (Hash)

    Contains the new parameters for the model.

  • opts (Hash) (defaults to: {})

    Options to pass through to the Aws::DynamoDB::Client#put_item call or the Aws::DynamoDB::Client#update_item call. :put_item is used when :force is true or when the item is new. :update_item is used when the item is not new.

Options Hash (opts):

  • :force (Boolean)

    if true, will save as a put operation and overwrite any existing item on the remote end. Otherwise, and by default, will either perform a conditional put or an update call.

Returns:

  • The update mode if the update is successful

Raises:



194
195
196
197
# File 'lib/aws-record/record/item_operations.rb', line 194

def update!(new_params, opts = {})
  assign_attributes(new_params)
  save!(opts)
end