validates_timeliness 2.3.2 → 3.0.0.beta

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.
Files changed (59) hide show
  1. data/CHANGELOG +12 -4
  2. data/LICENSE +1 -1
  3. data/README.rdoc +138 -280
  4. data/Rakefile +30 -16
  5. data/lib/generators/validates_timeliness/install_generator.rb +17 -0
  6. data/lib/generators/validates_timeliness/templates/en.yml +16 -0
  7. data/lib/generators/validates_timeliness/templates/validates_timeliness.rb +22 -0
  8. data/lib/validates_timeliness.rb +46 -52
  9. data/lib/validates_timeliness/attribute_methods.rb +51 -0
  10. data/lib/validates_timeliness/conversion.rb +69 -0
  11. data/lib/validates_timeliness/extensions.rb +14 -0
  12. data/lib/validates_timeliness/extensions/date_time_select.rb +45 -0
  13. data/lib/validates_timeliness/extensions/multiparameter_handler.rb +31 -0
  14. data/lib/validates_timeliness/helper_methods.rb +41 -0
  15. data/lib/validates_timeliness/orms/active_record.rb +14 -0
  16. data/lib/validates_timeliness/parser.rb +389 -17
  17. data/lib/validates_timeliness/validator.rb +37 -200
  18. data/lib/validates_timeliness/version.rb +1 -1
  19. data/spec/model_helpers.rb +27 -0
  20. data/spec/spec_helper.rb +74 -43
  21. data/spec/test_model.rb +56 -0
  22. data/spec/validates_timeliness/attribute_methods_spec.rb +36 -0
  23. data/spec/validates_timeliness/conversion_spec.rb +204 -0
  24. data/spec/validates_timeliness/extensions/date_time_select_spec.rb +178 -0
  25. data/spec/validates_timeliness/extensions/multiparameter_handler_spec.rb +21 -0
  26. data/spec/validates_timeliness/helper_methods_spec.rb +36 -0
  27. data/spec/{formats_spec.rb → validates_timeliness/parser_spec.rb} +105 -71
  28. data/spec/validates_timeliness/validator/after_spec.rb +59 -0
  29. data/spec/validates_timeliness/validator/before_spec.rb +59 -0
  30. data/spec/validates_timeliness/validator/is_at_spec.rb +63 -0
  31. data/spec/validates_timeliness/validator/on_or_after_spec.rb +59 -0
  32. data/spec/validates_timeliness/validator/on_or_before_spec.rb +59 -0
  33. data/spec/validates_timeliness/validator_spec.rb +172 -0
  34. data/validates_timeliness.gemspec +30 -0
  35. metadata +42 -40
  36. data/TODO +0 -8
  37. data/lib/validates_timeliness/action_view/instance_tag.rb +0 -52
  38. data/lib/validates_timeliness/active_record/attribute_methods.rb +0 -77
  39. data/lib/validates_timeliness/active_record/multiparameter_attributes.rb +0 -69
  40. data/lib/validates_timeliness/formats.rb +0 -368
  41. data/lib/validates_timeliness/locale/en.new.yml +0 -18
  42. data/lib/validates_timeliness/locale/en.old.yml +0 -18
  43. data/lib/validates_timeliness/matcher.rb +0 -1
  44. data/lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb +0 -162
  45. data/lib/validates_timeliness/validation_methods.rb +0 -46
  46. data/spec/action_view/instance_tag_spec.rb +0 -194
  47. data/spec/active_record/attribute_methods_spec.rb +0 -157
  48. data/spec/active_record/multiparameter_attributes_spec.rb +0 -118
  49. data/spec/ginger_scenarios.rb +0 -19
  50. data/spec/parser_spec.rb +0 -65
  51. data/spec/resources/application.rb +0 -2
  52. data/spec/resources/person.rb +0 -3
  53. data/spec/resources/schema.rb +0 -10
  54. data/spec/resources/sqlite_patch.rb +0 -19
  55. data/spec/spec/rails/matchers/validate_timeliness_spec.rb +0 -245
  56. data/spec/time_travel/MIT-LICENSE +0 -20
  57. data/spec/time_travel/time_extensions.rb +0 -33
  58. data/spec/time_travel/time_travel.rb +0 -12
  59. data/spec/validator_spec.rb +0 -723
@@ -1,34 +1,34 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe ValidatesTimeliness::Formats do
3
+ describe ValidatesTimeliness::Parser do
4
4
 
5
5
  describe "format proc generator" do
6
6
  it "should generate proc which outputs date array with values in correct order" do
7
- generate_proc('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,0,0,0,0]
7
+ generate_method('yyyy-mm-dd').call('2000', '1', '2').should == [2000,1,2,0,0,0,0]
8
8
  end
9
9
 
10
10
  it "should generate proc which outputs date array from format with different order" do
11
- generate_proc('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,0,0,0,0]
11
+ generate_method('dd/mm/yyyy').call('2', '1', '2000').should == [2000,1,2,0,0,0,0]
12
12
  end
13
13
 
14
14
  it "should generate proc which outputs time array" do
15
- generate_proc('hh:nn:ss').call('01', '02', '03').should == [0,0,0,1,2,3,0]
15
+ generate_method('hh:nn:ss').call('01', '02', '03').should == [0,0,0,1,2,3,0]
16
16
  end
17
17
 
18
18
  it "should generate proc which outputs time array with meridian 'pm' adjusted hour" do
19
- generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [0,0,0,13,2,3,0]
19
+ generate_method('hh:nn:ss ampm').call('01', '02', '03', 'pm').should == [0,0,0,13,2,3,0]
20
20
  end
21
21
 
22
22
  it "should generate proc which outputs time array with meridian 'am' unadjusted hour" do
23
- generate_proc('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [0,0,0,1,2,3,0]
23
+ generate_method('hh:nn:ss ampm').call('01', '02', '03', 'am').should == [0,0,0,1,2,3,0]
24
24
  end
25
25
 
26
26
  it "should generate proc which outputs time array with microseconds" do
27
- generate_proc('hh:nn:ss.u').call('01', '02', '03', '99').should == [0,0,0,1,2,3,990000]
27
+ generate_method('hh:nn:ss.u').call('01', '02', '03', '99').should == [0,0,0,1,2,3,990000]
28
28
  end
29
29
 
30
30
  it "should generate proc which outputs datetime array with zone offset" do
31
- generate_proc('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
31
+ generate_method('yyyy-mm-dd hh:nn:ss.u zo').call('2001', '02', '03', '04', '05', '06', '99', '+10:00').should == [2001,2,3,4,5,6,990000,36000]
32
32
  end
33
33
  end
34
34
 
@@ -95,115 +95,148 @@ describe ValidatesTimeliness::Formats do
95
95
  end
96
96
  end
97
97
 
98
- describe "parse" do
98
+ describe "_parse" do
99
99
 
100
100
  it "should return time array from date string" do
101
- time_array = formats.parse('12:13:14', :time, :strict => true)
101
+ time_array = formats._parse('12:13:14', :time, :strict => true)
102
102
  time_array.should == [2000,1,1,12,13,14,0]
103
103
  end
104
104
 
105
105
  it "should return nil if time hour is out of range for AM meridian" do
106
- time_array = formats.parse('13:14 am', :time, :strict => true)
106
+ time_array = formats._parse('13:14 am', :time, :strict => true)
107
107
  time_array.should == nil
108
- time_array = formats.parse('00:14 am', :time, :strict => true)
108
+ time_array = formats._parse('00:14 am', :time, :strict => true)
109
109
  time_array.should == nil
110
110
  end
111
111
 
112
112
  it "should return date array from time string" do
113
- time_array = formats.parse('2000-02-01', :date, :strict => true)
113
+ time_array = formats._parse('2000-02-01', :date, :strict => true)
114
114
  time_array.should == [2000,2,1,0,0,0,0]
115
115
  end
116
116
 
117
117
  it "should return datetime array from string value" do
118
- time_array = formats.parse('2000-02-01 12:13:14', :datetime, :strict => true)
118
+ time_array = formats._parse('2000-02-01 12:13:14', :datetime, :strict => true)
119
119
  time_array.should == [2000,2,1,12,13,14,0]
120
120
  end
121
121
 
122
122
  it "should parse date string when type is datetime" do
123
- time_array = formats.parse('2000-02-01', :datetime, :strict => false)
123
+ time_array = formats._parse('2000-02-01', :datetime, :strict => false)
124
124
  time_array.should == [2000,2,1,0,0,0,0]
125
125
  end
126
126
 
127
127
  it "should ignore time when extracting date and strict is false" do
128
- time_array = formats.parse('2000-02-01 12:13', :date, :strict => false)
128
+ time_array = formats._parse('2000-02-01 12:13', :date, :strict => false)
129
129
  time_array.should == [2000,2,1,0,0,0,0]
130
130
  end
131
131
 
132
132
  it "should ignore time when extracting date from format with trailing year and strict is false" do
133
- time_array = formats.parse('01-02-2000 12:13', :date, :strict => false)
133
+ time_array = formats._parse('01-02-2000 12:13', :date, :strict => false)
134
134
  time_array.should == [2000,2,1,0,0,0,0]
135
135
  end
136
136
 
137
137
  it "should ignore date when extracting time and strict is false" do
138
- time_array = formats.parse('2000-02-01 12:13', :time, :strict => false)
138
+ time_array = formats._parse('2000-02-01 12:13', :time, :strict => false)
139
139
  time_array.should == [2000,1,1,12,13,0,0]
140
140
  end
141
141
 
142
142
  it "should return zone offset when :include_offset option is true" do
143
- time_array = formats.parse('2000-02-01T12:13:14-10:30', :datetime, :include_offset => true)
143
+ time_array = formats._parse('2000-02-01T12:13:14-10:30', :datetime, :include_offset => true)
144
144
  time_array.should == [2000,2,1,12,13,14,0,-37800]
145
145
  end
146
- end
147
146
 
148
- describe "parse with format option" do
149
- it "should return values if string matches specified format" do
150
- time_array = formats.parse('2000-02-01 12:13:14', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
151
- time_array.should == [2000,2,1,12,13,14,0]
152
- end
147
+ context "with format option" do
148
+ it "should return values if string matches specified format" do
149
+ time_array = formats._parse('2000-02-01 12:13:14', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
150
+ time_array.should == [2000,2,1,12,13,14,0]
151
+ end
153
152
 
154
- it "should return nil if string does not match specified format" do
155
- time_array = formats.parse('2000-02-01 12:13', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
156
- time_array.should be_nil
153
+ it "should return nil if string does not match specified format" do
154
+ time_array = formats._parse('2000-02-01 12:13', :datetime, :format => 'yyyy-mm-dd hh:nn:ss')
155
+ time_array.should be_nil
156
+ end
157
157
  end
158
- end
159
158
 
160
- describe "parsing date with ambiguous year" do
161
- it "should return year in current century if year below threshold" do
162
- time_array = formats.parse('01-02-29', :date)
163
- time_array.should == [2029,2,1,0,0,0,0]
164
- end
159
+ context "date with ambiguous year" do
160
+ it "should return year in current century if year below threshold" do
161
+ time_array = formats._parse('01-02-29', :date)
162
+ time_array.should == [2029,2,1,0,0,0,0]
163
+ end
165
164
 
166
- it "should return year in last century if year at or above threshold" do
167
- time_array = formats.parse('01-02-30', :date)
168
- time_array.should == [1930,2,1,0,0,0,0]
165
+ it "should return year in last century if year at or above threshold" do
166
+ time_array = formats._parse('01-02-30', :date)
167
+ time_array.should == [1930,2,1,0,0,0,0]
168
+ end
169
+
170
+ it "should allow custom threshold" do
171
+ default = ValidatesTimeliness::Parser.ambiguous_year_threshold
172
+ ValidatesTimeliness::Parser.ambiguous_year_threshold = 40
173
+ time_array = formats._parse('01-02-39', :date)
174
+ time_array.should == [2039,2,1,0,0,0,0]
175
+ time_array = formats._parse('01-02-40', :date)
176
+ time_array.should == [1940,2,1,0,0,0,0]
177
+ ValidatesTimeliness::Parser.ambiguous_year_threshold = default
178
+ end
169
179
  end
170
180
 
171
- it "should allow custom threshold" do
172
- default = ValidatesTimeliness::Formats.ambiguous_year_threshold
173
- ValidatesTimeliness::Formats.ambiguous_year_threshold = 40
174
- time_array = formats.parse('01-02-39', :date)
175
- time_array.should == [2039,2,1,0,0,0,0]
176
- time_array = formats.parse('01-02-40', :date)
177
- time_array.should == [1940,2,1,0,0,0,0]
178
- ValidatesTimeliness::Formats.ambiguous_year_threshold = default
181
+ context "with custom dummy date values" do
182
+ before(:all) do
183
+ @old_dummy_date = ValidatesTimeliness.dummy_date_for_time_type
184
+ ValidatesTimeliness.dummy_date_for_time_type = [2009,1,1]
185
+ end
186
+
187
+ it "should return time array with custom dummy date" do
188
+ time_array = formats._parse('12:13:14', :time, :strict => true)
189
+ time_array.should == [2009,1,1,12,13,14,0]
190
+ end
191
+
192
+ after(:all) do
193
+ ValidatesTimeliness.dummy_date_for_time_type = @old_dummy_date
194
+ end
179
195
  end
180
196
  end
181
197
 
182
- describe "parse with custom dummy date values" do
183
- before(:all) do
184
- @old_dummy_date = formats.dummy_date_for_time_type
185
- formats.dummy_date_for_time_type = [2009,1,1]
198
+ describe "parse" do
199
+ it "should return time object for valid time string" do
200
+ parse("2000-01-01 12:13:14", :datetime).should be_kind_of(Time)
186
201
  end
187
-
188
- it "should return time array with custom dummy date" do
189
- time_array = formats.parse('12:13:14', :time, :strict => true)
190
- time_array.should == [2009,1,1,12,13,14,0]
202
+
203
+ it "should return nil for time string with invalid date part" do
204
+ parse("2000-02-30 12:13:14", :datetime).should be_nil
205
+ end
206
+
207
+ it "should return nil for time string with invalid time part" do
208
+ parse("2000-02-01 25:13:14", :datetime).should be_nil
209
+ end
210
+
211
+ it "should return Time object when passed a Time object" do
212
+ parse(Time.now, :datetime).should be_kind_of(Time)
213
+ end
214
+
215
+ it "should convert time string into current timezone" do
216
+ Time.zone = 'Melbourne'
217
+ time = parse("2000-01-01 12:13:14", :datetime, :timezone_aware => true)
218
+ Time.zone.utc_offset.should == 10.hours
191
219
  end
192
220
 
193
- after(:all) do
194
- formats.dummy_date_for_time_type = @old_dummy_date
221
+ it "should return nil for invalid date string" do
222
+ parse("2000-02-30", :date).should be_nil
223
+ end
224
+
225
+ def parse(*args)
226
+ ValidatesTimeliness::Parser.parse(*args)
195
227
  end
196
228
  end
197
229
 
198
- describe "parse ISO8601 datetime" do
199
- it "should return array without zone offset when no offset in string" do
200
- time_array = formats.parse('2000-02-01T12:13:14Z', :datetime, :strict => true)
201
- time_array.should == [2000,2,1,12,13,14,0]
230
+ describe "make_time" do
231
+ it "should create time using current timezone" do
232
+ time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0])
233
+ time.zone.should == "UTC"
202
234
  end
203
235
 
204
- it "should return array with zone offset when offset in string" do
205
- time_array = formats.parse('2000-02-01T12:13:14+10:00', :datetime, :strict => true)
206
- time_array.should == [2000,2,1,12,13,14,0,36000]
236
+ it "should create time using current timezone" do
237
+ Time.zone = 'Melbourne'
238
+ time = ValidatesTimeliness::Parser.make_time([2000,1,1,12,0,0], true)
239
+ time.zone.should == "EST"
207
240
  end
208
241
  end
209
242
 
@@ -248,7 +281,7 @@ describe ValidatesTimeliness::Formats do
248
281
  it "should add format before specified format and be higher precedence" do
249
282
  formats.add_formats(:time, "ss:hh:nn", :before => 'hh:nn:ss')
250
283
  validate("59:23:58", :time).should be_true
251
- time_array = formats.parse('59:23:58', :time)
284
+ time_array = formats._parse('59:23:58', :time)
252
285
  time_array.should == [2000,1,1,23,58,59,0]
253
286
  end
254
287
 
@@ -269,10 +302,10 @@ describe ValidatesTimeliness::Formats do
269
302
 
270
303
  describe "removing US formats" do
271
304
  it "should validate a date as European format when US formats removed" do
272
- time_array = formats.parse('01/02/2000', :date)
305
+ time_array = formats._parse('01/02/2000', :date)
273
306
  time_array.should == [2000, 1, 2,0,0,0,0]
274
307
  formats.remove_us_formats
275
- time_array = formats.parse('01/02/2000', :date)
308
+ time_array = formats._parse('01/02/2000', :date)
276
309
  time_array.should == [2000, 2, 1,0,0,0,0]
277
310
  end
278
311
 
@@ -283,7 +316,7 @@ describe ValidatesTimeliness::Formats do
283
316
 
284
317
 
285
318
  def formats
286
- ValidatesTimeliness::Formats
319
+ ValidatesTimeliness::Parser
287
320
  end
288
321
 
289
322
  def validate(time_string, type)
@@ -296,15 +329,16 @@ describe ValidatesTimeliness::Formats do
296
329
 
297
330
  def generate_regexp(format)
298
331
  # wrap in line start and end anchors to emulate extract values method
299
- /\A#{formats.send(:generate_format_expression, format)[0]}\Z/
332
+ /\A#{formats.send(:generate_format_expression, format)}\Z/
300
333
  end
301
334
 
302
335
  def generate_regexp_str(format)
303
- formats.send(:generate_format_expression, format)[0].inspect
336
+ formats.send(:generate_format_expression, format).inspect
304
337
  end
305
338
 
306
- def generate_proc(format)
307
- formats.send(:generate_format_expression, format)[1]
339
+ def generate_method(format)
340
+ formats.send(:generate_format_expression, format)
341
+ ValidatesTimeliness::Parser.method(:"format_#{format}")
308
342
  end
309
343
 
310
344
  def delete_format(type, format)
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":after option" do
4
+ include ModelHelpers
5
+
6
+ describe "for date type" do
7
+ before do
8
+ Person.validates_date :birth_date, :after => Date.new(2010, 1, 1)
9
+ end
10
+
11
+ it "should not be valid for same date value" do
12
+ invalid!(:birth_date, Date.new(2010, 1, 1), 'must be after 2010-01-01')
13
+ end
14
+
15
+ it "should not be valid for date before restriction" do
16
+ invalid!(:birth_date, Date.new(2009, 12, 31), 'must be after 2010-01-01')
17
+ end
18
+
19
+ it "should be valid for date after restriction" do
20
+ valid!(:birth_date, Date.new(2010, 1, 2))
21
+ end
22
+ end
23
+
24
+ describe "for time type" do
25
+ before do
26
+ Person.validates_time :birth_time, :after => Time.mktime(2000, 1, 1, 12, 0, 0)
27
+ end
28
+
29
+ it "should not be valid for same time as restriction" do
30
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be after 12:00:00')
31
+ end
32
+
33
+ it "should not be valid for time before restriction" do
34
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be after 12:00:00')
35
+ end
36
+
37
+ it "should be valid for time after restriction" do
38
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01))
39
+ end
40
+ end
41
+
42
+ describe "for datetime type" do
43
+ before do
44
+ Person.validates_datetime :birth_datetime, :after => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
45
+ end
46
+
47
+ it "should not be valid for same datetime as restriction" do
48
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be after 2010-01-01 12:00:00')
49
+ end
50
+
51
+ it "should be valid for datetime is before restriction" do
52
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be after 2010-01-01 12:00:00')
53
+ end
54
+
55
+ it "should be valid for datetime is after restriction" do
56
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1))
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":before option" do
4
+ include ModelHelpers
5
+
6
+ describe "for date type" do
7
+ before do
8
+ Person.validates_date :birth_date, :before => Date.new(2010, 1, 1)
9
+ end
10
+
11
+ it "should not be valid for date after restriction" do
12
+ invalid!(:birth_date, Date.new(2010, 1, 2), 'must be before 2010-01-01')
13
+ end
14
+
15
+ it "should not be valid for same date value" do
16
+ invalid!(:birth_date, Date.new(2010, 1, 1), 'must be before 2010-01-01')
17
+ end
18
+
19
+ it "should be valid for date before restriction" do
20
+ valid!(:birth_date, Date.new(2009, 12, 31))
21
+ end
22
+ end
23
+
24
+ describe "for time type" do
25
+ before do
26
+ Person.validates_time :birth_time, :before => Time.mktime(2000, 1, 1, 12, 0, 0)
27
+ end
28
+
29
+ it "should not be valid for time after restriction" do
30
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be before 12:00:00')
31
+ end
32
+
33
+ it "should not be valid for same time as restriction" do
34
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0), 'must be before 12:00:00')
35
+ end
36
+
37
+ it "should be valid for time before restriction" do
38
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59))
39
+ end
40
+ end
41
+
42
+ describe "for datetime type" do
43
+ before do
44
+ Person.validates_datetime :birth_datetime, :before => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
45
+ end
46
+
47
+ it "should not be valid for datetime after restriction" do
48
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be before 2010-01-01 12:00:00')
49
+ end
50
+
51
+ it "should not be valid for same datetime as restriction" do
52
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0), 'must be before 2010-01-01 12:00:00')
53
+ end
54
+
55
+ it "should be valid for datetime before restriction" do
56
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59))
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesTimeliness::Validator, ":is_at option" do
4
+ include ModelHelpers
5
+
6
+ before do
7
+ Timecop.freeze(Time.local_time(2010, 1, 1, 0, 0, 0))
8
+ end
9
+
10
+ describe "for date type" do
11
+ before do
12
+ Person.validates_date :birth_date, :is_at => Date.new(2010, 1, 1)
13
+ end
14
+
15
+ it "should not be valid for date before restriction" do
16
+ invalid!(:birth_date, Date.new(2009, 12, 31), 'must be at 2010-01-01')
17
+ end
18
+
19
+ it "should not be valid for date after restriction" do
20
+ invalid!(:birth_date, Date.new(2010, 1, 2), 'must be at 2010-01-01')
21
+ end
22
+
23
+ it "should be valid for same date value" do
24
+ valid!(:birth_date, Date.new(2010, 1, 1))
25
+ end
26
+ end
27
+
28
+ describe "for time type" do
29
+ before do
30
+ Person.validates_time :birth_time, :is_at => Time.mktime(2000, 1, 1, 12, 0, 0)
31
+ end
32
+
33
+ it "should not be valid for time before restriction" do
34
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 11, 59, 59), 'must be at 12:00:00')
35
+ end
36
+
37
+ it "should not be valid for time after restriction" do
38
+ invalid!(:birth_time, Time.local_time(2000, 1, 1, 12, 00, 01), 'must be at 12:00:00')
39
+ end
40
+
41
+ it "should be valid for same time as restriction" do
42
+ valid!(:birth_time, Time.local_time(2000, 1, 1, 12, 0, 0))
43
+ end
44
+ end
45
+
46
+ describe "for datetime type" do
47
+ before do
48
+ Person.validates_datetime :birth_datetime, :is_at => DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0)
49
+ end
50
+
51
+ it "should not be valid for datetime before restriction" do
52
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 11, 59, 59), 'must be at 2010-01-01 12:00:00')
53
+ end
54
+
55
+ it "should not be valid for datetime after restriction" do
56
+ invalid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 1), 'must be at 2010-01-01 12:00:00')
57
+ end
58
+
59
+ it "should be valid for same datetime as restriction" do
60
+ valid!(:birth_datetime, DateTime.civil_from_format(:local, 2010, 1, 1, 12, 0, 0))
61
+ end
62
+ end
63
+ end