split_dmy 0.2 → 0.3

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
  SHA1:
3
- metadata.gz: b1cc272431001c8df28f5868fc48099d0c6e9687
4
- data.tar.gz: 6788d518f913b907599e6f774d5964cf9d2d481c
3
+ metadata.gz: 77b51777ddac980e96091125b2a9c91944a47211
4
+ data.tar.gz: edbc9e21a698aa1e96a0b680c6d2ca5021ab4814
5
5
  SHA512:
6
- metadata.gz: eaac8f937162d0f5ef4e36e885bbe29ac511afa105c80b028c2a3e9e78b395eb85c46b26b0f68af1805aeef4b0fc6ef8407366143576a588d27525c5234f2761
7
- data.tar.gz: 65786e12689aa4b12199c99b8c56330b80e731de49d15299307d79e077ed521cd79b272b3a4f8229d8df4abdc4fcc630b206a09c018444ce73d741e98389bd1d
6
+ metadata.gz: 827fe90bd6339930a704f8862b7d73d0f56a7bf9fe57f235d102007bff323ba2fdb3667ce6261ddb75f2eb2dea443b9463c366d5c7bd0f3ebc49cddd18c40ac3
7
+ data.tar.gz: 55b434c4d90e11a60923489d9abdda47d2a5d829ebf917469c78c417d091a81b0e1edb9e33bbf6e8478c1c0dcf92ec52ad343c92dfe167f777d69adcfdaad731
@@ -9,8 +9,8 @@ module SplitDmy
9
9
  end
10
10
 
11
11
  def partial_updated
12
- bd = build_date
13
- partials_valid? && bd ? bd : nil
12
+ new_date = build_date
13
+ partials_valid? && new_date ? new_date : nil
14
14
  end
15
15
 
16
16
  def parse_month(val)
@@ -40,16 +40,8 @@ module SplitDmy
40
40
  false
41
41
  end
42
42
 
43
- def generate_errors
44
- %w[day month year].each do |part|
45
- error = get_partial_error(part)
46
- @object.errors.add("#{@attr}_#{part}".to_sym, error) if error.present?
47
- end
48
-
49
- err = []
50
- err << 'you need to provide a valid date' if all_partials_empty?
51
- err << combine_partials_error if partials_valid_date_fails?
52
- err
43
+ def any_errors?
44
+ !partials_valid? || partials_valid_date_fails?
53
45
  end
54
46
 
55
47
  private
@@ -62,11 +54,6 @@ module SplitDmy
62
54
  partials_valid? && !build_date
63
55
  end
64
56
 
65
- def combine_partials_error
66
- joined_date = [@split_day, @split_month, @split_year].join('-')
67
- "'#{joined_date}' is not a valid date"
68
- end
69
-
70
57
  def all_partials_empty?
71
58
  [@split_day.empty?, @split_month.empty?, @split_year.empty?].values.all?
72
59
  rescue
@@ -18,8 +18,7 @@ module SplitDmy
18
18
  def extend_validation(attr)
19
19
  define_method("validate_#{attr}_partials") do
20
20
  dv = DateValidator.new(self, attr)
21
- new_errs = dv.generate_errors
22
- unless new_errs.empty?
21
+ if dv.any_errors?
23
22
  errors.delete(attr.to_sym)
24
23
  errors.add(attr.to_sym, :invalid)
25
24
  end
@@ -44,10 +43,6 @@ module SplitDmy
44
43
  instance_variable_set("@#{attr}_month", full_date.month)
45
44
  instance_variable_set("@#{attr}_year", full_date.year)
46
45
  end
47
-
48
- define_method('make_sentence_of') do |errors|
49
- errors.to_sentence(last_word_connector: ' and ')
50
- end
51
46
  end
52
47
 
53
48
  def add_attr_accessors(attr)
@@ -1,3 +1,3 @@
1
1
  module SplitDmy
2
- VERSION = '0.2'.freeze
2
+ VERSION = '0.3'.freeze
3
3
  end
@@ -3,9 +3,14 @@ require 'split_dmy/split_accessors'
3
3
  require_relative '../../spec/support/matchers/accessors_shared'
4
4
 
5
5
  describe SplitDmy::SplitAccessors do
6
- let(:model) { User.new }
6
+ let(:date_of_birth) { nil }
7
+ let(:model) do
8
+ User.extend(described_class)
9
+ User.split_dmy_accessor(:date_of_birth)
10
+ User.new(date_of_birth: date_of_birth)
11
+ end
7
12
 
8
- describe 'test model' do
13
+ describe 'the test model' do
9
14
  it 'has a date of birth field to extend' do
10
15
  expect(model).to respond_to :date_of_birth
11
16
  end
@@ -14,329 +19,444 @@ describe SplitDmy::SplitAccessors do
14
19
  end
15
20
  end
16
21
 
17
- describe 'after adding our gem, a model' do
18
- before do
19
- User.extend(described_class)
20
- User.split_dmy_accessor(:date_of_birth)
22
+ ['day', 'month', 'year'].each do |part|
23
+ it "responds to attribute_#{part}" do
24
+ expect(model).to respond_to "date_of_birth_#{part}".to_sym
25
+ end
26
+
27
+ it "has split accessor for attribute_#{part}" do
28
+ expect(model).to have_attr_accessor "date_of_birth_#{part}".to_sym
21
29
  end
30
+ end
31
+
32
+ context 'when date is pre-filled' do
33
+ let(:date_of_birth) { Date.new(1992, 1, 21) }
34
+
35
+ describe 'it splits the partials' do
36
+ it 'sets the day' do
37
+ expect(model.date_of_birth_day).to eq 21
38
+ end
22
39
 
23
- ['day', 'month', 'year'].each do |part|
24
- it "responds to attribute_#{part}" do
25
- expect(model).to respond_to "date_of_birth_#{part}".to_sym
40
+ it 'sets the month' do
41
+ expect(model.date_of_birth_month).to eq 1
26
42
  end
27
43
 
28
- it "has split accessor for attribute_#{part}" do
29
- expect(model).to have_attr_accessor "date_of_birth_#{part}".to_sym
44
+ it 'sets the year' do
45
+ expect(model.date_of_birth_year).to eq 1992
30
46
  end
31
47
  end
32
48
 
33
- describe 'split dmy methods' do
34
- describe 'handles pre-set dates' do
35
- describe 'passes them to the new accessors' do
36
- before(:each) { model.date_of_birth = '1991-1-21' }
49
+ describe 'setting the _day' do
50
+ before { model.date_of_birth_day = dob_day }
51
+ describe 'when sent numbers' do
52
+ (1..31).each do |i|
53
+ describe 'between 1 and 31 ' do
54
+ let(:dob_day) { i }
55
+ before { model.valid? }
56
+
57
+ it 'sets the value' do
58
+ expect(model.date_of_birth_day).to eq i
59
+ end
60
+
61
+ it 'sets the date' do
62
+ expect(model.date_of_birth).to eq Date.new(1992, 1, i)
63
+ end
37
64
 
38
- it 'sets the _day' do
39
- expect(model.date_of_birth_day).to eq 21
65
+ it 'makes the model valid' do
66
+ expect(model).to be_valid
67
+ end
68
+
69
+ it 'returns no errors' do
70
+ expect(model.errors.count).to eq 0
71
+ end
40
72
  end
41
73
  end
74
+ end
42
75
 
43
- context 'when object created with date' do
44
- let(:model) { User.new(date_of_birth: Date.new(1992, 1, 21)) }
76
+ describe 'to text' do
77
+ let(:dob_day) { 'first' }
45
78
 
46
- it 'sets the date' do
47
- expect(model.date_of_birth).to eq Date.new(1992, 1, 21)
48
- end
79
+ before { model.valid? }
80
+
81
+ it 'sets the value' do
82
+ expect(model.date_of_birth_day).to eq 'first'
83
+ end
84
+
85
+ it 'sets the date to nil' do
86
+ expect(model.date_of_birth).to eq nil
87
+ end
88
+
89
+ it 'makes the model invalid' do
90
+ expect(model).to be_invalid
91
+ end
92
+
93
+ it 'sets the error to invalid' do
94
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
95
+ end
96
+ end
97
+
98
+ describe 'to too low a value' do
99
+ let(:dob_day) { 0 }
100
+
101
+ before { model.valid? }
102
+
103
+ it 'sets the value' do
104
+ expect(model.date_of_birth_day).to eq 0
105
+ end
106
+
107
+ it 'sets the date to nil' do
108
+ expect(model.date_of_birth).to eq nil
109
+ end
110
+
111
+ it 'makes the model invalid' do
112
+ expect(model).to be_invalid
113
+ end
114
+
115
+ it 'sets the error to invalid' do
116
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
117
+ end
118
+ end
119
+
120
+ describe 'to too high a value' do
121
+ let(:dob_day) { 32 }
122
+
123
+ before { model.valid? }
124
+
125
+ it 'sets the value' do
126
+ expect(model.date_of_birth_day).to eq 32
127
+ end
128
+
129
+ it 'sets the date to nil' do
130
+ expect(model.date_of_birth).to eq nil
131
+ end
49
132
 
50
- describe 'splits the partials' do
51
- it 'sets the day' do
52
- expect(model.date_of_birth_day).to eq 21
133
+ it 'makes the model invalid' do
134
+ expect(model).to be_invalid
135
+ end
136
+
137
+ it 'sets the error to invalid' do
138
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
139
+ end
140
+ end
141
+
142
+ describe 'to nil' do
143
+ let(:dob_day) { nil }
144
+
145
+ before { model.valid? }
146
+
147
+ it 'sets the value' do
148
+ expect(model.date_of_birth_day).to eq nil
149
+ end
150
+
151
+ it 'sets the date to nil' do
152
+ expect(model.date_of_birth).to be nil
153
+ end
154
+
155
+ it 'makes the model invalid' do
156
+ expect(model).to be_invalid
157
+ end
158
+
159
+ it 'sets the error to invalid' do
160
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
161
+ end
162
+ end
163
+ end
164
+
165
+ describe 'setting the _month' do
166
+ before { model.date_of_birth_month = dob_month }
167
+
168
+ describe 'when sent text' do
169
+
170
+ I18n.t('date.month_names').each_with_index do |month, _index|
171
+ next if month.nil?
172
+ describe 'of long month names' do
173
+ let(:dob_month) { model.date_of_birth_month = month }
174
+
175
+ before { model.valid? }
176
+
177
+ it { expect(model.date_of_birth_month).to eq Date::MONTHNAMES.index(month) }
178
+
179
+ it 'sets the date to nil' do
180
+ expect(model.date_of_birth).to eq Date.new(1992, Date::MONTHNAMES.index(month), 21)
53
181
  end
54
182
 
55
- it 'sets the month' do
56
- expect(model.date_of_birth_month).to eq 1
183
+ it 'makes the model valid' do
184
+ expect(model).to be_valid
57
185
  end
58
186
 
59
- it 'sets the year' do
60
- expect(model.date_of_birth_year).to eq 1992
187
+ it 'returns no errors' do
188
+ expect(model.errors.count).to eq 0
61
189
  end
62
190
  end
63
191
  end
64
192
 
65
- describe 'allows clearing of preset data' do
66
- before(:each) { model.date_of_birth = '1991-1-21' }
193
+ I18n.t('date.abbr_month_names').each_with_index do |month, _index|
194
+ next if month.nil?
195
+ describe 'of long month names' do
196
+ let(:dob_month) { model.date_of_birth_month = month }
67
197
 
68
- it 'by setting all values to nil' do
69
- model.date_of_birth_day = nil
70
- model.date_of_birth_month = nil
71
- model.date_of_birth_year = nil
72
- expect(model.date_of_birth).to be_nil
73
- end
198
+ before { model.valid? }
74
199
 
75
- describe 'by setting' do
76
- it 'day to nil' do
77
- model.date_of_birth_day = nil
78
- expect(model.date_of_birth).to be_nil
200
+ it { expect(model.date_of_birth_month).to eq Date::ABBR_MONTHNAMES.index(month) }
201
+
202
+ it 'sets the date to nil' do
203
+ expect(model.date_of_birth).to eq Date.new(1992, Date::ABBR_MONTHNAMES.index(month), 21)
79
204
  end
80
- it 'month to nil' do
81
- model.date_of_birth_month = nil
82
- expect(model.date_of_birth).to be_nil
205
+
206
+ it 'makes the model valid' do
207
+ expect(model).to be_valid
83
208
  end
84
- it 'year to nil' do
85
- model.date_of_birth_year = nil
86
- expect(model.date_of_birth).to be_nil
209
+
210
+ it 'returns no errors' do
211
+ expect(model.errors.count).to eq 0
87
212
  end
88
213
  end
89
214
  end
215
+
216
+ describe 'that is not a valid month' do
217
+ let(:dob_month) { 'first' }
218
+
219
+ before { model.valid? }
220
+
221
+ it 'sets the value' do
222
+ expect(model.date_of_birth_month).to eq 'first'
223
+ end
224
+
225
+ it 'sets the date to nil' do
226
+ expect(model.date_of_birth).to eq nil
227
+ end
228
+
229
+ it 'makes the model invalid' do
230
+ expect(model).to be_invalid
231
+ end
232
+
233
+ it 'sets the error to invalid' do
234
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
235
+ end
236
+ end
90
237
  end
91
238
 
92
- describe '_day' do
93
- describe 'when sent numbers' do
94
- describe 'between 1 and 31 is valid' do
239
+ describe 'when sent numbers' do
240
+ (1..12).each do |i|
241
+ describe 'between 1 and 12 ' do
242
+ let(:dob_month) { i }
243
+ before { model.valid? }
244
+
95
245
  it 'sets the value' do
96
- (1..31).each do |i|
97
- model.date_of_birth_day = i
98
- expect(model.date_of_birth_day).to eq i
99
- end
246
+ expect(model.date_of_birth_month).to eq i
100
247
  end
101
- end
102
248
 
103
- describe 'above accepted range' do
104
- before do
105
- model.date_of_birth_day = 32
106
- model.valid?
249
+ it 'sets the date' do
250
+ expect(model.date_of_birth).to eq Date.new(1992, i, 21)
107
251
  end
108
252
 
109
- it 'sets the value' do
110
- expect(model.date_of_birth_day).to eq 32
253
+ it 'makes the model valid' do
254
+ expect(model).to be_valid
111
255
  end
112
256
 
113
- it 'adds an error message' do
114
- err_msg = ['is not a valid day']
115
- expect(model.errors[:date_of_birth_day]).to eq err_msg
257
+ it 'returns no errors' do
258
+ expect(model.errors.count).to eq 0
116
259
  end
117
260
  end
261
+ end
118
262
 
119
- describe 'below accepted range' do
120
- before { model.date_of_birth_day = 0 }
263
+ describe 'to too high a value' do
264
+ let(:dob_month) { 13 }
121
265
 
122
- it 'sets the value' do
123
- expect(model.date_of_birth_day).to eq 0
124
- end
266
+ before { model.valid? }
125
267
 
126
- it 'makes the model invalid' do
127
- expect(model).to be_invalid
128
- end
268
+ it 'sets the value' do
269
+ expect(model.date_of_birth_month).to eq 13
270
+ end
271
+
272
+ it 'sets the date to nil' do
273
+ expect(model.date_of_birth).to eq nil
274
+ end
275
+
276
+ it 'makes the model invalid' do
277
+ expect(model).to be_invalid
278
+ end
279
+
280
+ it 'sets the error to invalid' do
281
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
129
282
  end
130
283
  end
131
284
 
132
- describe 'when sent text' do
133
- before { model.date_of_birth_day = 'first' }
285
+ describe 'to too low a value' do
286
+ let(:dob_month) { 0 }
287
+
288
+ before { model.valid? }
134
289
 
135
- it 'returns nil' do
136
- expect(model.date_of_birth_day).to eq 'first'
290
+ it 'sets the value' do
291
+ expect(model.date_of_birth_month).to eq 0
292
+ end
293
+
294
+ it 'sets the date to nil' do
295
+ expect(model.date_of_birth).to eq nil
137
296
  end
138
297
 
139
298
  it 'makes the model invalid' do
140
299
  expect(model).to be_invalid
141
300
  end
142
- end
143
301
 
144
- describe 'when sent nil' do
145
- it 'returns nil' do
146
- model.date_of_birth_day = nil
147
- expect(model.date_of_birth_day).to be_nil
302
+ it 'sets the error to invalid' do
303
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
148
304
  end
149
305
  end
150
306
  end
151
307
 
152
- describe '_month' do
153
- describe 'when sent numbers' do
154
- describe 'between 1 and 31 is valid' do
155
- it 'sets the value' do
156
- (1..12).each do |i|
157
- model.date_of_birth_month = i
158
- expect(model.date_of_birth_month).to eq i
159
- end
160
- end
161
- end
308
+ describe 'to nil' do
309
+ let(:dob_month) { nil }
162
310
 
163
- describe 'above accepted range' do
164
- before { model.date_of_birth_month = 13 }
311
+ before { model.valid? }
165
312
 
166
- it 'sets the value' do
167
- expect(model.date_of_birth_month).to eq 13
168
- end
313
+ it 'sets the value' do
314
+ expect(model.date_of_birth_month).to eq nil
315
+ end
169
316
 
170
- it 'makes the model invalid' do
171
- expect(model).to be_invalid
172
- end
173
- end
317
+ it 'sets the date to nil' do
318
+ expect(model.date_of_birth).to be nil
319
+ end
320
+
321
+ it 'makes the model invalid' do
322
+ expect(model).to be_invalid
323
+ end
324
+
325
+ it 'sets the error to invalid' do
326
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
327
+ end
328
+ end
329
+ end
174
330
 
175
- describe 'below accepted range' do
176
- before { model.date_of_birth_month = 0 }
331
+ describe 'setting the _year' do
332
+ before { model.date_of_birth_year = dob_year }
333
+
334
+ describe 'as numbers' do
335
+ describe 'numerically' do
336
+ describe 'in the acceptable range' do
337
+ let(:dob_year) { 1961 }
338
+
339
+ before { model.valid? }
177
340
 
178
341
  it 'sets the value' do
179
- expect(model.date_of_birth_month).to eq 0
342
+ expect(model.date_of_birth_year).to eq 1961
180
343
  end
181
344
 
182
- it 'makes the model invalid' do
183
- expect(model).to be_invalid
345
+ it 'sets the date to nil' do
346
+ expect(model.date_of_birth).to eq Date.new(1961, 1, 21)
184
347
  end
185
- end
186
- end
187
348
 
188
- describe 'when sent text' do
189
- describe 'that is a valid long month' do
190
- I18n.t('date.month_names').each_with_index do |month, _index|
191
- it "sending #{month} sets the month to #{month}" do
192
- model.date_of_birth_month = month
193
- expect(model.date_of_birth_month).to eq month unless month.nil?
194
- end
349
+ it 'makes the model valid' do
350
+ expect(model).to be_valid
195
351
  end
196
- end
197
- describe 'that is a valid short month' do
198
- I18n.t('date.abbr_month_names').each_with_index do |month, _index|
199
- it "sending #{month} sets the month to #{month}" do
200
- model.date_of_birth_month = month
201
- expect(model.date_of_birth_month).to eq month unless month.nil?
202
- end
352
+
353
+ it 'returns no errors' do
354
+ expect(model.errors.count).to eq 0
203
355
  end
204
356
  end
205
- describe 'that is not a valid month' do
206
- before { model.date_of_birth_month = 'first' }
207
357
 
208
- it 'returns the text' do
209
- expect(model.date_of_birth_month).to eq 'first'
358
+ describe 'outside the acceptable range' do
359
+ let(:dob_year) { 3334 }
360
+
361
+ before { model.valid? }
362
+
363
+ it 'sets the value' do
364
+ expect(model.date_of_birth_year).to eq 3334
365
+ end
366
+
367
+ it 'sets the date to nil' do
368
+ expect(model.date_of_birth).to eq nil
210
369
  end
211
370
 
212
371
  it 'makes the model invalid' do
213
372
  expect(model).to be_invalid
214
373
  end
215
- end
216
- end
217
-
218
- describe 'when sent nil' do
219
- it 'returns nil' do
220
- model.date_of_birth_month = nil
221
- expect(model.date_of_birth_month).to be_nil
222
- end
223
- end
224
- end
225
374
 
226
- describe '_year' do
227
- describe 'when sent numbers' do
228
- describe 'numerically' do
229
- describe 'in the acceptable range' do
230
- it 'sets the value' do
231
- model.date_of_birth_year = 1961
232
- expect(model.date_of_birth_year).to eq 1961
233
- end
234
- end
235
- describe 'outside the acceptable range' do
236
- before { model.date_of_birth_year = 3334 }
237
- it 'returns nil' do
238
- expect(model.date_of_birth_year).to be 3334
239
- end
240
-
241
- it 'makes the model invalid' do
242
- expect(model).to be_invalid
243
- end
244
- end
245
- end
246
- describe 'as text' do
247
- it 'accepts and converts them' do
248
- model.date_of_birth_year = '1961'
249
- expect(model.date_of_birth_year).to eq '1961'
375
+ it 'sets the error to invalid' do
376
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
250
377
  end
251
378
  end
252
379
  end
253
380
 
254
- describe 'when sent text' do
255
- before { model.date_of_birth_year = 'Nineteen Sixty One' }
381
+ describe 'as text' do
382
+ let(:dob_year) { '1961' }
383
+
384
+ before { model.valid? }
256
385
 
257
386
  it 'sets the value' do
258
- expect(model.date_of_birth_year).to eq 'Nineteen Sixty One'
387
+ expect(model.date_of_birth_year).to eq 1961
259
388
  end
260
389
 
261
- it 'makes the model invalid' do
262
- expect(model).to be_invalid
390
+ it 'sets the date to nil' do
391
+ expect(model.date_of_birth).to eq Date.new(1961, 1, 21)
392
+ end
393
+
394
+ it 'makes the model valid' do
395
+ expect(model).to be_valid
263
396
  end
264
- end
265
397
 
266
- describe 'when sent nil' do
267
- it 'returns nil' do
268
- model.date_of_birth_year = nil
269
- expect(model.date_of_birth_year).to be_nil
398
+ it 'returns no errors' do
399
+ expect(model.errors.count).to eq 0
270
400
  end
271
401
  end
272
402
  end
273
403
 
274
- describe 'when passed implausible dates' do
275
- before do
276
- model.date_of_birth_day = 30
277
- model.date_of_birth_month = 2
278
- model.date_of_birth_year = 2015
279
- model.valid?
280
- end
404
+ describe 'using text' do
405
+ let(:dob_year) { 'Nineteen Sixty One' }
281
406
 
282
- it 'returns an error' do
283
- expect(model.errors[:date_of_birth]).to eq ['is invalid']
407
+ before { model.valid? }
408
+
409
+ it 'sets the value' do
410
+ expect(model.date_of_birth_year).to eq 'Nineteen Sixty One'
284
411
  end
285
- end
286
412
 
287
- describe 'set themselves but do not set date' do
288
- describe '#posted_day' do
289
- it 'can be set and returned' do
290
- model.date_of_birth_day = 1
291
- expect(model.date_of_birth_day).to eq 1
292
- expect(model.date_of_birth).to be nil
293
- end
413
+ it 'sets the date to nil' do
414
+ expect(model.date_of_birth).to eq nil
294
415
  end
295
416
 
296
- describe '#posted_month' do
297
- it 'can be set and returned' do
298
- model.date_of_birth_month = 10
299
- expect(model.date_of_birth_month).to eq 10
300
- expect(model.date_of_birth).to be nil
301
- end
417
+ it 'makes the model invalid' do
418
+ expect(model).to be_invalid
302
419
  end
303
420
 
304
- describe '#posted_year' do
305
- it 'can be set and returned' do
306
- model.date_of_birth_year = 10
307
- expect(model.date_of_birth_year).to eq 10
308
- expect(model.date_of_birth).to be nil
309
- end
421
+ it 'sets the error to invalid' do
422
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
310
423
  end
311
424
  end
312
425
 
313
- describe 'set the date field by calling validate_date' do
314
- context 'when all are completed' do
315
- before(:each) { model.date_of_birth = nil }
426
+ describe 'to nil' do
427
+ let(:dob_year) { nil }
316
428
 
317
- it 'with year last' do
318
- model.date_of_birth_day = 21
319
- model.date_of_birth_month = 1
320
- model.date_of_birth_year = 1961
321
- expect(model.date_of_birth).to eql Date.new(1961, 1, 21)
322
- end
429
+ before { model.valid? }
323
430
 
324
- it 'with month last' do
325
- model.date_of_birth_year = 1961
326
- model.date_of_birth_day = 21
327
- model.date_of_birth_month = 1
328
- expect(model.date_of_birth).to eql Date.new(1961, 1, 21)
329
- end
431
+ it 'sets the value' do
432
+ expect(model.date_of_birth_year).to eq nil
433
+ end
330
434
 
331
- it 'with day last' do
332
- model.date_of_birth_month = 1
333
- model.date_of_birth_year = 1961
334
- model.date_of_birth_day = 21
335
- expect(model.date_of_birth).to eql Date.new(1961, 1, 21)
336
- end
435
+ it 'sets the date to nil' do
436
+ expect(model.date_of_birth).to be nil
437
+ end
438
+
439
+ it 'makes the model invalid' do
440
+ expect(model).to be_invalid
441
+ end
442
+
443
+ it 'sets the error to invalid' do
444
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
337
445
  end
338
446
  end
339
447
  end
340
448
 
449
+ describe 'when passed implausible dates' do
450
+ before do
451
+ model.date_of_birth_day = 30
452
+ model.date_of_birth_month = 2
453
+ model.date_of_birth_year = 2015
454
+ model.valid?
455
+ end
456
+
457
+ it 'returns an error' do
458
+ expect(model.errors[:date_of_birth]).to eq ['is invalid']
459
+ end
460
+ end
341
461
  end
342
462
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: split_dmy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Bruce
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-21 00:00:00.000000000 Z
11
+ date: 2016-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler