gbdev-validates_as_date_time 0.5.1

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.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ 0.5.1 - Aug 15th 2008
2
+ * Initial release
3
+ * Gem'ified plugin
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ Licensing terms follow:
2
+
3
+ This software is placed under the Creative Commons Attribution-Share ALike 3.0 Unported License
4
+
5
+ http://creativecommons.org/licenses/by-sa/3.0/
data/README ADDED
@@ -0,0 +1,31 @@
1
+ ValidatesAsDateTime
2
+ ===================
3
+ This Ruby on Rails gem/plugin implements an ActiveRecord validation helper called
4
+ validates_as_date_time which validates U.S. dates, times, or date-times. It accepts
5
+ strings, and ruby Date, Time, or DateTime objects.
6
+
7
+ Installation:
8
+ =============
9
+ gem sources -a http://gems.github.com
10
+
11
+ Install the gem(s):
12
+ sudo gem install gbdev-validates_as_date_time
13
+
14
+ Add to environment.rb initializer block:
15
+ config.gem 'gbdev-validates_as_date_time', :version => '>=0.5.0', :lib => 'validates_as_date_time', :source => 'http://gems.github.com'
16
+
17
+ Usage:
18
+ ======
19
+ In your model file do something like:
20
+
21
+ class MyClass < ActiveRecord::Base
22
+ validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => false
23
+ end
24
+
25
+ Tests:
26
+ ======
27
+ Some tests have been added.
28
+
29
+ License:
30
+ ========
31
+ See the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the validates_as_date_time plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the validates_as_date_time plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ValidatesAsDateTime'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'validates_as_date_time'
@@ -0,0 +1,117 @@
1
+ #
2
+ # Made this a plugin because there was not one out there yet. (yes I did an exhaustive search)
3
+ #
4
+ # Licensed under a Creative Commons Attribution-ShareAlike 2.5 License
5
+ # http://creativecommons.org/licenses/by-sa/2.5/
6
+ #
7
+
8
+ # Validation helper for ActiveRecord derived objects that cleanly and simply
9
+ # allows the model to check if the given string is a valid US datetime
10
+ # 11/11/1111 12pm
11
+ # 11/11/1111 1pm
12
+ # 1/11/1111 10:10pm
13
+ # 11/1/1111 01pm
14
+ # 1/1/1111 01:11pm
15
+ # 1/1/1111 1:11pm
16
+ #
17
+ # 11/11/1111
18
+ # 11/11/1111
19
+ # 1/11/1111
20
+ # 11/1/1111
21
+ # 1/1/1111
22
+ # 1/1/1111
23
+
24
+ module ActiveRecord
25
+ module Validations
26
+ module ClassMethods
27
+
28
+ def validates_as_time(*attr_names)
29
+ regExpStr = '[0-9]{1,2}(:[0-9]{2})? *(am|pm)?'
30
+ configuration = {
31
+ :message => 'is an invalid time',
32
+ :with => /^#{regExpStr}$/,
33
+ :allow_nil => false }
34
+
35
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
36
+
37
+ validates_each(attr_names,configuration) do |record, attr_name,value|
38
+ if !['Time', 'String'].include?(record.send(attr_name).class.to_s)
39
+ record.errors.add(attr_name, configuration[:message])
40
+ elsif record.send(attr_name).blank? and !configuration[:allow_nil]
41
+ record.errors.add(attr_name, configuration[:message])
42
+ elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
43
+ record.errors.add(attr_name, configuration[:message])
44
+ else
45
+ begin
46
+ Time.parse(value.to_s) # If time is not valid then it will throw an exception
47
+ rescue
48
+ record.errors.add(attr_name, configuration[:message])
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ def validates_as_date(*attr_names)
56
+ regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4})'
57
+ configuration = {
58
+ :message => 'is an invalid date',
59
+ :with => /^#{regExpStr}$/,
60
+ :allow_nil => false,
61
+ :year_range => nil }
62
+
63
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
64
+
65
+ validates_each(attr_names,configuration) do |record, attr_name,value|
66
+ if !['Date', 'String'].include?(record.send(attr_name).class.to_s)
67
+ record.errors.add(attr_name, configuration[:message])
68
+ elsif record.send(attr_name).blank? and !configuration[:allow_nil]
69
+ record.errors.add(attr_name, configuration[:message])
70
+ elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
71
+ record.errors.add(attr_name, configuration[:message])
72
+ else
73
+ begin
74
+ d = Date.parse(value.to_s) # If date is not valid then it will throw an exception
75
+ record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and
76
+ !configuration[:year_range].include?(d.year)
77
+ rescue
78
+ record.errors.add(attr_name, configuration[:message])
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+
85
+ def validates_as_date_time(*attr_names)
86
+ regExpStr = '(([0-1]?[0-9])\/[0-3]?[0-9]\/[0-9]{4}) *([0-9]{1,2}(:[0-9]{2})? *(am|pm))?'
87
+ configuration = {
88
+ :message => 'is an invalid datetime',
89
+ :with => /^#{regExpStr}$/,
90
+ :allow_nil => false,
91
+ :year_range => nil }
92
+
93
+ configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
94
+
95
+ validates_each(attr_names,configuration) do |record, attr_name,value|
96
+ if !['DateTime', 'String'].include?(record.send(attr_name).class.to_s)
97
+ record.errors.add(attr_name, configuration[:message])
98
+ elsif record.send(attr_name).blank? and !configuration[:allow_nil]
99
+ record.errors.add(attr_name, configuration[:message])
100
+ elsif record.send(attr_name).class.to_s == 'String' and (value.to_s =~ configuration[:with]).nil?
101
+ record.errors.add(attr_name, configuration[:message])
102
+ else
103
+ begin
104
+ d = DateTime.parse(value.to_s) # If date time is not valid then it will throw an exception
105
+ record.errors.add(attr_name, configuration[:message]) if !configuration[:year_range].nil? and
106
+ !configuration[:year_range].include?(d.year)
107
+ rescue
108
+ record.errors.add(attr_name, configuration[:message])
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ end
115
+ end
116
+ end
117
+
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'validates_as_date_time'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'activerecord'
10
+ require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
11
+ end
12
+
13
+ class TestRecordVd < ActiveRecord::Base
14
+ def self.columns; []; end
15
+ attr_accessor :my_birth_date
16
+ validates_as_date :my_birth_date, :year_range => 1980..2005, :allow_nil => false # false is default
17
+ end
18
+
19
+ class TestRecordVd2 < ActiveRecord::Base
20
+ def self.columns; []; end
21
+ attr_accessor :my_birth_date
22
+ validates_as_date :my_birth_date, :year_range => 1980..2005, :allow_nil => true
23
+ end
24
+
25
+ class ValidatesAsDateTimeTest < Test::Unit::TestCase
26
+
27
+ def test_valid_date
28
+ dates = [ '11/11/1990',
29
+ '11/11/1990',
30
+ '1/11/1990',
31
+ '11/1/1990',
32
+ '1/1/1990',
33
+ '1/1/1990']
34
+
35
+ dates.each do |d|
36
+ tr = TestRecordVd.new(:my_birth_date => d)
37
+ assert tr.valid?, "#{d} should be legal."
38
+ assert_equal tr.errors.count, 0
39
+ end
40
+ end
41
+
42
+ def test_invalid_date
43
+ dates = [ '11/11/11 12pm',
44
+ '11/11/11 1pm',
45
+ '1/11/11 10:10pm',
46
+ '11/1/11 01pm',
47
+ '1/1/11 01:11pm',
48
+ '1/1/11 1:11pm',
49
+ '1/1/1',
50
+ '2/29/2003 1:11pm',
51
+ '3/15/1970',
52
+ '',
53
+ nil,
54
+ DateTime.parse('1/1/2001'),
55
+ Date.parse('1/1/1')]
56
+
57
+ dates.each do |d|
58
+ tr = TestRecordVd.new(:my_birth_date => d)
59
+ assert !tr.valid?, "#{d} should be illegal."
60
+ assert_equal tr.errors.count, 1
61
+ end
62
+ end
63
+
64
+ def test_blank_date_times_not_allowed
65
+ tr = TestRecordVd.new(:my_birth_date => nil)
66
+ assert !tr.valid?, "Blank date should be illegal."
67
+ assert_equal tr.errors.count, 1
68
+ end
69
+
70
+ def test_blank_date_times_allowed
71
+ tr = TestRecordVd2.new(:my_birth_date => nil)
72
+ assert tr.valid?, "Blank date should be legal."
73
+ assert_equal tr.errors.count, 0
74
+ end
75
+
76
+ end
77
+
@@ -0,0 +1,81 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'validates_as_date_time'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'activerecord'
10
+ require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
11
+ end
12
+
13
+ class TestRecordVdt < ActiveRecord::Base
14
+ def self.columns; []; end
15
+ attr_accessor :my_birth_date
16
+ validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => false # false is default
17
+ end
18
+
19
+ class TestRecordVdt2 < ActiveRecord::Base
20
+ def self.columns; []; end
21
+ attr_accessor :my_birth_date
22
+ validates_as_date_time :my_birth_date, :year_range => 1980..2005, :allow_nil => true
23
+ end
24
+
25
+ class ValidatesAsDateTimeTest < Test::Unit::TestCase
26
+
27
+ def test_valid_date_times
28
+ date_times = [ '11/11/1990 12pm',
29
+ '11/11/1990 1pm',
30
+ '1/11/1990 10:10pm',
31
+ '11/1/1990 01pm',
32
+ '1/1/1990 01:11pm',
33
+ '1/1/1990 1:11pm',
34
+ '11/11/1990',
35
+ '11/11/1990',
36
+ '1/11/1990',
37
+ '11/1/1990',
38
+ '1/1/1990',
39
+ '1/1/1990',
40
+ DateTime.parse('1/1/2001')]
41
+ date_times.each do |dt|
42
+ assert TestRecordVdt.new(:my_birth_date => dt).valid?, "#{dt} should be legal."
43
+ end
44
+ end
45
+
46
+ def test_invalid_date_times
47
+ date_times = [ '11/11/11 12pm',
48
+ '11/11/11 1pm',
49
+ '1/11/11 10:10pm',
50
+ '11/1/11 01pm',
51
+ '1/1/11 01:11pm',
52
+ '1/1/11 1:11pm',
53
+ '11/11/11',
54
+ '11/11/11',
55
+ '1/11/11',
56
+ '11/1/11',
57
+ '1/1/11',
58
+ '1/1/11',
59
+ '2/30/2003 1:11pm',
60
+ '2/29/2003',
61
+ '3/15/1970',
62
+ '',
63
+ nil,
64
+ Date.parse('2/2/2002'),
65
+ DateTime.parse('1/1/1') ]
66
+
67
+ date_times.each do |dt|
68
+ assert !TestRecordVdt.new(:my_birth_date => dt).valid?, "#{dt} should be illegal."
69
+ end
70
+ end
71
+
72
+ def test_blank_date_times_not_allowed
73
+ assert !TestRecordVdt.new(:my_birth_date => nil).valid?, "Blank datetime should be illegal."
74
+ end
75
+
76
+ def test_blank_date_times_allowed
77
+ assert TestRecordVdt2.new(:my_birth_date => nil).valid?, "Blank datetime should be legal."
78
+ end
79
+
80
+ end
81
+
@@ -0,0 +1,64 @@
1
+ require 'test/unit'
2
+
3
+ begin
4
+ require File.dirname(__FILE__) + '/../../../../config/boot'
5
+ require 'active_record'
6
+ require 'validates_as_date_time'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ require 'activerecord'
10
+ require File.dirname(__FILE__) + '/../lib/validates_as_date_time'
11
+ end
12
+
13
+ class TestRecordVt < ActiveRecord::Base
14
+ def self.columns; []; end
15
+ attr_accessor :my_birth_time
16
+ validates_as_time :my_birth_time, :allow_nil => false # false is default
17
+ end
18
+
19
+ class TestRecordVt2 < ActiveRecord::Base
20
+ def self.columns; []; end
21
+ attr_accessor :my_birth_time
22
+ validates_as_time :my_birth_time, :allow_nil => true
23
+ end
24
+
25
+
26
+ class ValidatesAsDateTimeTest < Test::Unit::TestCase
27
+
28
+ def test_valid_time
29
+ times = [ '12pm',
30
+ '1:00',
31
+ '14:00',
32
+ '10:24am',
33
+ '10:25 pm',
34
+ '09:12',
35
+ Time.parse('2/2/2002 11:00 pm')]
36
+ times.each do |t|
37
+ assert TestRecordVt.new(:my_birth_time => t).valid?, "#{t} should be legal."
38
+ end
39
+ end
40
+
41
+ def test_invalid_time
42
+ times = [ 'asdf1:2::1asdf',
43
+ '1:002',
44
+ '14:80am',
45
+ '00:74pm',
46
+ '11:pm',
47
+ 'asdf',
48
+ '',
49
+ nil]
50
+ times.each do |t|
51
+ assert !TestRecordVt.new(:my_birth_time => t).valid?, "#{t} should be illegal."
52
+ end
53
+ end
54
+
55
+ def test_blank_times_not_allowed
56
+ assert !TestRecordVt.new(:my_birth_time => nil).valid?, "Blank date should be illegal."
57
+ end
58
+
59
+ def test_blank_times_allowed
60
+ assert TestRecordVt2.new(:my_birth_time => nil).valid?, "Blank date should be legal."
61
+ end
62
+
63
+ end
64
+
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gbdev-validates_as_date_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.1
5
+ platform: ruby
6
+ authors:
7
+ - Wes Hays
8
+ - John Dell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-08-15 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Rails gem/plugin that implements an ActiveRecord validation helper called validates_as_date_time which validates U.S. dates, times, and date-times
18
+ email: gems@gbdev.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - CHANGELOG
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - init.rb
31
+ - lib/validates_as_date_time.rb
32
+ - test/validates_as_date_test.rb
33
+ - test/validates_as_time_test.rb
34
+ - test/validates_as_date_time_test.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/gbdev/validates_as_date_time
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Rails gem/plugin to validate format of U.S. dates, times, and date-times
61
+ test_files:
62
+ - test/validates_as_date_test.rb
63
+ - test/validates_as_time_test.rb
64
+ - test/validates_as_date_time_test.rb