aws-record 2.0.2 → 2.1.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
  SHA1:
3
- metadata.gz: 7d74cea18f658e37fa6d0859826cbf76cb013a17
4
- data.tar.gz: 8c624c68f54659a10c48231a70be86608254fe49
3
+ metadata.gz: 1183f72d5c4a9eb76cce79e3dc780651d33840a8
4
+ data.tar.gz: 9b003057c87a84aca97b35e1bbeb2c92f873cb53
5
5
  SHA512:
6
- metadata.gz: b420c83918fc9306243df3e2b2f3cbb2226893763317bd84f97851c1cd9f7e34d1136929546417f627328ad275f4eb50e8621aa428ca49aa760e2d5e9a005b84
7
- data.tar.gz: 1139b5b759c65e1b6387046ba0ad139cb695e438f1e8dc5cab7d343a74a0473f3e24da5b173cb9e5bfa227f3648401e276462a616a70a30bb4ff7830c91f572a
6
+ metadata.gz: 641f8d63bd27d4c77725cf09c67260e7ea46d11b1502778c180fdb356fd987ae4ded2267428f4466bcf8412aefcf57135a3ea56383fe017af076e67573af6daa
7
+ data.tar.gz: 6cd6d4756195610a012767c3f7a9b5a7818ab931a5c0b78ab47b8a895963a7378d90e38ee829f45399826e1648a7b78391b61d9be0fe70155933635625c3f0e6
@@ -135,6 +135,70 @@ module Aws
135
135
  @data.dirty?
136
136
  end
137
137
 
138
+ # Returns +true+ if the model is not new and has not been deleted, +false+ otherwise.
139
+ #
140
+ # @example
141
+ # class Model
142
+ # include Aws::Record
143
+ # integer_attr :id, hash_key: true
144
+ # string_attr :name
145
+ # end
146
+ #
147
+ # model = Model.new
148
+ # model.persisted? # => false
149
+ # model.save
150
+ # model.persisted? # => true
151
+ # model.delete!
152
+ # model.persisted? # => false
153
+ #
154
+ # @return [Boolean] +true+ if the model is not new and has not been deleted, +false+
155
+ # otherwise.
156
+ def persisted?
157
+ @data.persisted?
158
+ end
159
+
160
+ # Returns +true+ if the model is newly initialized, +false+ otherwise.
161
+ #
162
+ # @example
163
+ # class Model
164
+ # include Aws::Record
165
+ # integer_attr :id, hash_key: true
166
+ # string_attr :name
167
+ # end
168
+ #
169
+ # model = Model.new
170
+ # model.new_record? # => true
171
+ # model.save
172
+ # model.new_record? # => false
173
+ #
174
+ # @return [Boolean] +true+ if the model is newly initialized, +false+
175
+ # otherwise.
176
+ def new_record?
177
+ @data.new_record?
178
+ end
179
+
180
+ # Returns +true+ if the model has been destroyed, +false+ otherwise.
181
+ #
182
+ # @example
183
+ # class Model
184
+ # include Aws::Record
185
+ # integer_attr :id, hash_key: true
186
+ # string_attr :name
187
+ # end
188
+ #
189
+ # model = Model.new
190
+ # model.destroyed? # => false
191
+ # model.save
192
+ # model.destroyed? # => false
193
+ # model.delete!
194
+ # model.destroyed? # => true
195
+ #
196
+ # @return [Boolean] +true+ if the model has been destroyed, +false+
197
+ # otherwise.
198
+ def destroyed?
199
+ @data.destroyed?
200
+ end
201
+
138
202
  # Fetches attributes for this instance of an item from Amazon DynamoDB
139
203
  # using its primary key and the +find(*)+ class method.
140
204
  #
@@ -97,6 +97,7 @@ module Aws
97
97
  data.set_attribute(name, attr.extract(item))
98
98
  end
99
99
  data.clean!
100
+ data.new_record = false
100
101
  ret << record
101
102
  end
102
103
  ret
@@ -23,8 +23,13 @@ module Aws
23
23
  @model_attributes = model_attributes
24
24
  @track_mutations = opts[:track_mutations]
25
25
  @track_mutations = true if opts[:track_mutations].nil?
26
+ @new_record = true
27
+ @destroyed = false
28
+
26
29
  populate_default_values
27
30
  end
31
+
32
+ attr_accessor :new_record, :destroyed
28
33
 
29
34
  def get_attribute(name)
30
35
  @model_attributes.attribute_for(name).type_cast(@data[name])
@@ -34,6 +39,18 @@ module Aws
34
39
  @data[name] = value
35
40
  end
36
41
 
42
+ def new_record?
43
+ @new_record
44
+ end
45
+
46
+ def destroyed?
47
+ @destroyed
48
+ end
49
+
50
+ def persisted?
51
+ !(new_record? || destroyed?)
52
+ end
53
+
37
54
  def raw_value(name)
38
55
  @data[name]
39
56
  end
@@ -81,6 +81,109 @@ module Aws
81
81
  end
82
82
  end
83
83
 
84
+
85
+ # Deletes the item instance that matches the key values of this item
86
+ # instance in Amazon DynamoDB.
87
+ #
88
+ # @example Usage Example
89
+ # class MyModel
90
+ # include Aws::Record
91
+ # integer_attr :uuid, hash_key: true
92
+ # string_attr :name, range_key: true
93
+ # integer_attr :age
94
+ # float_attr :height
95
+ # end
96
+ #
97
+ # model = MyModel.new(id: 4, name: "John", age: 4, height: 70.5)
98
+ # model.age # => 4
99
+ # model.height # => 70.5
100
+ # model.save
101
+ # model.dirty? # => false
102
+ #
103
+ # model.assign_attributes(age: 5, height: 150.75)
104
+ # model.age # => 5
105
+ # model.height # => 150.75
106
+ # model.dirty? # => true
107
+ #
108
+ #
109
+ # @param [Hash] opts
110
+ def assign_attributes(opts)
111
+ opts.each do |field, new_value|
112
+ field = field.to_sym
113
+ setter = "#{field}="
114
+ raise ArgumentError.new "Invalid field: #{field} for model" unless respond_to?(setter)
115
+ public_send(setter, new_value)
116
+ end
117
+ end
118
+
119
+ # Mass assigns the attributes to the model and then performs a save
120
+ #
121
+ # You can use the +:force+ option to perform a simple put/overwrite
122
+ # without conditional validation or update logic.
123
+ #
124
+ # Note that aws-record allows you to change your model's key values,
125
+ # but this will be interpreted as persisting a new item to your DynamoDB
126
+ # table
127
+ #
128
+ # @example Usage Example
129
+ # class MyModel
130
+ # include Aws::Record
131
+ # integer_attr :uuid, hash_key: true
132
+ # string_attr :name, range_key: true
133
+ # integer_attr :age
134
+ # float_attr :height
135
+ # end
136
+ #
137
+ # model = MyModel.new(id: 4, name: "John", age: 4, height: 70.5)
138
+ # model.age # => 4
139
+ # model.height # => 70.5
140
+ # model.save
141
+ # model.dirty? # => false
142
+ #
143
+ # model.update(age: 5, height: 150.75)
144
+ # model.age # => 5
145
+ # model.height # => 150.75
146
+ # model.dirty? # => false
147
+ #
148
+ #
149
+ # @param [Hash] new_param, contains the new parameters for the model
150
+ #
151
+ # @param [Hash] opts
152
+ # @option opts [Boolean] :force if true, will save as a put operation and
153
+ # overwrite any existing item on the remote end. Otherwise, and by
154
+ # default, will either perform a conditional put or an update call.
155
+ # @return false if the record is invalid as defined by an attempt to call
156
+ # +valid?+ on this item, if that method exists. Otherwise, returns client
157
+ # call return value.
158
+ def update(new_params, opts = {})
159
+ assign_attributes(new_params)
160
+ save(opts)
161
+ end
162
+
163
+ # Updates model attributes and validates new values
164
+ #
165
+ # You can use the +:force+ option to perform a simple put/overwrite
166
+ # without conditional validation or update logic.
167
+ #
168
+ # Note that aws-record allows you to change your model's key values,
169
+ # but this will be interpreted as persisting a new item to your DynamoDB
170
+ # table
171
+ #
172
+ # @param [Hash] new_param, contains the new parameters for the model
173
+ #
174
+ # @param [Hash] opts
175
+ # @option opts [Boolean] :force if true, will save as a put operation and
176
+ # overwrite any existing item on the remote end. Otherwise, and by
177
+ # default, will either perform a conditional put or an update call.
178
+ # @return The update mode if the update is successful
179
+ #
180
+ # @raise [Aws::Record::Errors::ValidationError] if any new values
181
+ # violate the models validations.
182
+ def update!(new_params, opts = {})
183
+ assign_attributes(new_params)
184
+ save!(opts)
185
+ end
186
+
84
187
  # Deletes the item instance that matches the key values of this item
85
188
  # instance in Amazon DynamoDB. Uses the
86
189
  # {http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#delete_item-instance_method Aws::DynamoDB::Client#delete_item}
@@ -90,7 +193,7 @@ module Aws
90
193
  table_name: self.class.table_name,
91
194
  key: key_values
92
195
  )
93
- true
196
+ self.instance_variable_get("@data").destroyed = true
94
197
  end
95
198
 
96
199
  private
@@ -151,6 +254,10 @@ module Aws
151
254
  )
152
255
  end
153
256
  end
257
+ data = self.instance_variable_get("@data")
258
+ data.destroyed = false
259
+ data.new_record = false
260
+ true
154
261
  end
155
262
 
156
263
  def _build_item_for_save
@@ -382,6 +489,7 @@ module Aws
382
489
  data = record.instance_variable_get("@data")
383
490
  attributes.attributes.each do |name, attr|
384
491
  data.set_attribute(name, attr.extract(resp.item))
492
+ data.new_record = false
385
493
  end
386
494
  record
387
495
  end
@@ -13,6 +13,6 @@
13
13
 
14
14
  module Aws
15
15
  module Record
16
- VERSION = '2.0.2'
16
+ VERSION = '2.1.0'
17
17
  end
18
18
  end
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.0.2
4
+ version: 2.1.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: 2018-06-08 00:00:00.000000000 Z
11
+ date: 2018-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-dynamodb