Class: Dynamoid::Transactions::Mutation::Builders::UpdateRequestBuilder
- Inherits:
-
Object
- Object
- Dynamoid::Transactions::Mutation::Builders::UpdateRequestBuilder
- Defined in:
- lib/dynamoid/transactions/mutation/builders/update_request_builder.rb
Instance Attribute Summary collapse
-
#condition_expression ⇒ Object
writeonly
Sets the attribute condition_expression.
-
#hash_key ⇒ Object
writeonly
Sets the attribute hash_key.
-
#range_key ⇒ Object
writeonly
Sets the attribute range_key.
Instance Method Summary collapse
- #add_expression_attribute_name(placeholder, name) ⇒ Object
- #add_expression_attribute_value(placeholder, value) ⇒ Object
- #add_value(name, value) ⇒ Object
- #delete_value(name, value) ⇒ Object
-
#initialize(model_class) ⇒ UpdateRequestBuilder
constructor
A new instance of UpdateRequestBuilder.
- #remove_attributes(names) ⇒ Object
- #request ⇒ Object
-
#set_attributes(attributes) ⇒ Object
rubocop:disable Naming/AccessorMethodName.
Constructor Details
#initialize(model_class) ⇒ UpdateRequestBuilder
Returns a new instance of UpdateRequestBuilder.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 11 def initialize(model_class) @model_class = model_class @attributes_to_set = {} @attributes_to_add = {} @attributes_to_delete = {} @attributes_to_remove = [] @condition_expression = nil @extra_attribute_names = {} @extra_attribute_values = {} end |
Instance Attribute Details
#condition_expression=(value) ⇒ Object (writeonly)
Sets the attribute condition_expression
9 10 11 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 9 def condition_expression=(value) @condition_expression = value end |
#hash_key=(value) ⇒ Object (writeonly)
Sets the attribute hash_key
9 10 11 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 9 def hash_key=(value) @hash_key = value end |
#range_key=(value) ⇒ Object (writeonly)
Sets the attribute range_key
9 10 11 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 9 def range_key=(value) @range_key = value end |
Instance Method Details
#add_expression_attribute_name(placeholder, name) ⇒ Object
23 24 25 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 23 def add_expression_attribute_name(placeholder, name) @extra_attribute_names[placeholder] = name end |
#add_expression_attribute_value(placeholder, value) ⇒ Object
27 28 29 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 27 def add_expression_attribute_value(placeholder, value) @extra_attribute_values[placeholder] = value end |
#add_value(name, value) ⇒ Object
35 36 37 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 35 def add_value(name, value) @attributes_to_add[name] = value end |
#delete_value(name, value) ⇒ Object
39 40 41 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 39 def delete_value(name, value) @attributes_to_delete[name] = value end |
#remove_attributes(names) ⇒ Object
43 44 45 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 43 def remove_attributes(names) @attributes_to_remove.concat(names) end |
#request ⇒ Object
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 47 def request 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 = @extra_attribute_names.dup expression_attribute_values = @extra_attribute_values.dup name_placeholder = '#_n0' value_placeholder = ':_v0' unless @attributes_to_set.empty? statements = [] @attributes_to_set.each do |name, value| statements << "#{name_placeholder} = #{value_placeholder}" expression_attribute_names[name_placeholder] = name expression_attribute_values[value_placeholder] = value name_placeholder = name_placeholder.succ value_placeholder = value_placeholder.succ end update_expression_statements << "SET #{statements.join(', ')}" end unless @attributes_to_add.empty? statements = [] @attributes_to_add.each do |name, value| statements << "#{name_placeholder} #{value_placeholder}" expression_attribute_names[name_placeholder] = name expression_attribute_values[value_placeholder] = value name_placeholder = name_placeholder.succ value_placeholder = value_placeholder.succ end update_expression_statements << "ADD #{statements.join(', ')}" end unless @attributes_to_delete.empty? statements = [] @attributes_to_delete.each do |name, value| statements << "#{name_placeholder} #{value_placeholder}" expression_attribute_names[name_placeholder] = name expression_attribute_values[value_placeholder] = value name_placeholder = name_placeholder.succ value_placeholder = value_placeholder.succ end update_expression_statements << "DELETE #{statements.join(', ')}" end unless @attributes_to_remove.empty? name_placeholders = [] @attributes_to_remove.each do |name| name_placeholders << name_placeholder expression_attribute_names[name_placeholder] = name name_placeholder = name_placeholder.succ value_placeholder = value_placeholder.succ end update_expression_statements << "REMOVE #{name_placeholders.join(', ')}" end update_expression = update_expression_statements.join(' ') { 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 |
#set_attributes(attributes) ⇒ Object
rubocop:disable Naming/AccessorMethodName
31 32 33 |
# File 'lib/dynamoid/transactions/mutation/builders/update_request_builder.rb', line 31 def set_attributes(attributes) # rubocop:disable Naming/AccessorMethodName @attributes_to_set.merge!(attributes) end |