aws-record 2.7.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67f48531ca8b917f680a1d9a5d21afd1611aba0eb96916a5783addaf7939cda8
4
- data.tar.gz: 7f6cbb16271455ada17a61f32106800b65d3b4103a5254e7686e06d5cf715cca
3
+ metadata.gz: 2aaa5337431b2b2f59b35245afd1fb9eb4cacb5226f9cc60a2fefeed5bde7619
4
+ data.tar.gz: 8e8a225d9abfb3ec8ca8ee30606df05016166bbb10e0affc4d81fbfbcaaa21f4
5
5
  SHA512:
6
- metadata.gz: 5bb474f4d13e469b9af991e061b10a54279f37d7390b9cb1bffafd40d039c649c12ca5423140d4a232a08536ec715df89db2e07f6670ef0242732cbe78d1d890
7
- data.tar.gz: 9b61d0d93ce9b657989fefde1201aab35253e656035ffb87ccbebfbafe4ea423883146dd4dec510dfdf5deec74b274784f6490d86804cf76b09aeee58f41cdfb
6
+ metadata.gz: f3ca0395934b8b5f464e7208c8b9b55c104475a045e6e93c01d9a8dfbcbbcab5bed6e4d3563ff963d43a93cce79e2918bfba032e4dab83d42168603289aa2bee
7
+ data.tar.gz: 5ed32da4f64d414076c16265ae5200beeb2a02225fb03cc9626626df9bdc1e07a48918231576236e59a22edff0e940fb48be582f7a3fb7162353bbeb6cc9a4fc
@@ -382,6 +382,77 @@ module Aws
382
382
  attr(name, Marshalers::NumericSetMarshaler.new(opts), opts)
383
383
  end
384
384
 
385
+ # Define an atomic counter attribute for your model.
386
+ #
387
+ # Atomic counter are an integer-type attribute that is incremented,
388
+ # unconditionally, without interfering with other write requests.
389
+ # The numeric value increments each time you call +increment_<attr>!+.
390
+ # If a specific numeric value are passed in the call, the attribute will
391
+ # increment by that value.
392
+ #
393
+ # To use +increment_<attr>!+ method, the following condition must be true:
394
+ # * None of the attributes have dirty changes.
395
+ # * If there is a value passed in, it must be an integer.
396
+ # For more information, see
397
+ # {https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters Atomic counter}
398
+ # in the Amazon DynamoDB Developer Guide.
399
+ #
400
+ # @param [Symbol] name Name of this attribute. It should be a name that
401
+ # is safe to use as a method.
402
+ # @param [Hash] opts
403
+ # @option opts [Object] :default_value Optional attribute used to
404
+ # define a "default value" to be used if the attribute's value on an
405
+ # item is nil or not set at persistence time. The "default value" of
406
+ # the attribute starts at 0.
407
+ #
408
+ # @example Usage Example
409
+ # class MyRecord
410
+ # include Aws::Record
411
+ # integer_attr :id, hash_key: true
412
+ # atomic_counter :counter
413
+ # end
414
+ #
415
+ # record = MyRecord.find(id: 1)
416
+ # record.counter #=> 0
417
+ # record.increment_counter! #=> 1
418
+ # record.increment_counter!(2) #=> 3
419
+ # @see #attr #attr method for additional hash options.
420
+ def atomic_counter(name, opts = {})
421
+ opts[:dynamodb_type] = "N"
422
+ opts[:default_value] ||= 0
423
+ attr(name, Marshalers::IntegerMarshaler.new(opts), opts)
424
+
425
+ define_method("increment_#{name}!") do |increment=1|
426
+
427
+ if dirty?
428
+ msg = "Attributes need to be saved before atomic counter can be incremented"
429
+ raise Errors::RecordError, msg
430
+ end
431
+
432
+ unless increment.is_a?(Integer)
433
+ msg = "expected an Integer value, got #{increment.class}"
434
+ raise ArgumentError, msg
435
+ end
436
+
437
+ resp = dynamodb_client.update_item({
438
+ table_name: self.class.table_name,
439
+ key: key_values,
440
+ expression_attribute_values: {
441
+ ":i" => increment
442
+ },
443
+ expression_attribute_names: {
444
+ "#n" => name
445
+ },
446
+ update_expression: "SET #n = #n + :i",
447
+ return_values: "UPDATED_NEW"
448
+ })
449
+ assign_attributes(resp[:attributes])
450
+ @data.clean!
451
+ @data.get_attribute(name)
452
+ end
453
+
454
+ end
455
+
385
456
  # @return [Symbol,nil] The symbolic name of the table's hash key.
386
457
  def hash_key
387
458
  @keys.hash_key
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-28 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-dynamodb
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  requirements: []
87
- rubygems_version: 3.2.7
87
+ rubygems_version: 3.3.3
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: AWS Record library for Amazon DynamoDB