Class: Dynamoid::Transactions::Mutation::UpdateFields
- Defined in:
- lib/dynamoid/transactions/mutation/update_fields.rb
Defined Under Namespace
Classes: ItemUpdater
Instance Method Summary collapse
- #aborted? ⇒ Boolean
- #action_requests ⇒ Object
-
#initialize(model_class, hash_key, range_key, attributes, &block) ⇒ UpdateFields
constructor
A new instance of UpdateFields.
- #observable_by_user_result ⇒ Object
- #on_commit ⇒ Object
- #on_registration ⇒ Object
- #on_rollback ⇒ Object
- #skipped? ⇒ Boolean
Methods inherited from Base
Constructor Details
#initialize(model_class, hash_key, range_key, attributes, &block) ⇒ UpdateFields
Returns a new instance of UpdateFields.
62 63 64 65 66 67 68 69 70 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 62 def initialize(model_class, hash_key, range_key, attributes, &block) super() @model_class = model_class @hash_key = hash_key @range_key = range_key @attributes = attributes || {} @block = block end |
Instance Method Details
#aborted? ⇒ Boolean
86 87 88 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 86 def aborted? false end |
#action_requests ⇒ Object
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 98 def action_requests builder = Builders::UpdateRequestBuilder.new(@model_class) # primary key to look up an item to update builder.hash_key = cast_and_dump(@model_class.hash_key, @hash_key) builder.range_key = cast_and_dump(@model_class.range_key, @range_key) if @model_class.range_key? # require primary key to exist builder.add_expression_attribute_name('#_h', @model_class.hash_key) condition_expression = 'attribute_exists(#_h)' if @model_class.range_key? builder.add_expression_attribute_name('#_r', @model_class.range_key) condition_expression += ' AND attribute_exists(#_r)' end builder.condition_expression = condition_expression # changed attributes to persist changes = (@attributes, skip_created_at: true) changes_dumped = Dynamoid::Dumping.dump_attributes(changes, @model_class.attributes) if Dynamoid.config.store_attribute_with_nil_value builder.set_attributes(changes_dumped) else nil_attributes = changes_dumped.select { |_, v| v.nil? } non_nil_attributes = changes_dumped.reject { |_, v| v.nil? } # rubocop:disable Style/PartitionInsteadOfDoubleSelect builder.remove_attributes(nil_attributes.keys) builder.set_attributes(non_nil_attributes) end # given a block if @item_updater builder.set_attributes(@item_updater.attributes_to_set) builder.remove_attributes(@item_updater.attributes_to_remove) @item_updater.attributes_to_add.each do |name, value| # The ADD section in UpdateExpressions requires values to be a # set to update a set attribute. # Allow specifying values as any Enumerable collection (e.g. Array). # Allow a single value not wrapped into a Set if @model_class.attributes[name][:type] == :set value = value.is_a?(Enumerable) ? Set.new(value) : Set[value] end builder.add_value(name, value) end @item_updater.attributes_to_delete.each do |name, value| # The DELETE section in UpdateExpressions requires values to be a # set to update a set attribute. # Allow specifying values as any Enumerable collection (e.g. Array). # Allow a single value not wrapped into a Set value = value.is_a?(Enumerable) ? Set.new(value) : Set[value] builder.delete_value(name, value) end end [builder.request] end |
#observable_by_user_result ⇒ Object
94 95 96 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 94 def observable_by_user_result nil end |
#on_commit ⇒ Object
82 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 82 def on_commit; end |
#on_registration ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 72 def on_registration validate_primary_key! validate_attribute_names!(@model_class, @attributes.keys) if @block @item_updater = ItemUpdater.new(@model_class) @block.call(@item_updater) end end |
#on_rollback ⇒ Object
84 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 84 def on_rollback; end |
#skipped? ⇒ Boolean
90 91 92 |
# File 'lib/dynamoid/transactions/mutation/update_fields.rb', line 90 def skipped? @attributes.empty? && (!@item_updater || @item_updater.empty?) end |