whyvalidationssuckin96 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.0
1
+ 1.5.1
@@ -8,4 +8,5 @@ require 'whyvalidationssuckin96/macros/validates_length'
8
8
  require 'whyvalidationssuckin96/macros/validates_numericality'
9
9
  require 'whyvalidationssuckin96/macros/validates_presence'
10
10
  require 'whyvalidationssuckin96/macros/validates_url'
11
- require 'whyvalidationssuckin96/macros/validates_email'
11
+ require 'whyvalidationssuckin96/macros/validates_email'
12
+ require 'whyvalidationssuckin96/macros/validates_date'
@@ -0,0 +1,53 @@
1
+ require 'whyvalidationssuckin96/skippable_validation'
2
+ require 'whyvalidationssuckin96/attribute_based_validation'
3
+
4
+ module WhyValidationsSuckIn96
5
+
6
+ # Checks the validity of an date attribute against a regular expression.
7
+ #
8
+ # @example Default usage
9
+ # setup_validations do
10
+ # validates_as_date :start_date
11
+ # end
12
+ class ValidatesDate < Validation
13
+ include WhyValidationsSuckIn96::SkippableValidation
14
+ include WhyValidationsSuckIn96::AttributeBasedValidation
15
+ DefaultDelimiters = %r{[-/]}
16
+ DefaultParser = lambda do |str|
17
+ month, day, year = str.split(DefaultDelimiters, 3)
18
+ {:month => month, :day => day, :year => year}
19
+ end
20
+
21
+ DefaultOptions = {
22
+ :message => "does not match the given date format or is not a valid date",
23
+ :parser => DefaultParser
24
+ }
25
+
26
+ # @param [Object] validatable An object to be validated
27
+ # @param [Hash] options The options to set up the validation with
28
+ # @option options [Regexp] :with A regular expression to check against
29
+ def initialize(validatable, options = {})
30
+ super
31
+ end
32
+
33
+ def validate
34
+ super
35
+ if parse_date
36
+ pass
37
+ else
38
+ fail
39
+ end
40
+ rescue => e
41
+ fail
42
+ end
43
+
44
+ private
45
+
46
+ def parse_date
47
+ parsed = options[:parser].call(attribute_value.to_s)
48
+ Date.civil(parsed[:year].to_i, parsed[:month].to_i, parsed[:day].to_i)
49
+ end
50
+ end # Validation
51
+
52
+ ValidationBuilder.register_macro :validates_as_date, WhyValidationsSuckIn96::ValidatesDate
53
+ end # WhyValidationsSuckIn96
@@ -0,0 +1,36 @@
1
+ require 'teststrap'
2
+
3
+ context "validates date" do
4
+
5
+ should "add a validation macro" do
6
+ WhyValidationsSuckIn96::ValidationBuilder.instance_methods
7
+ end.includes('validates_as_date')
8
+
9
+ context "with some default options" do
10
+ setup do
11
+ WhyValidationsSuckIn96::ValidatesDate.new(Object.new, :attribute => :start_date)
12
+ end
13
+
14
+ should "have a message accessor with a default message" do
15
+ topic.message
16
+ end.equals("does not match the given date format or is not a valid date")
17
+ end # with some default options
18
+
19
+ context "validating an object" do
20
+
21
+ should "fail if given attribute does not match the regular expression" do
22
+ validation = WhyValidationsSuckIn96::ValidatesDate.new(OpenStruct.new(:start_date => "13-22-1969"), :attribute => :start_date)
23
+ validation.validates?
24
+ end.equals(false)
25
+
26
+ should "fail if given attribute is not a valid date" do
27
+ validation = WhyValidationsSuckIn96::ValidatesDate.new(OpenStruct.new(:start_date => "02-31-1969"), :attribute => :start_date)
28
+ validation.validates?
29
+ end.equals(false)
30
+
31
+ should "pass if given attribute matches the regular expression" do
32
+ validation = WhyValidationsSuckIn96::ValidatesDate.new(OpenStruct.new(:start_date => "4-20-1969"), :attribute => :start_date)
33
+ validation.validates?
34
+ end
35
+ end # validating an object
36
+ end # validates format
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{whyvalidationssuckin96}
8
- s.version = "1.5.0"
8
+ s.version = "1.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["gabrielg", "douglasmeyer"]
12
- s.date = %q{2010-01-30}
12
+ s.date = %q{2010-02-15}
13
13
  s.description = %q{A library for setting up model validations, such as in ActiveRecord.}
14
14
  s.email = %q{gabriel.gironda@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -76,6 +76,7 @@ Gem::Specification.new do |s|
76
76
  "lib/whyvalidationssuckin96/macros/validates_acceptance.rb",
77
77
  "lib/whyvalidationssuckin96/macros/validates_associated.rb",
78
78
  "lib/whyvalidationssuckin96/macros/validates_confirmation.rb",
79
+ "lib/whyvalidationssuckin96/macros/validates_date.rb",
79
80
  "lib/whyvalidationssuckin96/macros/validates_email.rb",
80
81
  "lib/whyvalidationssuckin96/macros/validates_exclusion.rb",
81
82
  "lib/whyvalidationssuckin96/macros/validates_format.rb",
@@ -100,6 +101,7 @@ Gem::Specification.new do |s|
100
101
  "test/macros/validates_acceptance_test.rb",
101
102
  "test/macros/validates_associated_test.rb",
102
103
  "test/macros/validates_confirmation_test.rb",
104
+ "test/macros/validates_date_test.rb",
103
105
  "test/macros/validates_email_test.rb",
104
106
  "test/macros/validates_exclusion_test.rb",
105
107
  "test/macros/validates_format_test.rb",
@@ -131,6 +133,7 @@ Gem::Specification.new do |s|
131
133
  "test/macros/validates_acceptance_test.rb",
132
134
  "test/macros/validates_associated_test.rb",
133
135
  "test/macros/validates_confirmation_test.rb",
136
+ "test/macros/validates_date_test.rb",
134
137
  "test/macros/validates_email_test.rb",
135
138
  "test/macros/validates_exclusion_test.rb",
136
139
  "test/macros/validates_format_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whyvalidationssuckin96
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - gabrielg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-30 00:00:00 -06:00
13
+ date: 2010-02-15 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -112,6 +112,7 @@ files:
112
112
  - lib/whyvalidationssuckin96/macros/validates_acceptance.rb
113
113
  - lib/whyvalidationssuckin96/macros/validates_associated.rb
114
114
  - lib/whyvalidationssuckin96/macros/validates_confirmation.rb
115
+ - lib/whyvalidationssuckin96/macros/validates_date.rb
115
116
  - lib/whyvalidationssuckin96/macros/validates_email.rb
116
117
  - lib/whyvalidationssuckin96/macros/validates_exclusion.rb
117
118
  - lib/whyvalidationssuckin96/macros/validates_format.rb
@@ -136,6 +137,7 @@ files:
136
137
  - test/macros/validates_acceptance_test.rb
137
138
  - test/macros/validates_associated_test.rb
138
139
  - test/macros/validates_confirmation_test.rb
140
+ - test/macros/validates_date_test.rb
139
141
  - test/macros/validates_email_test.rb
140
142
  - test/macros/validates_exclusion_test.rb
141
143
  - test/macros/validates_format_test.rb
@@ -189,6 +191,7 @@ test_files:
189
191
  - test/macros/validates_acceptance_test.rb
190
192
  - test/macros/validates_associated_test.rb
191
193
  - test/macros/validates_confirmation_test.rb
194
+ - test/macros/validates_date_test.rb
192
195
  - test/macros/validates_email_test.rb
193
196
  - test/macros/validates_exclusion_test.rb
194
197
  - test/macros/validates_format_test.rb