split_dmy 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/split_dmy/date_validator.rb +4 -17
- data/lib/split_dmy/split_accessors.rb +1 -6
- data/lib/split_dmy/version.rb +1 -1
- data/spec/split_dmy/split_accessors_spec.rb +339 -219
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77b51777ddac980e96091125b2a9c91944a47211
|
4
|
+
data.tar.gz: edbc9e21a698aa1e96a0b680c6d2ca5021ab4814
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
13
|
-
partials_valid? &&
|
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
|
44
|
-
|
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
|
-
|
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)
|
data/lib/split_dmy/version.rb
CHANGED
@@ -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(:
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
24
|
-
|
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
|
29
|
-
expect(model).to
|
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 '
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
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
|
-
|
44
|
-
|
76
|
+
describe 'to text' do
|
77
|
+
let(:dob_day) { 'first' }
|
45
78
|
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
51
|
-
|
52
|
-
|
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 '
|
56
|
-
expect(model
|
183
|
+
it 'makes the model valid' do
|
184
|
+
expect(model).to be_valid
|
57
185
|
end
|
58
186
|
|
59
|
-
it '
|
60
|
-
expect(model.
|
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
|
-
|
66
|
-
|
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
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
78
|
-
expect(model.date_of_birth).to
|
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
|
-
|
81
|
-
|
82
|
-
expect(model
|
205
|
+
|
206
|
+
it 'makes the model valid' do
|
207
|
+
expect(model).to be_valid
|
83
208
|
end
|
84
|
-
|
85
|
-
|
86
|
-
expect(model.
|
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 '
|
93
|
-
|
94
|
-
describe 'between 1 and
|
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
|
-
(
|
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
|
-
|
104
|
-
|
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 '
|
110
|
-
expect(model
|
253
|
+
it 'makes the model valid' do
|
254
|
+
expect(model).to be_valid
|
111
255
|
end
|
112
256
|
|
113
|
-
it '
|
114
|
-
|
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
|
-
|
120
|
-
|
263
|
+
describe 'to too high a value' do
|
264
|
+
let(:dob_month) { 13 }
|
121
265
|
|
122
|
-
|
123
|
-
expect(model.date_of_birth_day).to eq 0
|
124
|
-
end
|
266
|
+
before { model.valid? }
|
125
267
|
|
126
|
-
|
127
|
-
|
128
|
-
|
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 '
|
133
|
-
|
285
|
+
describe 'to too low a value' do
|
286
|
+
let(:dob_month) { 0 }
|
287
|
+
|
288
|
+
before { model.valid? }
|
134
289
|
|
135
|
-
it '
|
136
|
-
expect(model.
|
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
|
-
|
145
|
-
|
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 '
|
153
|
-
|
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
|
-
|
164
|
-
before { model.date_of_birth_month = 13 }
|
311
|
+
before { model.valid? }
|
165
312
|
|
166
|
-
|
167
|
-
|
168
|
-
|
313
|
+
it 'sets the value' do
|
314
|
+
expect(model.date_of_birth_month).to eq nil
|
315
|
+
end
|
169
316
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
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
|
-
|
176
|
-
|
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.
|
342
|
+
expect(model.date_of_birth_year).to eq 1961
|
180
343
|
end
|
181
344
|
|
182
|
-
it '
|
183
|
-
expect(model).to
|
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
|
-
|
189
|
-
|
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
|
-
|
197
|
-
|
198
|
-
|
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
|
-
|
209
|
-
|
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
|
-
|
227
|
-
|
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 '
|
255
|
-
|
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
|
387
|
+
expect(model.date_of_birth_year).to eq 1961
|
259
388
|
end
|
260
389
|
|
261
|
-
it '
|
262
|
-
expect(model).to
|
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
|
-
|
267
|
-
|
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 '
|
275
|
-
|
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
|
-
|
283
|
-
|
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
|
-
|
288
|
-
|
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
|
-
|
297
|
-
|
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
|
-
|
305
|
-
|
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 '
|
314
|
-
|
315
|
-
before(:each) { model.date_of_birth = nil }
|
426
|
+
describe 'to nil' do
|
427
|
+
let(:dob_year) { nil }
|
316
428
|
|
317
|
-
|
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
|
-
|
325
|
-
|
326
|
-
|
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
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
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.
|
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-
|
11
|
+
date: 2016-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|