Class: Dynamoid::TransactionWrite::UpdateFields::UpdateRequestBuilder
- Inherits:
-
Object
- Object
- Dynamoid::TransactionWrite::UpdateFields::UpdateRequestBuilder
- Defined in:
- lib/dynamoid/transaction_write/update_fields.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_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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 122 def initialize(model_class) @model_class = model_class @attributes_to_set = {} @attributes_to_add = {} @attributes_to_delete = {} @attributes_to_remove = [] @condition_expression = nil end |
Instance Attribute Details
#condition_expression=(value) ⇒ Object (writeonly)
Sets the attribute condition_expression
120 121 122 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 120 def condition_expression=(value) @condition_expression = value end |
#hash_key=(value) ⇒ Object (writeonly)
Sets the attribute hash_key
120 121 122 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 120 def hash_key=(value) @hash_key = value end |
#range_key=(value) ⇒ Object (writeonly)
Sets the attribute range_key
120 121 122 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 120 def range_key=(value) @range_key = value end |
Instance Method Details
#add_value(name, value) ⇒ Object
136 137 138 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 136 def add_value(name, value) @attributes_to_add[name] = value end |
#delete_value(name, value) ⇒ Object
140 141 142 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 140 def delete_value(name, value) @attributes_to_delete[name] = value end |
#remove_attributes(names) ⇒ Object
144 145 146 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 144 def remove_attributes(names) @attributes_to_remove.concat(names) end |
#request ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 148 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 = {} expression_attribute_values = {} 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
132 133 134 |
# File 'lib/dynamoid/transaction_write/update_fields.rb', line 132 def set_attributes(attributes) # rubocop:disable Naming/AccessorMethodName @attributes_to_set.merge!(attributes) end |