couch_potato 1.22.0 → 1.22.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d8263b3487e2591fa53b3f19f6c7363bfa0d9478d449098d1bc2eee29b90ee7
4
- data.tar.gz: 6ab3f176e570bcc2f94f531ba7248cd7bcfdd2a2bde2cf5f13b0fffd077a7dbd
3
+ metadata.gz: 666f7dd052f1c9fbaa75382ed1bca8103b41603950087f1081cf62ff184964bc
4
+ data.tar.gz: b6ac16b525eebb35e27d92c5546dcb4ff1359a15991d760ab5c5c39490664b88
5
5
  SHA512:
6
- metadata.gz: 4f59675db667b580551a53f1a7e978d0f860f2cfad4b7aae58676d59679f84664e45544a38b39d501851d6a76752e097e3912fb3e15f4e5220621b8653c44f0b
7
- data.tar.gz: 90c43f70f518f38e325066435b5b5ad3051df392a91399707fc814bb88615bbf90a9eada713781aa20fe5fde73a79730462a22eb8fc1da725218e16f560ba610
6
+ metadata.gz: 537ff4580ebe58fe9d5b29e10bae273ee70b3b85b7762af94448b70c11b213fc800ca434b6ef47156c62c436b6b398ec162344983fa0d508b2b95611c7560696
7
+ data.tar.gz: ccb0b54672f83d83762aa900887dbfd950e2d9f8a4fe92790cf01355d3945534fc4a969d9337ba9816dee8715c58a02657795c59074e8a8c553a67a76f9b0f00
data/CHANGES.md CHANGED
@@ -1,159 +1,175 @@
1
- ## Changes
1
+ # Changes
2
2
 
3
- # 1.22.00
3
+ ## 1.22.2
4
+
5
+ - fix instantiating properties that include CouchPotato::Persistence (call .json_create instead of .new)
6
+
7
+ ## 1.22.1
8
+
9
+ - fix lazy property loading calling overridden setters (use private `assign_attribute` instead)
10
+
11
+ ## couch_potato-rspec 4.4.0
12
+
13
+ - support multiple return values in `stub_view(...).and_return` for consecutive `view` / `first` / `first!` / `view_in_batches` calls
14
+
15
+ ## couch_potato-rspec 4.3.0
16
+
17
+ - support flex views in `stub_db` / `stub_view` (array returns a `FlexViewSpec::Results` stub with `docs`; `first`/`first!` stubbed on the db)
18
+
19
+ ## 1.22.00
4
20
 
5
21
  - support `Database#first` / `#first!` for flex views
6
22
 
7
- # 1.21.0
23
+ ## 1.21.0
8
24
 
9
25
  - add railties 8.1, Ruby 3.4 to CI
10
26
 
11
- # 1.20.1
27
+ ## 1.20.1
12
28
 
13
29
  - fix `database.save!` did not use passed block
14
30
 
15
- # 1.20.0
31
+ ## 1.20.0
16
32
 
17
33
  - add passing validation context to save methods
18
34
 
19
- # 1.19.2
35
+ ## 1.19.2
20
36
 
21
37
  - fix 404/409 error when querying single design doc with `digest_view_names` enabled
22
38
 
23
- # 1.19.1
39
+ ## 1.19.1
24
40
 
25
41
  - add digest to design doc name if `single_design_document` and `digest_view_names` are enabled
26
42
 
27
- # 1.19.0 / rspec-matchers 4.2.0
43
+ ## 1.19.0 / rspec-matchers 4.2.0
28
44
 
29
45
  - add `single_design_document` config option
30
46
  - remove support for lists and lib
31
47
 
32
- # 1.18.0
48
+ ## 1.18.0
33
49
 
34
50
  - add testing Rails 7.2/8 on CI
35
51
  - change gemspec to allow for Rails 8
36
52
  - remove support for Rails < 7.2
37
53
 
38
- # 1.17.0
54
+ ## 1.17.0
39
55
 
40
56
  - filter out nil ids for loading multiple documents
41
57
  - cache nil documents
42
58
 
43
- # 1.16.0
59
+ ## 1.16.0
44
60
 
45
61
  - add payload to ActiveSupport instrumentation calls
46
62
  - only notify load.cached when there are cached documents
47
63
 
48
- # 1.15.0
64
+ ## 1.15.0
49
65
 
50
66
  - cache loading multiple documents
51
67
  - keep the database cache when switching to another database and back to the original one
52
68
 
53
- # 1.14.0
69
+ ## 1.14.0
54
70
 
55
71
  - add database_collection to models to help avoid n+1 requests
56
72
 
57
- # 1.13.0
73
+ ## 1.13.0
58
74
 
59
75
  - add Ruby 3.2 support
60
76
  - remove Ruby 2.7 support
61
77
  - add active_support 7.1 support
62
78
  - remove active_support 6.x support
63
79
 
64
- # 1.12.1
80
+ ## 1.12.1
65
81
 
66
82
  - re-enable aliases when parsing config yaml file
67
83
 
68
- # 1.12.0
84
+ ## 1.12.0
69
85
 
70
86
  - remove active_support 5.x
71
87
  - add Ruby 3.1 support (active_support 6.1, 7.0)
72
88
 
73
- # 1.11.0
89
+ ## 1.11.0
74
90
 
75
91
  - improve view_in_batches performance by switching to using startkey_docid over skip
76
92
 
77
- ### 1.10.1
93
+ ## 1.10.1
78
94
 
79
95
  - support passing an empty array to CouchPotato::Database#load
80
96
 
81
- ### 1.10.0
97
+ ## 1.10.0
82
98
 
83
99
  - add spec/support for Rails 7
84
100
 
85
- ### 1.9.0
101
+ ## 1.9.0
86
102
 
87
103
  - add spec/support for Rails 6.1
88
104
  - add caching Couchrest connections to reduce total number created
89
105
 
90
- ### 1.8.0
106
+ ## 1.8.0
91
107
 
92
108
  - remove not saving model if it is not dirty. this could lead to missed document updates if the document has been updated in a different process in-between being loaded and saved in the current process.
93
109
 
94
110
  - remove deep dirty tracking
95
111
 
96
- ### 1.7.1
112
+ ## 1.7.1
97
113
 
98
114
  - added support for properties of type Integer
99
115
 
100
- ### 1.7.0
116
+ ## 1.7.0
101
117
 
102
118
  - added `_revisions` method that returns all available revisions of a document.
103
119
 
104
- ### 1.6.4
120
+ ## 1.6.4
105
121
 
106
122
  - bug fix for accessing inherited properties (Alexander Lang)
107
123
 
108
- ### 1.6.3
124
+ ## 1.6.3
109
125
 
110
126
  - added ActiveSupport instrumentation (Alexander Lang)
111
127
 
112
- ### 1.6.2
128
+ ## 1.6.2
113
129
 
114
130
  - view digest bugfix (Alexander Lang)
115
131
 
116
- ### 1.6.1
132
+ ## 1.6.1
117
133
 
118
134
  - added option to add digests to a single view (Alexander Lang)
119
135
 
120
- ### 1.6.0
136
+ ## 1.6.0
121
137
 
122
138
  - added global option to add digests to view names (Alexander Lang)
123
139
 
124
- ### 1.5.1
140
+ ## 1.5.1
125
141
 
126
142
  - updated CouchRest to 2.0.0.rc3 in order to re-use http connections. Performance improvements. (Thilo Utke)
127
143
  - added passing params to lists (Alexander Lang)
128
144
 
129
- ### 1.5.0
145
+ ## 1.5.0
130
146
 
131
147
  - Moved RSpec matchers into couch_potato-rspec gem. This way, people not using RSpec don't need to install the helpers, plus we can release separate matchers for RSpec 2 and 3.
132
148
 
133
- ### 1.4.0
149
+ ## 1.4.0
134
150
 
135
151
  - Added support for passing the model to blocks for computing the default value of a property (Alexander Lang)
136
152
 
137
- ### 1.3.0
153
+ ## 1.3.0
138
154
 
139
155
  - Add support for built-in couchdb reduce functions in map reduce specs (Andy Morris)
140
156
  - Make specs handle CommonJS modules via module.exports as well as exports (Andy Morris)
141
157
  - Changed #attributes to return a HashWithIndifferentAccess (Owen Davies)
142
158
 
143
- ### 1.2.0
159
+ ## 1.2.0
144
160
 
145
161
  - adds optional deep dirty tracking (andymorris)
146
162
  - fixes an exception when deleting an already deleted document (Alexander Lang)
147
163
 
148
- ### 1.1.4
164
+ ## 1.1.4
149
165
 
150
166
  - Removes the dependency to json/add/core (cstettner)
151
167
 
152
- ### 1.1.3
168
+ ## 1.1.3
153
169
 
154
170
  - removes check if database exists to avoid lots of additional requests (Alexander Lang)
155
171
 
156
- ### 1.1.2
172
+ ## 1.1.2
157
173
 
158
174
  - fixes `CouchPotato.models` did not include subclasses
159
175
  - adds `CouchPotato.use` (Daniel Lohse)
@@ -161,19 +177,19 @@
161
177
  - adds `CouchPotato::Config.database_host` for using multiple databases in a project (Daniel Lohse)
162
178
  - makes `cast_native` cast floats with no leading digits (wrshawn)
163
179
 
164
- ### 1.1.1
180
+ ## 1.1.1
165
181
 
166
182
  - fixes properties were leaked to sibling classes (Alexander Lang)
167
183
 
168
- ### 1.1.0
184
+ ## 1.1.0
169
185
 
170
186
  - adds conflict handling for Database#save/destroy (Alexander Lang)
171
187
 
172
- ### 1.0.1
188
+ ## 1.0.1
173
189
 
174
190
  - fixes error when bulk loaded document does not respond to database= (Alexander Lang)
175
191
 
176
- ### 1.0.0
192
+ ## 1.0.0
177
193
 
178
194
  - adds `reload` method (Alexander Lang)
179
195
  - removes `total_rows` from database results (Alexander Lang)
@@ -187,11 +203,11 @@
187
203
  - adds suppot for BigDecimal properties (Fredrik Rubensson)
188
204
  - adds support for 2.0, Rubinius, 1.9.3, drops Ruby 1.8, 1.9.2
189
205
 
190
- ### 0.7.1
206
+ ## 0.7.1
191
207
 
192
208
  - fixes a bug when trying to bulk-load non-existant documents
193
209
 
194
- ### 0.7.0
210
+ ## 0.7.0
195
211
 
196
212
  - ActiveSupport/Rails 3.2 compatibility (Alexander Lang)
197
213
  - removed Object#try, String#blank? as they are part of ActiveSupport - ActiveSupport's try behaves differently than the couch potato implementation so this change might break your app (now calling a non-existant method on a non-nil raises a NoMethodError, before it did not) (Alexander Lang)
@@ -205,168 +221,168 @@
205
221
  - don't crash, only warn if couchdb.yml is missing (Alexander Lang)
206
222
  - use the therubyracer gem to run view specs instead of relying on a `js` executable (Alexander Lang)
207
223
 
208
- ### 0.6.0
224
+ ## 0.6.0
209
225
 
210
226
  - ActiveSupport/Rails 3.1 compatibility (Maximilian Mack)
211
227
  - fix no such file to load with json/add/rails (Simone Carletti)
212
228
 
213
- ### 0.5.7
229
+ ## 0.5.7
214
230
 
215
231
  - support CouchPotato::Database#first/#first! calls when using `stub_db` from tests (langalex)
216
232
  - support RSpec2 block syntax in `stub_db` (langalex)
217
233
 
218
- ### 0.5.6
234
+ ## 0.5.6
219
235
 
220
236
  - remove the stale parameter from a view query if it's nil, as couchdb only allows stale to be ok or update_after (langalex)
221
237
 
222
- ### 0.5.5
238
+ ## 0.5.5
223
239
 
224
240
  - support for split_design_documents_per_view (jweiss)
225
241
  - errors now returns a Hash instead of an Array (bterkuile)
226
242
  - support passing in list names as symbols in view specs (langalex)
227
243
 
228
- ### 0.5.4
244
+ ## 0.5.4
229
245
 
230
246
  - cast 'false' to false for boolean properties (langalex)
231
247
 
232
- ### 0.5.3
248
+ ## 0.5.3
233
249
 
234
250
  - added CouchPotato::Database.load! (langalex)
235
251
 
236
- ### 0.5.2
252
+ ## 0.5.2
237
253
 
238
254
  - added CouchPotato::Database#first and #first! methods (langalex)
239
255
  - added workaround for BigCouch/Cloudant to not add null reduce functions to views (langalex)
240
256
  - don't add \_attachments if there are none (langalex)
241
257
 
242
- ### 0.5.1
258
+ ## 0.5.1
243
259
 
244
260
  - fixed issues with tzinfo gem (Bernd Ahlers)
245
261
 
246
- ### 0.5.0
262
+ ## 0.5.0
247
263
 
248
264
  - time zone support (Time properties are now converted to current Time.zone) (langalex)
249
265
  - lazy property initialization (performance!) (langalex)
250
266
  - active_model is now the default validation framework (langalex)
251
267
 
252
- ### 0.4.0
268
+ ## 0.4.0
253
269
 
254
270
  - ruby 1.9.2 compatibility (langalex)
255
271
  - couch potato objects now behave correctly when used as keys in Hashes (langalex)
256
272
  - use as_json instead of to_s(:json), which is the rails way
257
273
  - use ActiveModel dirty tracking (langalex) - this means no more "deep tracking", e.g. `user.tags << 'new_tag'; user.dirty? # false`
258
274
 
259
- ### 0.3.2
275
+ ## 0.3.2
260
276
 
261
277
  - support yielding to blocks on #initialize (martinrehfeld)
262
278
  - support for negative numbers in Fixnum/Float properties (langalex)
263
279
 
264
- ### 0.3.1
280
+ ## 0.3.1
265
281
 
266
282
  - ActiveModel callbacks (kazjote)
267
283
  - do not use Rails.env in initializer as it will free Rails.env for all times and in Rails 2.3.x apps it will be called too early thus always beeing development (jweiss)
268
284
  - ruby 1.9.2 compatibility (langalex)
269
285
  - can configure validation framework in couchdb.yml, process couchdb.yml with erb (langalex)
270
286
 
271
- ### 0.3.0
287
+ ## 0.3.0
272
288
 
273
289
  - support for lists (langalex)
274
290
 
275
- ### 0.2.32
291
+ ## 0.2.32
276
292
 
277
293
  - added persisted? and to_key for proper ActiveModel compliance (thilo)
278
294
  - id setter (jhohertz-work)
279
295
  - load document ids if include_documents is false (jweiss)
280
296
  - persist given created_at/updated_at instead of Time.now (langalex)
281
297
 
282
- ### 0.2.31
298
+ ## 0.2.31
283
299
 
284
300
  - Removed requirement for validatable gem. Allows for using more uptodate versions of the library, or doesn't install it when you're using ActiveModel. (mattmatt)
285
301
  - fixed callbacks of super classes were not run (langalex)
286
302
 
287
- ### 0.2.30
303
+ ## 0.2.30
288
304
 
289
305
  - pass in multiple keys when querying a view (langalex)
290
306
 
291
- ### 0.2.29
307
+ ## 0.2.29
292
308
 
293
309
  - nicer inspect() for models (mattmatt)
294
310
  - fixed (re)reduce for property views wasn't working (langalex)
295
311
 
296
- ### 0.2.28
312
+ ## 0.2.28
297
313
 
298
314
  - fixed reloading nested classes (langalex)
299
315
  - fixed constant missing error when loading models with uninitialized classes via views (langalex)
300
316
  - added rspec helpers for stubbing out views (langalex)
301
317
  - fixed design document names for nested model classes (svenfuchs)
302
318
 
303
- ### 0.2.27
319
+ ## 0.2.27
304
320
 
305
321
  - workaround for Rails apps using bundler: database name was not initialized from couchdb.yml (langalex)
306
322
 
307
- ### 0.2.26
323
+ ## 0.2.26
308
324
 
309
325
  - added to_s(:json) to Date and Time to be able to get properly formatted dates/times for searching with dates/times (langalex)
310
326
  - all times are now stored as UTC (langalex)
311
327
  - added support for Float attributes (arbovm)
312
328
 
313
- ### 0.2.25
329
+ ## 0.2.25
314
330
 
315
331
  - automatic view updates: when you change the definition of a view couch potato will now update the design document in the database (langalex)
316
332
  - support for properties of type Date, better support for Time (langalex)
317
333
  - support for default reduce count methods in custom views (jweiss)
318
334
 
319
- ### 0.2.24
335
+ ## 0.2.24
320
336
 
321
337
  - persistent instances can now be marked as dirty with #is_dirty (langalex)
322
338
 
323
- ### 0.2.23
339
+ ## 0.2.23
324
340
 
325
341
  - Couch Potato models now conform to the ActiveModel interface when ActiveModel is installed, see http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ (langalex)
326
342
  - fixed error with dirty tracking and BigDecimals (thilo)
327
343
  - added the ability to use ActiveModel validations instead of validatable (martinrehfeld)
328
344
 
329
- ### 0.2.22
345
+ ## 0.2.22
330
346
 
331
347
  - fixed properties with default values returned default when a blank value like '' or [] was set (langalex)
332
348
 
333
- ### 0.2.21
349
+ ## 0.2.21
334
350
 
335
351
  - automatically set a database instance on results of CouchPotato::Database#view (langalex)
336
352
  - improved auto loading of unloaded constants - can now load constants that have never been loaded before (langalex)
337
353
  - raise exception on invalid parameters passed to a couchdb view query (langalex)
338
354
  - when querying a view: pass in ranges as key instead of startkey/endkey, pass in plain value instead of hash with key (langalex)
339
355
 
340
- ### 0.2.20
356
+ ## 0.2.20
341
357
 
342
358
  - support for :boolean properties (jweiss)
343
359
  - return the total_rows when querying a view (langalex)
344
360
 
345
- ### 0.2.19
361
+ ## 0.2.19
346
362
 
347
363
  - added conditions to views (langalex)
348
364
 
349
- ### 0.2.18
365
+ ## 0.2.18
350
366
 
351
367
  - set Fixnum property to nil when given a blank string (langalex)
352
368
 
353
- ### 0.2.17
369
+ ## 0.2.17
354
370
 
355
371
  - fixed nil attributes were omitted in json (jweiss, mattmatt)
356
372
  - support for properties of type Fixnum (langalex)
357
373
 
358
- ### 0.2.16
374
+ ## 0.2.16
359
375
 
360
376
  - fixed problem with classes being not loaded in rails development mode (langalex)
361
377
  - fixed persist boolean false value (bernd)
362
378
 
363
- ### 0.2.15
379
+ ## 0.2.15
364
380
 
365
381
  - ability to change the name of the attribute that stores the ruby class in the documents by setting JSON.create_id (lennart)
366
382
  - fixed double loading issue with bundler (jweiss)
367
383
  - fixed an issue with setting attachments (endor)
368
384
 
369
- ### 0.2.13
385
+ ## 0.2.13
370
386
 
371
387
  - support adding errors in before_validation callbacks (mattmatt)
372
388
  - support for inheritance (mattmatt)
@@ -374,11 +390,11 @@
374
390
  - improved (de)serialization now supports deserializing nested objects (railsbros, specs by hagenburger)
375
391
  - RSpec matchers for testing map/reduce functions (langalex)
376
392
 
377
- ### 0.2.10
393
+ ## 0.2.10
378
394
 
379
395
  - fixed bug with hardcoded timezone
380
396
 
381
- ### 0.2.9
397
+ ## 0.2.9
382
398
 
383
399
  - allow to overwrite attribute accessor of properties and use super to call the original accessors
384
400
  - allow read access to attributes that are present in the Couchdb document but not defined as properties
@@ -12,6 +12,13 @@ module CouchPotato
12
12
  value
13
13
  end
14
14
  end
15
+
16
+ def assign_attribute(name, value, from_json: false)
17
+ property = self.class.properties.find_property(name)
18
+ typecasted_value = type_caster.cast(value, property.type, from_json:)
19
+ send("#{name}_will_change!") unless @skip_dirty_tracking || typecasted_value == send(name)
20
+ instance_variable_set("@#{name}", typecasted_value)
21
+ end
15
22
  end
16
23
 
17
24
  class SimpleProperty #:nodoc:
@@ -19,7 +26,6 @@ module CouchPotato
19
26
 
20
27
  def initialize(owner_clazz, name, options = {})
21
28
  self.name = name
22
- @setter_name = "#{name}="
23
29
  self.type = options[:type]
24
30
  self.default_value = options[:default]
25
31
  @type_caster = TypeCaster.new
@@ -29,8 +35,7 @@ module CouchPotato
29
35
  end
30
36
 
31
37
  def build(object, json)
32
- value = json[name]
33
- object.send @setter_name, value
38
+ object.send(:assign_attribute, name, json[name], from_json: true)
34
39
  end
35
40
 
36
41
  def serialize(json, object)
@@ -77,9 +82,7 @@ module CouchPotato
77
82
  end
78
83
 
79
84
  define_method "#{name}=" do |value|
80
- typecasted_value = type_caster.cast(value, options[:type])
81
- send("#{name}_will_change!") unless @skip_dirty_tracking || typecasted_value == send(name)
82
- self.instance_variable_set(ivar_name, typecasted_value)
85
+ assign_attribute(name, value)
83
86
  end
84
87
 
85
88
  define_method "#{name}?" do
@@ -6,14 +6,14 @@ module CouchPotato
6
6
  class TypeCaster #:nodoc:
7
7
  NUMBER_REGEX = /-?\d*\.?\d*/.freeze
8
8
 
9
- def cast(value, type)
9
+ def cast(value, type, from_json: false)
10
10
  if type == :boolean
11
11
  cast_boolean(value)
12
12
  elsif type.instance_of?(Array)
13
13
  nested_type = type.first
14
- value&.map { |val| cast_native(val, nested_type) }
14
+ value&.map { |val| cast_native(val, nested_type, from_json:) }
15
15
  else
16
- cast_native(value, type)
16
+ cast_native(value, type, from_json:)
17
17
  end
18
18
  end
19
19
 
@@ -29,7 +29,7 @@ module CouchPotato
29
29
  end
30
30
  end
31
31
 
32
- def cast_native(value, type)
32
+ def cast_native(value, type, from_json:)
33
33
  if type && !value.is_a?(type)
34
34
 
35
35
  if %w[Integer Bignum].include?(type.to_s)
@@ -40,7 +40,7 @@ module CouchPotato
40
40
  value.to_d unless value.blank?
41
41
  elsif type == Hash
42
42
  value.to_hash unless value.blank?
43
- elsif type.ancestors.include?(CouchPotato::Persistence)
43
+ elsif type.ancestors.include?(CouchPotato::Persistence) && !from_json
44
44
  type.new value unless value.blank?
45
45
  else
46
46
  type.json_create value unless value.blank?
@@ -1,4 +1,4 @@
1
1
  module CouchPotato
2
- VERSION = '1.22.0'.freeze
3
- RSPEC_VERSION = '4.2.0'.freeze
2
+ VERSION = '1.22.2'.freeze
3
+ RSPEC_VERSION = '4.4.0'.freeze
4
4
  end
@@ -12,6 +12,7 @@ class Watch
12
12
  property :custom_address, type: [Address]
13
13
  property :overwritten_read
14
14
  property :overwritten_write
15
+ property :normalized_title
15
16
  property :diameter, type: Float
16
17
 
17
18
  def overwritten_read
@@ -21,6 +22,10 @@ class Watch
21
22
  def overwritten_write=(value)
22
23
  super value.to_s
23
24
  end
25
+
26
+ def normalized_title=(value)
27
+ super(value.to_s.upcase)
28
+ end
24
29
  end
25
30
 
26
31
  class Address2 < Address
@@ -59,6 +64,12 @@ describe 'properties' do
59
64
  expect(Watch.new(:overwritten_write => 1).overwritten_write).to eq('1')
60
65
  end
61
66
 
67
+ it "does not call overwritten write accessors when loading from a document" do
68
+ watch = Watch.json_create('normalized_title' => 'Hello', JSON.create_id => 'Watch')
69
+
70
+ expect(watch.normalized_title).to eq('Hello')
71
+ end
72
+
62
73
  it "should return the property names" do
63
74
  expect(Comment.property_names).to eq([:created_at, :updated_at, :title])
64
75
  end
@@ -121,8 +132,8 @@ describe 'properties' do
121
132
  expect(w.custom_address[0]).to be_an_instance_of Address2
122
133
  end
123
134
 
124
- it 'initializes an typed array property from an array of hashes' do
125
- w = Watch.new(custom_address: [{id: 'a1', city: 'Berlin'}])
135
+ it 'initializes a typed array property from an array of hashes' do
136
+ w = Watch.new(custom_address: [{_id: 'a1', city: 'Berlin'}])
126
137
 
127
138
  expect(w.custom_address.map(&:class)).to eq([Address])
128
139
  expect(w.custom_address.map(&:id)).to eq(['a1'])
@@ -187,6 +198,32 @@ describe 'properties' do
187
198
  expect(p.ship_address).to be_nil
188
199
  end
189
200
 
201
+ it "persists a nested object with non-couch potato attributes delegating to properties" do
202
+ class PersonWithName
203
+ include CouchPotato::Persistence
204
+
205
+ property :first_name
206
+ property :last_name
207
+
208
+ def name=(value)
209
+ self.first_name = value.split(' ').first
210
+ self.last_name = value.split(' ').last
211
+ end
212
+ end
213
+ class PersonContainer
214
+ include CouchPotato::Persistence
215
+
216
+ property :person, type: PersonWithName
217
+ end
218
+
219
+ container = PersonContainer.new(person: {name: 'John Doe'})
220
+ CouchPotato.database.save_document!(container)
221
+ container = CouchPotato.database.load_document(container.id)
222
+
223
+ expect(container.person.first_name).to eq('John')
224
+ expect(container.person.last_name).to eq('Doe')
225
+ end
226
+
190
227
  it "should actually pass the null value down in the JSON document " do
191
228
  p = Person.new
192
229
  p.ship_address = nil
@@ -9,6 +9,12 @@ class WithStubbedView
9
9
  view :stubbed_view, key: :x
10
10
  end
11
11
 
12
+ class WithStubbedFlexView
13
+ include CouchPotato::Persistence
14
+
15
+ view :stubbed_flex_view, type: :flex, key: :x
16
+ end
17
+
12
18
  describe 'stubbing the db' do
13
19
  it 'replaces CouchPotato.database with a double' do
14
20
  CouchPotato.stub_db
@@ -37,6 +43,40 @@ describe 'stubbing a view' do
37
43
  expect(@db.view(WithStubbedView.stubbed_view('123'))).to eq([:result])
38
44
  end
39
45
 
46
+ it 'returns successive values on consecutive view calls' do
47
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return([:first], [:second])
48
+
49
+ expect(@db.view(WithStubbedView.stubbed_view('123'))).to eq([:first])
50
+ expect(@db.view(WithStubbedView.stubbed_view('123'))).to eq([:second])
51
+ end
52
+
53
+ it 'returns successive first values on consecutive first calls' do
54
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return([:a], [:b])
55
+
56
+ expect(@db.first(WithStubbedView.stubbed_view('123'))).to eq(:a)
57
+ expect(@db.first(WithStubbedView.stubbed_view('123'))).to eq(:b)
58
+ end
59
+
60
+ it 'returns successive values on consecutive first! calls and raises for a nil first value' do
61
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return([:a], [], [:b])
62
+
63
+ expect(@db.first!(WithStubbedView.stubbed_view('123'))).to eq(:a)
64
+ expect { @db.first!(WithStubbedView.stubbed_view('123')) }.to raise_error(CouchPotato::NotFound)
65
+ expect(@db.first!(WithStubbedView.stubbed_view('123'))).to eq(:b)
66
+ end
67
+
68
+ it 'yields successive return values on consecutive view_in_batches calls' do
69
+ @db.stub_view(WithStubbedView, :stubbed_view).with('123').and_return([:a, :b], [:c])
70
+
71
+ expect do |b|
72
+ @db.view_in_batches(WithStubbedView.stubbed_view('123'), batch_size: 2, &b)
73
+ end.to yield_successive_args([:a, :b])
74
+
75
+ expect do |b|
76
+ @db.view_in_batches(WithStubbedView.stubbed_view('123'), batch_size: 2, &b)
77
+ end.to yield_successive_args([:c])
78
+ end
79
+
40
80
  it 'stubs the database to return the first fake result' do
41
81
  expect(@db.first(WithStubbedView.stubbed_view('123'))).to eq(:result)
42
82
  expect(@db.first!(WithStubbedView.stubbed_view('123'))).to eq(:result)
@@ -96,3 +136,55 @@ describe 'stubbing a view' do
96
136
  expect(@db.view(WithStubbedView.stubbed_view('123'))).to eq(:results)
97
137
  end
98
138
  end
139
+
140
+ describe 'stubbing a flex view' do
141
+ before(:each) do
142
+ @db = CouchPotato.stub_db
143
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).with('123').and_return([:result])
144
+ end
145
+
146
+ it 'stubs the database to return a results stub with docs when given an array' do
147
+ expect(@db.view(WithStubbedFlexView.stubbed_flex_view('123')).docs).to eq([:result])
148
+ end
149
+
150
+ it 'stubs the database to return the first fake result' do
151
+ expect(@db.first(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:result)
152
+ expect(@db.first!(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:result)
153
+ end
154
+
155
+ it 'returns successive results stubs on consecutive view calls' do
156
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).with('123').and_return([:first], [:second])
157
+
158
+ expect(@db.view(WithStubbedFlexView.stubbed_flex_view('123')).docs).to eq([:first])
159
+ expect(@db.view(WithStubbedFlexView.stubbed_flex_view('123')).docs).to eq([:second])
160
+ end
161
+
162
+ it 'returns successive first values on consecutive first calls' do
163
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).with('123').and_return([:a], [:b])
164
+
165
+ expect(@db.first(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:a)
166
+ expect(@db.first(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:b)
167
+ end
168
+
169
+ it 'returns successive values on consecutive first! calls and raises for a nil first value' do
170
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).with('123').and_return([:a], [], [:b])
171
+
172
+ expect(@db.first!(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:a)
173
+ expect { @db.first!(WithStubbedFlexView.stubbed_flex_view('123')) }.to raise_error(CouchPotato::NotFound)
174
+ expect(@db.first!(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:b)
175
+ end
176
+
177
+ it 'raises from db.first! when the array is empty' do
178
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).and_return([])
179
+
180
+ expect do
181
+ @db.first!(WithStubbedFlexView.stubbed_flex_view)
182
+ end.to raise_error(CouchPotato::NotFound)
183
+ end
184
+
185
+ it 'returns a non-array return value directly' do
186
+ @db.stub_view(WithStubbedFlexView, :stubbed_flex_view).with('123').and_return(:reduced)
187
+
188
+ expect(@db.view(WithStubbedFlexView.stubbed_flex_view('123'))).to eq(:reduced)
189
+ end
190
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_potato
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.22.0
4
+ version: 1.22.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lang