Module: Aws::Record::ItemOperations
- Included in:
- Aws::Record
- Defined in:
- lib/aws-record/record/item_operations.rb
Defined Under Namespace
Modules: ItemOperationsClassMethods
Class Method Summary collapse
- .included(sub_class) ⇒ Object private
Instance Method Summary collapse
-
#assign_attributes(opts) ⇒ Object
Assigns the attributes provided onto the model.
-
#delete! ⇒ Object
Deletes the item instance that matches the key values of this item instance in Amazon DynamoDB.
-
#key_values ⇒ Object
Validates and generates the key values necessary for API operations such as the Aws::DynamoDB::Client#delete_item operation.
-
#save(opts = {}) ⇒ Object
Saves this instance of an item to Amazon DynamoDB.
-
#save!(opts = {}) ⇒ Object
Saves this instance of an item to Amazon DynamoDB.
-
#save_values ⇒ Object
Validates key values and returns a hash consisting of the parameters to save the record using the Aws::DynamoDB::Client#batch_write_item operation.
-
#update(new_params, opts = {}) ⇒ Object
Mass assigns the attributes to the model and then performs a save.
-
#update!(new_params, opts = {}) ⇒ Object
Updates model attributes and validates new values.
Class Method Details
.included(sub_class) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/aws-record/record/item_operations.rb', line 8 def self.included(sub_class) sub_class.extend(ItemOperationsClassMethods) end |
Instance Method Details
#assign_attributes(opts) ⇒ Object
Assigns the attributes provided onto the model.
98 99 100 101 102 103 104 105 |
# File 'lib/aws-record/record/item_operations.rb', line 98 def assign_attributes(opts) opts.each do |field, new_value| field = field.to_sym setter = "#{field}=" raise ArgumentError.new "Invalid field: #{field} for model" unless respond_to?(setter) public_send(setter, new_value) end end |
#delete! ⇒ 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.
179 180 181 182 183 184 185 |
# File 'lib/aws-record/record/item_operations.rb', line 179 def delete! dynamodb_client.delete_item( table_name: self.class.table_name, key: key_values ) self.instance_variable_get("@data").destroyed = true end |
#key_values ⇒ Object
Validates and generates the key values necessary for API operations such as the Aws::DynamoDB::Client#delete_item operation.
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/aws-record/record/item_operations.rb', line 190 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.
65 66 67 68 69 70 71 |
# File 'lib/aws-record/record/item_operations.rb', line 65 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.
36 37 38 39 40 41 42 43 |
# File 'lib/aws-record/record/item_operations.rb', line 36 def save!(opts = {}) ret = save(opts) if ret ret else raise Errors::ValidationError.new("Validation hook returned false!") end end |
#save_values ⇒ Object
Validates key values and returns a hash consisting of the parameters to save the record using the Aws::DynamoDB::Client#batch_write_item operation.
205 206 207 |
# File 'lib/aws-record/record/item_operations.rb', line 205 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
146 147 148 149 |
# File 'lib/aws-record/record/item_operations.rb', line 146 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
170 171 172 173 |
# File 'lib/aws-record/record/item_operations.rb', line 170 def update!(new_params, opts = {}) assign_attributes(new_params) save!(opts) end |