Class: Dynomite::Item::Write::UpdateItem
- Defined in:
- lib/dynomite/item/write/update_item.rb
Instance Method Summary collapse
-
#call ⇒ Object
Note: fields assigned directly with brackets are not tracked as changed IE: post = “test”.
- #expression_attribute_names ⇒ Object
- #expression_attribute_values ⇒ Object
-
#initialize(model, options = {}) ⇒ UpdateItem
constructor
A new instance of UpdateItem.
- #params ⇒ Object (also: #to_params)
-
#save_changes(changes = {}) ⇒ Object
Allows updates to specific attributes and counters.
- #update_expression ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(model, options = {}) ⇒ UpdateItem
Returns a new instance of UpdateItem.
3 4 5 6 7 |
# File 'lib/dynomite/item/write/update_item.rb', line 3 def initialize(model, ={}) super @attrs = {} @count_changes = {} end |
Instance Method Details
#call ⇒ Object
Note: fields assigned directly with brackets are not tracked as changed IE: post = “test”
11 12 13 14 15 16 17 |
# File 'lib/dynomite/item/write/update_item.rb', line 11 def call changed_fields = @model.changed_attributes.keys return if changed_fields.empty? # no changes to save @attrs = @model.attrs.slice(*changed_fields) log_debug(params) client.update_item(params) end |
#expression_attribute_names ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/dynomite/item/write/update_item.rb', line 38 def expression_attribute_names attr_names = @attrs.inject({}) do |names, (name,_)| names.merge!("##{name}" => name) end count_names = @count_changes.inject({}) do |names, (name,_)| names.merge!("##{name}" => name) end attr_names.merge(count_names) end |
#expression_attribute_values ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dynomite/item/write/update_item.rb', line 48 def expression_attribute_values typecaster = Dynomite::Item::Typecaster.new(@model) attr_values = @attrs.inject({}) do |values, (name,value)| = @model.class.[name.to_sym] # can be nil if field is not defined type = ? [:type] : :infer value = typecaster.cast_to_type(type, value, on: :write) values.merge!(":#{name}" => value) end count_values = @count_changes.inject({}) do |values, (name,value)| values.merge!(":#{name}" => value) end attr_values.merge(count_values) end |
#params ⇒ Object Also known as: to_params
27 28 29 30 31 32 33 34 35 |
# File 'lib/dynomite/item/write/update_item.rb', line 27 def params { expression_attribute_names: expression_attribute_names, # { "##{attribute}" => attribute }, expression_attribute_values: expression_attribute_values, # { ':attribute' => value } or { ':by' => by } update_expression: update_expression, # "SET ##{attribute} = ##{attribute}" or "SET ##{attribute} = ##{attribute} + :by" key: @model.primary_key, table_name: @model.class.table_name } end |
#save_changes(changes = {}) ⇒ Object
Allows updates to specific attributes and counters
20 21 22 23 24 25 |
# File 'lib/dynomite/item/write/update_item.rb', line 20 def save_changes(changes={}) @attrs = changes[:attrs] || {} @count_changes = changes[:count_changes] || {} log_debug(params) client.update_item(params) end |
#update_expression ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dynomite/item/write/update_item.rb', line 62 def update_expression expressions = [] @attrs.inject([]) do |exp, (name,_)| expressions << "##{name} = :#{name}" end @count_changes.inject([]) do |exp, (name,_)| expressions << "##{name} = ##{name} + :#{name}" end "SET " + expressions.join(', ') end |