88require 'dynamoid/transaction_write/update_fields'
99require 'dynamoid/transaction_write/update_attributes'
1010require 'dynamoid/transaction_write/upsert'
11+ require 'dynamoid/transaction_write/item_updater'
1112
1213module Dynamoid
1314 # The class +TransactionWrite+ provides means to perform multiple modifying
@@ -345,6 +346,52 @@ def upsert(model_class, hash_key, range_key = nil, attributes) # rubocop:disable
345346 # t.update_fields(User, '1', 'Tylor', age: 26)
346347 # end
347348 #
349+ # Updates can also be performed in a block.
350+ #
351+ # Dynamoid::TransactionWrite.execute do |t|
352+ # t.update_fields(User, 1) do |u|
353+ # u.add(article_count: 1)
354+ # u.delete(favorite_colors: 'green')
355+ # u.set(age: 27, last_name: 'Tylor')
356+ # end
357+ # end
358+ #
359+ # Operation +add+ just adds a value for numeric attributes and join
360+ # collections if attribute is a set.
361+ #
362+ # t.update_fields(User, 1) do |u|
363+ # u.add(age: 1, followers_count: 5)
364+ # u.add(hobbies: ['skying', 'climbing'])
365+ # end
366+ #
367+ # Operation +delete+ is applied to collection attribute types and
368+ # substructs one collection from another.
369+ #
370+ # t.update_fields(User, 1) do |u|
371+ # u.delete(hobbies: ['skying'])
372+ # end
373+ #
374+ # Operation +set+ just changes an attribute value:
375+ #
376+ # t.update_fields(User, 1) do |u|
377+ # u.set(age: 21)
378+ # end
379+ #
380+ # Operation +remove+ removes one or more attributes from an item.
381+ #
382+ # t.update_fields(User, 1) do |u|
383+ # u.remove(:age)
384+ # end
385+ #
386+ # All the operations work like +ADD+, +DELETE+, +REMOVE+, and +SET+ actions supported
387+ # by +UpdateExpression+
388+ # {parameter}[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html]
389+ # of +UpdateItem+ operation.
390+ #
391+ # It's atomic operations. So adding or deleting elements in a collection
392+ # or incrementing or decrementing a numeric field is atomic and does not
393+ # interfere with other write requests.
394+ #
348395 # Raises a +Dynamoid::Errors::UnknownAttribute+ exception if any of the
349396 # attributes is not declared in the model class.
350397 #
@@ -353,8 +400,14 @@ def upsert(model_class, hash_key, range_key = nil, attributes) # rubocop:disable
353400 # @param range_key [Scalar value] range key value (optional)
354401 # @param attributes [Hash]
355402 # @return [nil]
356- def update_fields ( model_class , hash_key , range_key = nil , attributes ) # rubocop:disable Style/OptionalArguments
357- action = Dynamoid ::TransactionWrite ::UpdateFields . new ( model_class , hash_key , range_key , attributes )
403+ def update_fields ( model_class , hash_key , range_key = nil , attributes = nil , &block )
404+ # given no attributes, but there may be a block
405+ if range_key . is_a? ( Hash ) && !attributes
406+ attributes = range_key
407+ range_key = nil
408+ end
409+
410+ action = Dynamoid ::TransactionWrite ::UpdateFields . new ( model_class , hash_key , range_key , attributes , &block )
358411 register_action action
359412 end
360413
0 commit comments