Class: Dynomite::Item::Write::UpdateItem

Inherits:
Base
  • Object
show all
Defined in:
lib/dynomite/item/write/update_item.rb

Instance Method Summary collapse

Methods inherited from Base

call

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, options={})
  super
  @attrs = {}
  @count_changes = {}
end

Instance Method Details

#callObject

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_namesObject



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_valuesObject



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)|
    meta = @model.class.fields_meta[name.to_sym] # can be nil if field is not defined
    type = meta ? meta[: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

#paramsObject 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_expressionObject



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