Class: Dynamoid::TransactionWrite::UpdateFields
- Defined in:
- lib/dynamoid/transaction_write/update_fields.rb
Instance Method Summary collapse
- #aborted? ⇒ Boolean
- #action_request ⇒ Object
-
#initialize(model_class, hash_key, range_key, attributes) ⇒ UpdateFields
constructor
A new instance of UpdateFields.
- #observable_by_user_result ⇒ Object
- #on_commit ⇒ Object
- #on_registration ⇒ Object
- #on_rollback ⇒ Object
- #skipped? ⇒ Boolean
Constructor Details
#initialize(model_class, hash_key, range_key, attributes) ⇒ UpdateFields
Returns a new instance of UpdateFields.
9 10 11 12 13 14 15 16 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 9 def initialize(model_class, hash_key, range_key, attributes) super() @model_class = model_class @hash_key = hash_key @range_key = range_key @attributes = attributes end |
Instance Method Details
#aborted? ⇒ Boolean
27 28 29 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 27 def aborted? false end |
#action_request ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 39 def action_request # changed attributes to persist changes = @attributes.dup changes = (changes, skip_created_at: true) changes_dumped = Dynamoid::Dumping.dump_attributes(changes, @model_class.attributes) # primary key to look up an item to update key = { @model_class.hash_key => @hash_key } key[@model_class.range_key] = @range_key if @model_class.range_key? # Build UpdateExpression and keep names and values placeholders mapping # in ExpressionAttributeNames and ExpressionAttributeValues. update_expression_statements = [] expression_attribute_names = {} expression_attribute_values = {} changes_dumped.each_with_index do |(name, value), i| name_placeholder = "#_n#{i}" value_placeholder = ":_s#{i}" update_expression_statements << "#{name_placeholder} = #{value_placeholder}" expression_attribute_names[name_placeholder] = name expression_attribute_values[value_placeholder] = value end update_expression = "SET #{update_expression_statements.join(', ')}" # require primary key to exist condition_expression = "attribute_exists(#{@model_class.hash_key})" if @model_class.range_key? condition_expression += " AND attribute_exists(#{@model_class.range_key})" end { update: { key: key, table_name: @model_class.table_name, update_expression: update_expression, expression_attribute_names: expression_attribute_names, expression_attribute_values: expression_attribute_values, condition_expression: condition_expression } } end |
#observable_by_user_result ⇒ Object
35 36 37 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 35 def observable_by_user_result nil end |
#on_commit ⇒ Object
23 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 23 def on_commit; end |
#on_registration ⇒ Object
18 19 20 21 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 18 def on_registration validate_primary_key! Dynamoid::Persistence::UpdateValidations.validate_attributes_exist(@model_class, @attributes) end |
#on_rollback ⇒ Object
25 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 25 def on_rollback; end |
#skipped? ⇒ Boolean
31 32 33 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 31 def skipped? @attributes.empty? end |