couchrest_model 2.1.0.rc1 → 2.2.0.beta1
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 +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +15 -4
- data/Gemfile.activesupport-4.x +4 -0
- data/Gemfile.activesupport-5.x +4 -0
- data/README.md +2 -0
- data/VERSION +1 -1
- data/couchrest_model.gemspec +3 -2
- data/history.md +14 -1
- data/lib/couchrest/model/associations.rb +3 -8
- data/lib/couchrest/model/base.rb +15 -7
- data/lib/couchrest/model/casted_array.rb +22 -34
- data/lib/couchrest/model/configuration.rb +2 -0
- data/lib/couchrest/model/design.rb +4 -3
- data/lib/couchrest/model/designs/view.rb +37 -32
- data/lib/couchrest/model/dirty.rb +93 -19
- data/lib/couchrest/model/embeddable.rb +2 -14
- data/lib/couchrest/model/extended_attachments.rb +2 -4
- data/lib/couchrest/model/persistence.rb +14 -17
- data/lib/couchrest/model/properties.rb +46 -54
- data/lib/couchrest/model/property.rb +0 -3
- data/lib/couchrest/model/proxyable.rb +20 -4
- data/lib/couchrest/model/validations/uniqueness.rb +4 -1
- data/lib/couchrest_model.rb +2 -2
- data/spec/fixtures/models/article.rb +1 -1
- data/spec/fixtures/models/card.rb +2 -1
- data/spec/fixtures/models/person.rb +1 -0
- data/spec/fixtures/models/project.rb +3 -0
- data/spec/unit/assocations_spec.rb +73 -73
- data/spec/unit/attachment_spec.rb +34 -34
- data/spec/unit/base_spec.rb +102 -102
- data/spec/unit/casted_array_spec.rb +7 -7
- data/spec/unit/casted_spec.rb +7 -7
- data/spec/unit/configuration_spec.rb +11 -11
- data/spec/unit/connection_spec.rb +30 -30
- data/spec/unit/core_extensions/{time_parsing.rb → time_parsing_spec.rb} +21 -21
- data/spec/unit/design_spec.rb +38 -38
- data/spec/unit/designs/design_mapper_spec.rb +26 -26
- data/spec/unit/designs/migrations_spec.rb +13 -13
- data/spec/unit/designs/view_spec.rb +319 -274
- data/spec/unit/designs_spec.rb +39 -39
- data/spec/unit/dirty_spec.rb +188 -103
- data/spec/unit/embeddable_spec.rb +119 -117
- data/spec/unit/inherited_spec.rb +4 -4
- data/spec/unit/persistence_spec.rb +122 -122
- data/spec/unit/properties_spec.rb +466 -16
- data/spec/unit/property_protection_spec.rb +32 -32
- data/spec/unit/property_spec.rb +45 -436
- data/spec/unit/proxyable_spec.rb +140 -82
- data/spec/unit/subclass_spec.rb +14 -14
- data/spec/unit/translations_spec.rb +5 -5
- data/spec/unit/typecast_spec.rb +131 -131
- data/spec/unit/utils/migrate_spec.rb +2 -2
- data/spec/unit/validations_spec.rb +31 -31
- metadata +27 -12
- data/lib/couchrest/model/casted_hash.rb +0 -84
@@ -5,27 +5,27 @@ describe "ActiveModel Translations" do
|
|
5
5
|
|
6
6
|
describe ".human_attribute_name" do
|
7
7
|
it "should provide translation" do
|
8
|
-
Card.human_attribute_name(:first_name).
|
8
|
+
expect(Card.human_attribute_name(:first_name)).to eql("First name")
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
describe ".i18n_scope" do
|
13
13
|
it "should provide activemodel default" do
|
14
|
-
Card.i18n_scope.
|
14
|
+
expect(Card.i18n_scope).to eql(:couchrest)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
describe ".lookup_ancestors" do
|
19
19
|
it "should provide basic lookup" do
|
20
|
-
Cat.lookup_ancestors.
|
20
|
+
expect(Cat.lookup_ancestors).to eql([Cat])
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should provide lookup with ancestors" do
|
24
|
-
ChildCat.lookup_ancestors.
|
24
|
+
expect(ChildCat.lookup_ancestors).to eql([ChildCat, Cat])
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should provide Base if request directly" do
|
28
|
-
CouchRest::Model::Base.lookup_ancestors.
|
28
|
+
expect(CouchRest::Model::Base.lookup_ancestors).to eql([CouchRest::Model::Base])
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/spec/unit/typecast_spec.rb
CHANGED
@@ -10,49 +10,49 @@ describe "Type Casting" do
|
|
10
10
|
describe "when value is nil" do
|
11
11
|
it "leaves the value unchanged" do
|
12
12
|
@course.title = nil
|
13
|
-
@course['title'].
|
13
|
+
expect(@course['title']).to eq(nil)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "when value is empty string" do
|
18
18
|
it "leaves the value unchanged" do
|
19
19
|
@course.title = ""
|
20
|
-
@course['title'].
|
20
|
+
expect(@course['title']).to eq("")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "when blank is not allow on property" do
|
25
25
|
it "leaves nil as nil" do
|
26
26
|
@course.subtitle = nil
|
27
|
-
@course['subtitle'].
|
27
|
+
expect(@course['subtitle']).to eq(nil)
|
28
28
|
end
|
29
29
|
it "converts blank to nil" do
|
30
30
|
@course.subtitle = ""
|
31
|
-
@course['subtitle'].
|
31
|
+
expect(@course['subtitle']).to eq(nil)
|
32
32
|
end
|
33
33
|
it "leaves text as text" do
|
34
34
|
@course.subtitle = "Test"
|
35
|
-
@course['subtitle'].
|
35
|
+
expect(@course['subtitle']).to eq("Test")
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
describe "when type primitive is an Object" do
|
40
40
|
it "it should not cast given value" do
|
41
41
|
@course.participants = [{}, 'q', 1]
|
42
|
-
@course['participants'].
|
42
|
+
expect(@course['participants']).to eq([{}, 'q', 1])
|
43
43
|
end
|
44
44
|
|
45
45
|
it "should cast started_on to Date" do
|
46
46
|
@course.started_on = Date.today
|
47
|
-
@course['started_on'].
|
47
|
+
expect(@course['started_on']).to be_an_instance_of(Date)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
describe "when class responds to .couchrest_typecast" do
|
52
52
|
it "should accept call" do
|
53
53
|
@course.price = "1299"
|
54
|
-
@course.price.cents.
|
55
|
-
@course.price.currency.
|
54
|
+
expect(@course.price.cents).to eql(1299)
|
55
|
+
expect(@course.price.currency).to eql('EUR')
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -60,12 +60,12 @@ describe "Type Casting" do
|
|
60
60
|
it "keeps string value unchanged" do
|
61
61
|
value = "1.0"
|
62
62
|
@course.title = value
|
63
|
-
@course['title'].
|
63
|
+
expect(@course['title']).to equal(value)
|
64
64
|
end
|
65
65
|
|
66
66
|
it "it casts to string representation of the value" do
|
67
67
|
@course.title = 1.0
|
68
|
-
@course['title'].
|
68
|
+
expect(@course['title']).to eql("1.0")
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
@@ -73,17 +73,17 @@ describe "Type Casting" do
|
|
73
73
|
it "keeps symbol value unchanged" do
|
74
74
|
value = :a_symbol
|
75
75
|
@course.symbol = value
|
76
|
-
@course['symbol'].
|
76
|
+
expect(@course['symbol']).to equal(:a_symbol)
|
77
77
|
end
|
78
78
|
|
79
79
|
it "it casts to symbol representation of the value" do
|
80
80
|
@course.symbol = "a_symbol"
|
81
|
-
@course['symbol'].
|
81
|
+
expect(@course['symbol']).to equal(:a_symbol)
|
82
82
|
end
|
83
83
|
|
84
84
|
it "turns blank value into nil" do
|
85
85
|
@course.symbol = ""
|
86
|
-
@course['symbol'].
|
86
|
+
expect(@course['symbol']).to be_nil
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
@@ -92,122 +92,122 @@ describe "Type Casting" do
|
|
92
92
|
it 'returns same value if a float' do
|
93
93
|
value = 24.0
|
94
94
|
@course.estimate = value
|
95
|
-
@course['estimate'].
|
95
|
+
expect(@course['estimate']).to equal(value)
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'returns float representation of a zero string integer' do
|
99
99
|
@course.estimate = '0'
|
100
|
-
@course['estimate'].
|
100
|
+
expect(@course['estimate']).to eql(0.0)
|
101
101
|
end
|
102
102
|
|
103
103
|
it 'returns float representation of a positive string integer' do
|
104
104
|
@course.estimate = '24'
|
105
|
-
@course['estimate'].
|
105
|
+
expect(@course['estimate']).to eql(24.0)
|
106
106
|
end
|
107
107
|
|
108
108
|
it 'returns float representation of a negative string integer' do
|
109
109
|
@course.estimate = '-24'
|
110
|
-
@course['estimate'].
|
110
|
+
expect(@course['estimate']).to eql(-24.0)
|
111
111
|
end
|
112
112
|
|
113
113
|
it 'returns float representation of a zero string float' do
|
114
114
|
@course.estimate = '0.0'
|
115
|
-
@course['estimate'].
|
115
|
+
expect(@course['estimate']).to eql(0.0)
|
116
116
|
end
|
117
117
|
|
118
118
|
it 'returns float representation of a positive string float' do
|
119
119
|
@course.estimate = '24.35'
|
120
|
-
@course['estimate'].
|
120
|
+
expect(@course['estimate']).to eql(24.35)
|
121
121
|
end
|
122
122
|
|
123
123
|
it 'returns float representation of a negative string float' do
|
124
124
|
@course.estimate = '-24.35'
|
125
|
-
@course['estimate'].
|
125
|
+
expect(@course['estimate']).to eql(-24.35)
|
126
126
|
end
|
127
127
|
|
128
128
|
it 'returns float representation of a zero string float, with no leading digits' do
|
129
129
|
@course.estimate = '.0'
|
130
|
-
@course['estimate'].
|
130
|
+
expect(@course['estimate']).to eql(0.0)
|
131
131
|
end
|
132
132
|
|
133
133
|
it 'returns float representation of a positive string float, with no leading digits' do
|
134
134
|
@course.estimate = '.41'
|
135
|
-
@course['estimate'].
|
135
|
+
expect(@course['estimate']).to eql(0.41)
|
136
136
|
end
|
137
137
|
|
138
138
|
it 'returns float representation of a zero integer' do
|
139
139
|
@course.estimate = 0
|
140
|
-
@course['estimate'].
|
140
|
+
expect(@course['estimate']).to eql(0.0)
|
141
141
|
end
|
142
142
|
|
143
143
|
it 'returns float representation of a positive integer' do
|
144
144
|
@course.estimate = 24
|
145
|
-
@course['estimate'].
|
145
|
+
expect(@course['estimate']).to eql(24.0)
|
146
146
|
end
|
147
147
|
|
148
148
|
it 'returns float representation of a negative integer' do
|
149
149
|
@course.estimate = -24
|
150
|
-
@course['estimate'].
|
150
|
+
expect(@course['estimate']).to eql(-24.0)
|
151
151
|
end
|
152
152
|
|
153
153
|
it 'returns float representation of a zero decimal' do
|
154
154
|
@course.estimate = BigDecimal('0.0')
|
155
|
-
@course['estimate'].
|
155
|
+
expect(@course['estimate']).to eql(0.0)
|
156
156
|
end
|
157
157
|
|
158
158
|
it 'returns float representation of a positive decimal' do
|
159
159
|
@course.estimate = BigDecimal('24.35')
|
160
|
-
@course['estimate'].
|
160
|
+
expect(@course['estimate']).to eql(24.35)
|
161
161
|
end
|
162
162
|
|
163
163
|
it 'returns float representation of a negative decimal' do
|
164
164
|
@course.estimate = BigDecimal('-24.35')
|
165
|
-
@course['estimate'].
|
165
|
+
expect(@course['estimate']).to eql(-24.35)
|
166
166
|
end
|
167
167
|
|
168
168
|
it 'return float of a number with commas instead of points for decimals' do
|
169
169
|
@course.estimate = '23,35'
|
170
|
-
@course['estimate'].
|
170
|
+
expect(@course['estimate']).to eql(23.35)
|
171
171
|
end
|
172
172
|
|
173
173
|
it "should handle numbers with commas and points" do
|
174
174
|
@course.estimate = '1,234.00'
|
175
|
-
@course.estimate.
|
175
|
+
expect(@course.estimate).to eql(1234.00)
|
176
176
|
end
|
177
177
|
|
178
178
|
it "should handle a mis-match of commas and points and maintain the last one" do
|
179
179
|
@course.estimate = "1,232.434.123,323"
|
180
|
-
@course.estimate.
|
180
|
+
expect(@course.estimate).to eql(1232434123.323)
|
181
181
|
end
|
182
182
|
|
183
183
|
it "should handle numbers with whitespace" do
|
184
184
|
@course.estimate = " 24.35 "
|
185
|
-
@course.estimate.
|
185
|
+
expect(@course.estimate).to eql(24.35)
|
186
186
|
end
|
187
187
|
|
188
188
|
it "should handle numbers with unit strings" do
|
189
189
|
@course.estimate = "23.21 points"
|
190
|
-
@course['estimate'].
|
190
|
+
expect(@course['estimate']).to eql(23.21)
|
191
191
|
end
|
192
192
|
|
193
193
|
[ '', 'string', ' foo ' ].each do |value|
|
194
194
|
it "should typecast string without a number to nil (#{value.inspect})" do
|
195
195
|
@course.estimate = value
|
196
|
-
@course['estimate'].
|
196
|
+
expect(@course['estimate']).to be_nil
|
197
197
|
end
|
198
198
|
end
|
199
199
|
|
200
200
|
[ '00.0', '0.', '-.0' ].each do |value|
|
201
201
|
it "should typecast strings with strange numbers to zero (#{value.inspect})" do
|
202
202
|
@course.estimate = value
|
203
|
-
@course['estimate'].
|
203
|
+
expect(@course['estimate']).to eql(0.0)
|
204
204
|
end
|
205
205
|
end
|
206
206
|
|
207
207
|
[ Object.new, true ].each do |value|
|
208
208
|
it "should not typecast non-numeric value that won't respond to #to_f (#{value.inspect})" do
|
209
209
|
@course.estimate = value
|
210
|
-
@course['estimate'].
|
210
|
+
expect(@course['estimate']).to equal(nil)
|
211
211
|
end
|
212
212
|
end
|
213
213
|
|
@@ -217,112 +217,112 @@ describe "Type Casting" do
|
|
217
217
|
it 'returns same value if an integer' do
|
218
218
|
value = 24
|
219
219
|
@course.hours = value
|
220
|
-
@course['hours'].
|
220
|
+
expect(@course['hours']).to equal(value)
|
221
221
|
end
|
222
222
|
|
223
223
|
it 'returns integer representation of a zero string integer' do
|
224
224
|
@course.hours = '0'
|
225
|
-
@course['hours'].
|
225
|
+
expect(@course['hours']).to eql(0)
|
226
226
|
end
|
227
227
|
|
228
228
|
it 'returns integer representation of a positive string integer' do
|
229
229
|
@course.hours = '24'
|
230
|
-
@course['hours'].
|
230
|
+
expect(@course['hours']).to eql(24)
|
231
231
|
end
|
232
232
|
|
233
233
|
it 'returns integer representation of a negative string integer' do
|
234
234
|
@course.hours = '-24'
|
235
|
-
@course['hours'].
|
235
|
+
expect(@course['hours']).to eql(-24)
|
236
236
|
end
|
237
237
|
|
238
238
|
it 'returns integer representation of a zero string float' do
|
239
239
|
@course.hours = '0.0'
|
240
|
-
@course['hours'].
|
240
|
+
expect(@course['hours']).to eql(0)
|
241
241
|
end
|
242
242
|
|
243
243
|
it 'returns integer representation of a positive string float' do
|
244
244
|
@course.hours = '24.35'
|
245
|
-
@course['hours'].
|
245
|
+
expect(@course['hours']).to eql(24)
|
246
246
|
end
|
247
247
|
|
248
248
|
it 'returns integer representation of a negative string float' do
|
249
249
|
@course.hours = '-24.35'
|
250
|
-
@course['hours'].
|
250
|
+
expect(@course['hours']).to eql(-24)
|
251
251
|
end
|
252
252
|
|
253
253
|
it 'returns integer representation of a zero string float, with no leading digits' do
|
254
254
|
@course.hours = '.0'
|
255
|
-
@course['hours'].
|
255
|
+
expect(@course['hours']).to eql(0)
|
256
256
|
end
|
257
257
|
|
258
258
|
it 'returns integer representation of a positive string float, with no leading digits' do
|
259
259
|
@course.hours = '.41'
|
260
|
-
@course['hours'].
|
260
|
+
expect(@course['hours']).to eql(0)
|
261
261
|
end
|
262
262
|
|
263
263
|
it 'returns integer representation of a zero float' do
|
264
264
|
@course.hours = 0.0
|
265
|
-
@course['hours'].
|
265
|
+
expect(@course['hours']).to eql(0)
|
266
266
|
end
|
267
267
|
|
268
268
|
it 'returns integer representation of a positive float' do
|
269
269
|
@course.hours = 24.35
|
270
|
-
@course['hours'].
|
270
|
+
expect(@course['hours']).to eql(24)
|
271
271
|
end
|
272
272
|
|
273
273
|
it 'returns integer representation of a negative float' do
|
274
274
|
@course.hours = -24.35
|
275
|
-
@course['hours'].
|
275
|
+
expect(@course['hours']).to eql(-24)
|
276
276
|
end
|
277
277
|
|
278
278
|
it 'returns integer representation of a zero decimal' do
|
279
279
|
@course.hours = '0.0'
|
280
|
-
@course['hours'].
|
280
|
+
expect(@course['hours']).to eql(0)
|
281
281
|
end
|
282
282
|
|
283
283
|
it 'returns integer representation of a positive decimal' do
|
284
284
|
@course.hours = '24.35'
|
285
|
-
@course['hours'].
|
285
|
+
expect(@course['hours']).to eql(24)
|
286
286
|
end
|
287
287
|
|
288
288
|
it 'returns integer representation of a negative decimal' do
|
289
289
|
@course.hours = '-24.35'
|
290
|
-
@course['hours'].
|
290
|
+
expect(@course['hours']).to eql(-24)
|
291
291
|
end
|
292
292
|
|
293
293
|
it "should handle numbers with whitespace" do
|
294
294
|
@course.hours = " 24 "
|
295
|
-
@course['hours'].
|
295
|
+
expect(@course['hours']).to eql(24)
|
296
296
|
end
|
297
297
|
|
298
298
|
it "should handle numbers with string units" do
|
299
299
|
@course.hours = "23 hours"
|
300
|
-
@course['hours'].
|
300
|
+
expect(@course['hours']).to eql(23)
|
301
301
|
end
|
302
302
|
|
303
303
|
it "should typecast an empty string to nil" do
|
304
304
|
@course.hours = ""
|
305
|
-
@course['hours'].
|
305
|
+
expect(@course['hours']).to be_nil
|
306
306
|
end
|
307
307
|
|
308
308
|
[ '', 'string', ' foo ' ].each do |value|
|
309
309
|
it "should typecast string without a number to nil (#{value.inspect})" do
|
310
310
|
@course.hours = value
|
311
|
-
@course['hours'].
|
311
|
+
expect(@course['hours']).to be_nil
|
312
312
|
end
|
313
313
|
end
|
314
314
|
|
315
315
|
[ '00.0', '0.', '-.0' ].each do |value|
|
316
316
|
it "should typecast strings with strange numbers to zero (#{value.inspect})" do
|
317
317
|
@course.hours = value
|
318
|
-
@course['hours'].
|
318
|
+
expect(@course['hours']).to eql(0)
|
319
319
|
end
|
320
320
|
end
|
321
321
|
|
322
322
|
[ Object.new, true ].each do |value|
|
323
323
|
it "should not typecast non-numeric value that won't respond to #to_i (#{value.inspect})" do
|
324
324
|
@course.hours = value
|
325
|
-
@course['hours'].
|
325
|
+
expect(@course['hours']).to equal(nil)
|
326
326
|
end
|
327
327
|
end
|
328
328
|
|
@@ -332,112 +332,112 @@ describe "Type Casting" do
|
|
332
332
|
it 'returns same value if a decimal' do
|
333
333
|
value = BigDecimal('24.0')
|
334
334
|
@course.profit = value
|
335
|
-
@course['profit'].
|
335
|
+
expect(@course['profit']).to equal(value)
|
336
336
|
end
|
337
337
|
|
338
338
|
it 'returns decimal representation of a zero string integer' do
|
339
339
|
@course.profit = '0'
|
340
|
-
@course['profit'].
|
340
|
+
expect(@course['profit']).to eql(BigDecimal('0.0'))
|
341
341
|
end
|
342
342
|
|
343
343
|
it 'returns decimal representation of a positive string integer' do
|
344
344
|
@course.profit = '24'
|
345
|
-
@course['profit'].
|
345
|
+
expect(@course['profit']).to eql(BigDecimal('24.0'))
|
346
346
|
end
|
347
347
|
|
348
348
|
it 'returns decimal representation of a negative string integer' do
|
349
349
|
@course.profit = '-24'
|
350
|
-
@course['profit'].
|
350
|
+
expect(@course['profit']).to eql(BigDecimal('-24.0'))
|
351
351
|
end
|
352
352
|
|
353
353
|
it 'returns decimal representation of a zero string float' do
|
354
354
|
@course.profit = '0.0'
|
355
|
-
@course['profit'].
|
355
|
+
expect(@course['profit']).to eql(BigDecimal('0.0'))
|
356
356
|
end
|
357
357
|
|
358
358
|
it 'returns decimal representation of a positive string float' do
|
359
359
|
@course.profit = '24.35'
|
360
|
-
@course['profit'].
|
360
|
+
expect(@course['profit']).to eql(BigDecimal('24.35'))
|
361
361
|
end
|
362
362
|
|
363
363
|
it 'returns decimal representation of a negative string float' do
|
364
364
|
@course.profit = '-24.35'
|
365
|
-
@course['profit'].
|
365
|
+
expect(@course['profit']).to eql(BigDecimal('-24.35'))
|
366
366
|
end
|
367
367
|
|
368
368
|
it 'returns decimal representation of a zero string float, with no leading digits' do
|
369
369
|
@course.profit = '.0'
|
370
|
-
@course['profit'].
|
370
|
+
expect(@course['profit']).to eql(BigDecimal('0.0'))
|
371
371
|
end
|
372
372
|
|
373
373
|
it 'returns decimal representation of a positive string float, with no leading digits' do
|
374
374
|
@course.profit = '.41'
|
375
|
-
@course['profit'].
|
375
|
+
expect(@course['profit']).to eql(BigDecimal('0.41'))
|
376
376
|
end
|
377
377
|
|
378
378
|
it 'returns decimal representation of a zero integer' do
|
379
379
|
@course.profit = 0
|
380
|
-
@course['profit'].
|
380
|
+
expect(@course['profit']).to eql(BigDecimal('0.0'))
|
381
381
|
end
|
382
382
|
|
383
383
|
it 'returns decimal representation of a positive integer' do
|
384
384
|
@course.profit = 24
|
385
|
-
@course['profit'].
|
385
|
+
expect(@course['profit']).to eql(BigDecimal('24.0'))
|
386
386
|
end
|
387
387
|
|
388
388
|
it 'returns decimal representation of a negative integer' do
|
389
389
|
@course.profit = -24
|
390
|
-
@course['profit'].
|
390
|
+
expect(@course['profit']).to eql(BigDecimal('-24.0'))
|
391
391
|
end
|
392
392
|
|
393
393
|
it 'returns decimal representation of a zero float' do
|
394
394
|
@course.profit = 0.0
|
395
|
-
@course['profit'].
|
395
|
+
expect(@course['profit']).to eql(BigDecimal('0.0'))
|
396
396
|
end
|
397
397
|
|
398
398
|
it 'returns decimal representation of a positive float' do
|
399
399
|
@course.profit = 24.35
|
400
|
-
@course['profit'].
|
400
|
+
expect(@course['profit']).to eql(BigDecimal('24.35'))
|
401
401
|
end
|
402
402
|
|
403
403
|
it 'returns decimal representation of a negative float' do
|
404
404
|
@course.profit = -24.35
|
405
|
-
@course['profit'].
|
405
|
+
expect(@course['profit']).to eql(BigDecimal('-24.35'))
|
406
406
|
end
|
407
407
|
|
408
408
|
it "should handle numbers with whitespace" do
|
409
409
|
@course.profit = " 24.35 "
|
410
|
-
@course['profit'].
|
410
|
+
expect(@course['profit']).to eql(BigDecimal('24.35'))
|
411
411
|
end
|
412
412
|
|
413
413
|
it "should handle numbers with strings" do
|
414
414
|
@course.profit = "22.23 euros"
|
415
|
-
@course['profit'].
|
415
|
+
expect(@course['profit']).to eql(BigDecimal('22.23'))
|
416
416
|
end
|
417
417
|
|
418
418
|
it "should typecast an empty string to nil" do
|
419
419
|
@course.profit = ""
|
420
|
-
@course['profit'].
|
420
|
+
expect(@course['profit']).to be_nil
|
421
421
|
end
|
422
422
|
|
423
423
|
[ '', 'string', ' foo ' ].each do |value|
|
424
424
|
it "should typecast string without a number to nil (#{value.inspect})" do
|
425
425
|
@course.profit = value
|
426
|
-
@course['profit'].
|
426
|
+
expect(@course['profit']).to be_nil
|
427
427
|
end
|
428
428
|
end
|
429
429
|
|
430
430
|
[ '00.0', '0.', '-.0' ].each do |value|
|
431
431
|
it "should typecast strings with strange numbers to zero (#{value.inspect})" do
|
432
432
|
@course.profit = value
|
433
|
-
@course['profit'].
|
433
|
+
expect(@course['profit']).to eql(0.0)
|
434
434
|
end
|
435
435
|
end
|
436
436
|
|
437
437
|
[ Object.new, true ].each do |value|
|
438
438
|
it "should typecast non-numeric value that won't respond to to_d (#{value.inspect}) as nil" do
|
439
439
|
@course.profit = value
|
440
|
-
@course['profit'].
|
440
|
+
expect(@course['profit']).to equal(nil)
|
441
441
|
end
|
442
442
|
end
|
443
443
|
|
@@ -456,26 +456,26 @@ describe "Type Casting" do
|
|
456
456
|
}
|
457
457
|
result = @course['updated_at']
|
458
458
|
|
459
|
-
result.
|
460
|
-
result.year.
|
461
|
-
result.month.
|
462
|
-
result.day.
|
463
|
-
result.hour.
|
464
|
-
result.min.
|
465
|
-
result.sec.
|
459
|
+
expect(result).to be_kind_of(DateTime)
|
460
|
+
expect(result.year).to eql(2006)
|
461
|
+
expect(result.month).to eql(11)
|
462
|
+
expect(result.day).to eql(23)
|
463
|
+
expect(result.hour).to eql(12)
|
464
|
+
expect(result.min).to eql(0)
|
465
|
+
expect(result.sec).to eql(0)
|
466
466
|
end
|
467
467
|
end
|
468
468
|
|
469
469
|
describe 'and value is a string' do
|
470
470
|
it 'parses the string' do
|
471
471
|
@course.updated_at = 'Dec, 2006'
|
472
|
-
@course['updated_at'].month.
|
472
|
+
expect(@course['updated_at'].month).to eq(12)
|
473
473
|
end
|
474
474
|
end
|
475
475
|
|
476
476
|
it 'does not typecast non-datetime values' do
|
477
477
|
@course.updated_at = 'not-datetime'
|
478
|
-
@course['updated_at'].
|
478
|
+
expect(@course['updated_at']).to be_nil
|
479
479
|
end
|
480
480
|
end
|
481
481
|
|
@@ -489,25 +489,25 @@ describe "Type Casting" do
|
|
489
489
|
}
|
490
490
|
result = @course['started_on']
|
491
491
|
|
492
|
-
result.
|
493
|
-
result.year.
|
494
|
-
result.month.
|
495
|
-
result.day.
|
492
|
+
expect(result).to be_kind_of(Date)
|
493
|
+
expect(result.year).to eql(2007)
|
494
|
+
expect(result.month).to eql(3)
|
495
|
+
expect(result.day).to eql(25)
|
496
496
|
end
|
497
497
|
end
|
498
498
|
|
499
499
|
describe 'and value is a string' do
|
500
500
|
it 'parses the string' do
|
501
501
|
@course.started_on = 'Dec 20th, 2006'
|
502
|
-
@course.started_on.month.
|
503
|
-
@course.started_on.day.
|
504
|
-
@course.started_on.year.
|
502
|
+
expect(@course.started_on.month).to eq(12)
|
503
|
+
expect(@course.started_on.day).to eq(20)
|
504
|
+
expect(@course.started_on.year).to eq(2006)
|
505
505
|
end
|
506
506
|
end
|
507
507
|
|
508
508
|
it 'does not typecast non-date values' do
|
509
509
|
@course.started_on = 'not-date'
|
510
|
-
@course['started_on'].
|
510
|
+
expect(@course['started_on']).to be_nil
|
511
511
|
end
|
512
512
|
end
|
513
513
|
|
@@ -524,13 +524,13 @@ describe "Type Casting" do
|
|
524
524
|
}
|
525
525
|
result = @course['ends_at']
|
526
526
|
|
527
|
-
result.
|
528
|
-
result.year.
|
529
|
-
result.month.
|
530
|
-
result.day.
|
531
|
-
result.hour.
|
532
|
-
result.min.
|
533
|
-
result.sec.
|
527
|
+
expect(result).to be_kind_of(Time)
|
528
|
+
expect(result.year).to eql(2006)
|
529
|
+
expect(result.month).to eql(11)
|
530
|
+
expect(result.day).to eql(23)
|
531
|
+
expect(result.hour).to eql(12)
|
532
|
+
expect(result.min).to eql(0)
|
533
|
+
expect(result.sec).to eql(0)
|
534
534
|
end
|
535
535
|
end
|
536
536
|
|
@@ -538,36 +538,36 @@ describe "Type Casting" do
|
|
538
538
|
it 'parses the string' do
|
539
539
|
t = Time.new(2011, 4, 1, 18, 50, 32, "+02:00")
|
540
540
|
@course.ends_at = t.strftime('%Y/%m/%d %H:%M:%S %z')
|
541
|
-
@course['ends_at'].year.
|
542
|
-
@course['ends_at'].month.
|
543
|
-
@course['ends_at'].day.
|
544
|
-
@course['ends_at'].hour.
|
545
|
-
@course['ends_at'].min.
|
546
|
-
@course['ends_at'].sec.
|
541
|
+
expect(@course['ends_at'].year).to eql(t.year)
|
542
|
+
expect(@course['ends_at'].month).to eql(t.month)
|
543
|
+
expect(@course['ends_at'].day).to eql(t.day)
|
544
|
+
expect(@course['ends_at'].hour).to eql(t.hour)
|
545
|
+
expect(@course['ends_at'].min).to eql(t.min)
|
546
|
+
expect(@course['ends_at'].sec).to eql(t.sec)
|
547
547
|
end
|
548
548
|
it 'parses the string without offset as UTC' do
|
549
549
|
t = Time.now.utc
|
550
550
|
@course.ends_at = t.strftime("%Y-%m-%d %H:%M:%S")
|
551
|
-
@course.ends_at.utc
|
552
|
-
@course['ends_at'].year.
|
553
|
-
@course['ends_at'].month.
|
554
|
-
@course['ends_at'].day.
|
555
|
-
@course['ends_at'].hour.
|
556
|
-
@course['ends_at'].min.
|
557
|
-
@course['ends_at'].sec.
|
551
|
+
expect(@course.ends_at.utc?).to be_truthy
|
552
|
+
expect(@course['ends_at'].year).to eql(t.year)
|
553
|
+
expect(@course['ends_at'].month).to eql(t.month)
|
554
|
+
expect(@course['ends_at'].day).to eql(t.day)
|
555
|
+
expect(@course['ends_at'].hour).to eql(t.hour)
|
556
|
+
expect(@course['ends_at'].min).to eql(t.min)
|
557
|
+
expect(@course['ends_at'].sec).to eql(t.sec)
|
558
558
|
end
|
559
559
|
end
|
560
560
|
|
561
561
|
it "converts a time value into utc" do
|
562
562
|
t = Time.new(2011, 4, 1, 18, 50, 32, "+02:00")
|
563
563
|
@course.ends_at = t
|
564
|
-
@course.ends_at.utc
|
565
|
-
@course.ends_at.to_i.
|
564
|
+
expect(@course.ends_at.utc?).to be_truthy
|
565
|
+
expect(@course.ends_at.to_i).to eql(Time.utc(2011, 4, 1, 16, 50, 32).to_i)
|
566
566
|
end
|
567
567
|
|
568
568
|
it 'does not typecast non-time values' do
|
569
569
|
@course.ends_at = 'not-time'
|
570
|
-
@course['ends_at'].
|
570
|
+
expect(@course['ends_at']).to be_nil
|
571
571
|
end
|
572
572
|
end
|
573
573
|
|
@@ -575,17 +575,17 @@ describe "Type Casting" do
|
|
575
575
|
it 'returns same value if a class' do
|
576
576
|
value = Course
|
577
577
|
@course.klass = value
|
578
|
-
@course['klass'].
|
578
|
+
expect(@course['klass']).to equal(value)
|
579
579
|
end
|
580
580
|
|
581
581
|
it 'returns the class if found' do
|
582
582
|
@course.klass = 'Course'
|
583
|
-
@course['klass'].
|
583
|
+
expect(@course['klass']).to eql(Course)
|
584
584
|
end
|
585
585
|
|
586
586
|
it 'does not typecast non-class values' do
|
587
587
|
@course.klass = 'NoClass'
|
588
|
-
@course['klass'].
|
588
|
+
expect(@course['klass']).to be_nil
|
589
589
|
end
|
590
590
|
end
|
591
591
|
|
@@ -594,40 +594,40 @@ describe "Type Casting" do
|
|
594
594
|
[ true, 'true', 'TRUE', '1', 1, 't', 'T' ].each do |value|
|
595
595
|
it "returns true when value is #{value.inspect}" do
|
596
596
|
@course.active = value
|
597
|
-
@course['active'].
|
597
|
+
expect(@course['active']).to be_truthy
|
598
598
|
end
|
599
599
|
end
|
600
600
|
|
601
601
|
[ false, 'false', 'FALSE', '0', 0, 'f', 'F' ].each do |value|
|
602
602
|
it "returns false when value is #{value.inspect}" do
|
603
603
|
@course.active = value
|
604
|
-
@course['active'].
|
604
|
+
expect(@course['active']).to be_falsey
|
605
605
|
end
|
606
606
|
end
|
607
607
|
|
608
608
|
[ 'string', 2, 1.0, BigDecimal('1.0'), DateTime.now, Time.now, Date.today, Class, Object.new, ].each do |value|
|
609
609
|
it "does not typecast value #{value.inspect}" do
|
610
610
|
@course.active = value
|
611
|
-
@course['active'].
|
611
|
+
expect(@course['active']).to be_nil
|
612
612
|
end
|
613
613
|
end
|
614
614
|
|
615
615
|
it "should respond to requests with ? modifier" do
|
616
616
|
@course.active = nil
|
617
|
-
@course.active
|
617
|
+
expect(@course.active?).to be_falsey
|
618
618
|
@course.active = false
|
619
|
-
@course.active
|
619
|
+
expect(@course.active?).to be_falsey
|
620
620
|
@course.active = true
|
621
|
-
@course.active
|
621
|
+
expect(@course.active?).to be_truthy
|
622
622
|
end
|
623
623
|
|
624
624
|
it "should respond to requests with ? modifier on TrueClass" do
|
625
625
|
@course.very_active = nil
|
626
|
-
@course.very_active
|
626
|
+
expect(@course.very_active?).to be_falsey
|
627
627
|
@course.very_active = false
|
628
|
-
@course.very_active
|
628
|
+
expect(@course.very_active?).to be_falsey
|
629
629
|
@course.very_active = true
|
630
|
-
@course.very_active
|
630
|
+
expect(@course.very_active?).to be_truthy
|
631
631
|
end
|
632
632
|
end
|
633
633
|
|