validators 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/Gemfile CHANGED
@@ -1,4 +1,2 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in validators.gemspec
1
+ source :rubygems
4
2
  gemspec
data/Gemfile.lock CHANGED
@@ -1,40 +1,39 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validators (0.1.0)
5
- activerecord (>= 3.0.0)
4
+ validators (0.1.2)
5
+ activerecord (~> 3.0.5)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
- activemodel (3.0.1)
11
- activesupport (= 3.0.1)
10
+ activemodel (3.0.5)
11
+ activesupport (= 3.0.5)
12
12
  builder (~> 2.1.2)
13
- i18n (~> 0.4.1)
14
- activerecord (3.0.1)
15
- activemodel (= 3.0.1)
16
- activesupport (= 3.0.1)
17
- arel (~> 1.0.0)
13
+ i18n (~> 0.4)
14
+ activerecord (3.0.5)
15
+ activemodel (= 3.0.5)
16
+ activesupport (= 3.0.5)
17
+ arel (~> 2.0.2)
18
18
  tzinfo (~> 0.3.23)
19
- activesupport (3.0.1)
19
+ activesupport (3.0.5)
20
20
  archive-tar-minitar (0.5.2)
21
- arel (1.0.1)
22
- activesupport (~> 3.0.0)
21
+ arel (2.0.9)
23
22
  builder (2.1.2)
24
23
  columnize (0.3.2)
25
24
  diff-lcs (1.1.2)
26
- i18n (0.4.2)
27
- linecache19 (0.5.11)
25
+ i18n (0.5.0)
26
+ linecache19 (0.5.12)
28
27
  ruby_core_source (>= 0.1.4)
29
- rspec (2.1.0)
30
- rspec-core (~> 2.1.0)
31
- rspec-expectations (~> 2.1.0)
32
- rspec-mocks (~> 2.1.0)
33
- rspec-core (2.1.0)
34
- rspec-expectations (2.1.0)
28
+ rspec (2.5.0)
29
+ rspec-core (~> 2.5.0)
30
+ rspec-expectations (~> 2.5.0)
31
+ rspec-mocks (~> 2.5.0)
32
+ rspec-core (2.5.1)
33
+ rspec-expectations (2.5.0)
35
34
  diff-lcs (~> 1.1.2)
36
- rspec-mocks (2.1.0)
37
- ruby-debug-base19 (0.11.24)
35
+ rspec-mocks (2.5.0)
36
+ ruby-debug-base19 (0.11.25)
38
37
  columnize (>= 0.3.1)
39
38
  linecache19 (>= 0.5.11)
40
39
  ruby_core_source (>= 0.1.4)
@@ -42,17 +41,18 @@ GEM
42
41
  columnize (>= 0.3.1)
43
42
  linecache19 (>= 0.5.11)
44
43
  ruby-debug-base19 (>= 0.11.19)
45
- ruby_core_source (0.1.4)
44
+ ruby_core_source (0.1.5)
46
45
  archive-tar-minitar (>= 0.5.2)
47
- sqlite3-ruby (1.3.2)
48
- tzinfo (0.3.23)
46
+ sqlite3 (1.3.3)
47
+ sqlite3-ruby (1.3.3)
48
+ sqlite3 (>= 1.3.3)
49
+ tzinfo (0.3.26)
49
50
 
50
51
  PLATFORMS
51
52
  ruby
52
53
 
53
54
  DEPENDENCIES
54
- activerecord (>= 3.0.0)
55
- rspec (>= 2.0.0)
55
+ rspec (~> 2.5.0)
56
56
  ruby-debug19
57
57
  sqlite3-ruby
58
58
  validators!
data/README.rdoc CHANGED
@@ -59,6 +59,17 @@ Then add it to your Gemfile:
59
59
  validates_ip_address :address, :only => :v6
60
60
  end
61
61
 
62
+ === validates_datetime
63
+
64
+ class Server < ActiveRecord::Base
65
+ validates_datetime :starts_at
66
+ validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
67
+ validates_datetime :ends_at, :after => :now
68
+ validates_datetime :ends_at, :before => :today
69
+
70
+ validates :starts_at, :datetime => true
71
+ end
72
+
62
73
  == Maintainer
63
74
 
64
75
  * Nando Vieira - http://simplesideias.com.br
@@ -0,0 +1,91 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class DatetimeValidator < EachValidator
4
+ def date?(value)
5
+ value.kind_of?(Date) || value.kind_of?(Time)
6
+ end
7
+
8
+ def validate_each(record, attribute, value)
9
+ unless date?(value)
10
+ record.errors.add(attribute, :invalid_date,
11
+ :message => options[:message], :value => value
12
+ )
13
+ end
14
+
15
+ if date?(value)
16
+ validate_after_option(record, attribute, value)
17
+ validate_before_option(record, attribute, value)
18
+ end
19
+ end
20
+
21
+ private
22
+ def date_for(record, value, option)
23
+ date = case option
24
+ when :today
25
+ Date.today
26
+ when :now
27
+ Time.now
28
+ when Time, Date, DateTime, ActiveSupport::TimeWithZone
29
+ option
30
+ when Proc
31
+ option.call(record)
32
+ else
33
+ record.__send__(option) if record.respond_to?(option)
34
+ end
35
+
36
+ if date.kind_of?(Time)
37
+ value = value.to_time
38
+ elsif date.kind_of?(Date)
39
+ value = value.to_date
40
+ end
41
+
42
+ [date, value]
43
+ end
44
+
45
+ def validate_after_option(record, attribute, value)
46
+ return unless options[:after]
47
+
48
+ date, value = date_for(record, value, options[:after])
49
+
50
+ record.errors.add(attribute, :invalid_date_after, {
51
+ :message => options[:after_message],
52
+ :value => value,
53
+ :date => (date?(date) ? I18n.l(date) : date.inspect)
54
+ }) unless value.present? && date.present? && (value && date && value > date)
55
+ end
56
+
57
+ def validate_before_option(record, attribute, value)
58
+ return unless options[:before]
59
+
60
+ date, value = date_for(record, value, options[:before])
61
+
62
+ record.errors.add(attribute, :invalid_date_before, {
63
+ :message => options[:before_message],
64
+ :value => value,
65
+ :date => I18n.l(date)
66
+ }) unless value.present? && date.present? && (value && date && value < date)
67
+ end
68
+ end
69
+
70
+ module ClassMethods
71
+ # Validates weather or not the specified e-mail address is valid.
72
+ #
73
+ # class User < ActiveRecord::Base
74
+ # validates_datetime :birth
75
+ # end
76
+ #
77
+ # Other usages:
78
+ #
79
+ # validates_datetime :starts_at, :after => 2.years.ago
80
+ # validates_datetime :starts_at, :before => 2.years.ago
81
+ # validates_datetime :starts_at, :before => :today
82
+ # validates_datetime :starts_at, :before => :now
83
+ # validates_datetime :starts_at, :before => :ends_at
84
+ # validates_datetime :ends_at, :after => :starts_at
85
+ #
86
+ def validates_datetime(*attr_names)
87
+ validates_with DatetimeValidator, _merge_attributes(attr_names)
88
+ end
89
+ end
90
+ end
91
+ end
@@ -2,7 +2,7 @@ module Validators
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 2
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
data/lib/validators.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "active_record"
2
2
  require "validators/constants"
3
+ require "validators/validates_datetime"
3
4
  require "validators/validates_ip_address"
4
5
  require "validators/validates_email_format_of"
5
6
  require "validators/validates_url_format_of"
data/spec/schema.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :users do |t|
3
3
  t.string :email, :corporate_email, :url
4
+ t.datetime :registered_at, :starts_at, :ends_at
5
+ t.date :birthday
4
6
  end
5
7
 
6
8
  create_table :categories do |t|
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require "validators"
2
+ require "active_support/all"
3
+
4
+ Time.zone = "America/Sao_Paulo"
2
5
 
3
6
  Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
4
7
 
@@ -10,6 +13,7 @@ I18n.load_path << File.dirname(__FILE__) + "/support/translations.yml"
10
13
  RSpec.configure do |config|
11
14
  config.before do
12
15
  I18n.locale = :en
16
+ Time.zone = "America/Sao_Paulo"
13
17
 
14
18
  ActiveRecord::Base.descendants.each do |model|
15
19
  model.delete_all
@@ -0,0 +1,19 @@
1
+ VALID_DATES = [
2
+ "2010-04-26",
3
+ "2010-04-26 23:59:00",
4
+ "26/04/2010",
5
+ "2000-08-01 00:00:00 +0900",
6
+ "Aug 21 2000",
7
+ "Thu Nov 29 14:33:20 GMT 2001",
8
+ Time.now,
9
+ Date.today,
10
+ DateTime.now,
11
+ Time.zone.now
12
+ ]
13
+
14
+ INVALID_DATES = [
15
+ "invalid",
16
+ nil,
17
+ "",
18
+ "4/26"
19
+ ]
@@ -3,6 +3,9 @@ en:
3
3
  errors:
4
4
  messages:
5
5
  record_invalid: "Errors: %{errors}"
6
+ invalid_date: "is not a valid date"
7
+ invalid_date_before: "needs to be before %{date}"
8
+ invalid_date_after: "needs to be after %{date}"
6
9
  invalid_email: "is not a valid address"
7
10
  invalid_url: "is not a valid address"
8
11
  invalid_ipv4_address: "is not a valid IPv4 address"
@@ -0,0 +1,124 @@
1
+ require "spec_helper"
2
+
3
+ describe ".validates_datetime" do
4
+ subject { User.new }
5
+
6
+ context "default options" do
7
+ before do
8
+ User.validates_datetime :registered_at
9
+ User.validates :birthday, :datetime => true
10
+ end
11
+
12
+ VALID_DATES.each do |date|
13
+ it "accepts #{date.inspect} as valid date" do
14
+ subject.registered_at = date
15
+ subject.birthday = date
16
+
17
+ subject.should be_valid
18
+ end
19
+ end
20
+
21
+ INVALID_DATES.each do |date|
22
+ it "rejects #{date.inspect} as valid date" do
23
+ subject.registered_at = date
24
+ subject.birthday = date
25
+ subject.should_not be_valid
26
+
27
+ subject.errors[:registered_at].should_not be_empty
28
+ subject.errors[:birthday].should_not be_empty
29
+ end
30
+ end
31
+
32
+ it "includes default error message" do
33
+ subject.registered_at = nil
34
+ subject.should_not be_valid
35
+
36
+ subject.errors[:registered_at].should == ["is not a valid date"]
37
+ end
38
+ end
39
+
40
+ context "after option" do
41
+ it "rejects when date is set to before :after option" do
42
+ User.validates_datetime :registered_at, :after => 1.week.from_now
43
+ subject.registered_at = Time.now
44
+
45
+ subject.should_not be_valid
46
+ subject.errors[:registered_at].should == ["needs to be after #{I18n.l(1.week.from_now)}"]
47
+ end
48
+
49
+ it "accepts when date is set accordingly to the :after option" do
50
+ User.validates_datetime :registered_at, :after => 1.week.from_now
51
+ subject.registered_at = 2.weeks.from_now
52
+
53
+ subject.should be_valid
54
+ end
55
+
56
+ it "validates using today as date" do
57
+ User.validates_datetime :registered_at, :after => :today
58
+
59
+ subject.registered_at = Time.now
60
+ subject.should_not be_valid
61
+
62
+ subject.registered_at = Date.today
63
+ subject.should_not be_valid
64
+
65
+ subject.registered_at = Date.tomorrow
66
+ subject.should be_valid
67
+
68
+ subject.registered_at = 1.day.from_now
69
+ subject.should be_valid
70
+ end
71
+
72
+ it "validates using now as date" do
73
+ User.validates_datetime :registered_at, :after => :now
74
+
75
+ subject.registered_at = Time.now
76
+ subject.should_not be_valid
77
+
78
+ subject.registered_at = Date.today
79
+ subject.should_not be_valid
80
+
81
+ subject.registered_at = Date.tomorrow
82
+ subject.should be_valid
83
+
84
+ subject.registered_at = 1.day.from_now
85
+ subject.should be_valid
86
+ end
87
+
88
+ it "validates using method as date" do
89
+ User.validates_datetime :starts_at
90
+ User.validates_datetime :ends_at, :after => :starts_at, :if => :starts_at?
91
+
92
+ subject.starts_at = nil
93
+ subject.ends_at = Time.now
94
+ subject.should_not be_valid
95
+ subject.errors[:ends_at].should be_empty
96
+
97
+ subject.starts_at = Time.parse("Apr 26 2010")
98
+ subject.ends_at = Time.parse("Apr 25 2010")
99
+ subject.should_not be_valid
100
+ subject.errors[:ends_at].should == ["needs to be after #{I18n.l(Time.parse("Apr 26 2010"))}"]
101
+
102
+ subject.starts_at = Time.now
103
+ subject.ends_at = 1.hour.from_now
104
+ subject.should be_valid
105
+ end
106
+ end
107
+
108
+ context "before option" do
109
+ it "rejects when date is set to after :before option" do
110
+ User.validates_datetime :registered_at, :before => 1.week.ago
111
+ subject.registered_at = Time.now
112
+
113
+ subject.should_not be_valid
114
+ subject.errors[:registered_at].should == ["needs to be before #{I18n.l(1.week.ago)}"]
115
+ end
116
+
117
+ it "accepts when date is set accordingly to the :before option" do
118
+ User.validates_datetime :registered_at, :before => 1.week.ago
119
+ subject.registered_at = 2.weeks.ago
120
+
121
+ subject.should be_valid
122
+ end
123
+ end
124
+ end
data/validators.gemspec CHANGED
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency "activerecord", ">= 3.0.0"
21
- s.add_development_dependency "rspec", ">= 2.0.0"
20
+ s.add_dependency "activerecord", "~> 3.0.5"
21
+ s.add_development_dependency "rspec", "~> 2.5.0"
22
22
  s.add_development_dependency "sqlite3-ruby"
23
- s.add_development_dependency "ruby-debug19"
23
+ s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
24
24
  end
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validators
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
4
+ prerelease:
5
+ version: 0.1.2
10
6
  platform: ruby
11
7
  authors:
12
8
  - Nando Vieira
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-11-11 00:00:00 -02:00
13
+ date: 2011-04-05 00:00:00 -03:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -23,13 +19,9 @@ dependencies:
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
24
20
  none: false
25
21
  requirements:
26
- - - ">="
22
+ - - ~>
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 3
30
- - 0
31
- - 0
32
- version: 3.0.0
24
+ version: 3.0.5
33
25
  type: :runtime
34
26
  version_requirements: *id001
35
27
  - !ruby/object:Gem::Dependency
@@ -38,13 +30,9 @@ dependencies:
38
30
  requirement: &id002 !ruby/object:Gem::Requirement
39
31
  none: false
40
32
  requirements:
41
- - - ">="
33
+ - - ~>
42
34
  - !ruby/object:Gem::Version
43
- segments:
44
- - 2
45
- - 0
46
- - 0
47
- version: 2.0.0
35
+ version: 2.5.0
48
36
  type: :development
49
37
  version_requirements: *id002
50
38
  - !ruby/object:Gem::Dependency
@@ -55,8 +43,6 @@ dependencies:
55
43
  requirements:
56
44
  - - ">="
57
45
  - !ruby/object:Gem::Version
58
- segments:
59
- - 0
60
46
  version: "0"
61
47
  type: :development
62
48
  version_requirements: *id003
@@ -68,8 +54,6 @@ dependencies:
68
54
  requirements:
69
55
  - - ">="
70
56
  - !ruby/object:Gem::Version
71
- segments:
72
- - 0
73
57
  version: "0"
74
58
  type: :development
75
59
  version_requirements: *id004
@@ -84,6 +68,7 @@ extra_rdoc_files: []
84
68
 
85
69
  files:
86
70
  - .gitignore
71
+ - .rspec
87
72
  - Gemfile
88
73
  - Gemfile.lock
89
74
  - README.rdoc
@@ -91,6 +76,7 @@ files:
91
76
  - lib/validators.rb
92
77
  - lib/validators/constants.rb
93
78
  - lib/validators/ip.rb
79
+ - lib/validators/validates_datetime.rb
94
80
  - lib/validators/validates_email_format_of.rb
95
81
  - lib/validators/validates_ip_address.rb
96
82
  - lib/validators/validates_ownership_of.rb
@@ -98,12 +84,14 @@ files:
98
84
  - lib/validators/version.rb
99
85
  - spec/schema.rb
100
86
  - spec/spec_helper.rb
87
+ - spec/support/dates.rb
101
88
  - spec/support/emails.rb
102
89
  - spec/support/ips.rb
103
90
  - spec/support/models.rb
104
91
  - spec/support/translations.yml
105
92
  - spec/support/urls.rb
106
93
  - spec/validators/ip_spec.rb
94
+ - spec/validators/validates_datetime_spec.rb
107
95
  - spec/validators/validates_email_format_of_spec.rb
108
96
  - spec/validators/validates_ip_address_spec.rb
109
97
  - spec/validators/validates_ownership_of_spec.rb
@@ -123,33 +111,31 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
111
  requirements:
124
112
  - - ">="
125
113
  - !ruby/object:Gem::Version
126
- segments:
127
- - 0
128
114
  version: "0"
129
115
  required_rubygems_version: !ruby/object:Gem::Requirement
130
116
  none: false
131
117
  requirements:
132
118
  - - ">="
133
119
  - !ruby/object:Gem::Version
134
- segments:
135
- - 0
136
120
  version: "0"
137
121
  requirements: []
138
122
 
139
123
  rubyforge_project:
140
- rubygems_version: 1.3.7
124
+ rubygems_version: 1.6.0
141
125
  signing_key:
142
126
  specification_version: 3
143
127
  summary: Add some nice Rails 3 ActiveRecord validators.
144
128
  test_files:
145
129
  - spec/schema.rb
146
130
  - spec/spec_helper.rb
131
+ - spec/support/dates.rb
147
132
  - spec/support/emails.rb
148
133
  - spec/support/ips.rb
149
134
  - spec/support/models.rb
150
135
  - spec/support/translations.yml
151
136
  - spec/support/urls.rb
152
137
  - spec/validators/ip_spec.rb
138
+ - spec/validators/validates_datetime_spec.rb
153
139
  - spec/validators/validates_email_format_of_spec.rb
154
140
  - spec/validators/validates_ip_address_spec.rb
155
141
  - spec/validators/validates_ownership_of_spec.rb