date_validator 0.0.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,14 +1,15 @@
1
1
  = date_validator
2
2
 
3
- A simple date validator for Rails 3. And I mean simple:
3
+ A simple date validator for Rails 3.
4
4
 
5
- gem install codegram-date_validator
6
-
7
- In your model:
5
+ gem sources -a http://gemcutter.org/
6
+ gem install date_validator
8
7
 
9
- validates_date_of :expiration_date, :after => Time.now, :before => Time.now + 1.year
8
+ And I mean simple. In your model:
10
9
 
11
- For now these the available options you can use:
10
+ validates :expiration_date, :date => { :after => Time.now, :before => Time.now + 1.year }
11
+
12
+ For now these are the available options you can use:
12
13
 
13
14
  :after
14
15
  :before
@@ -17,7 +18,7 @@ For now these the available options you can use:
17
18
 
18
19
  Pretty much self-explanatory! :) And if you like remarkable, just install this gem as well:
19
20
 
20
- gem install codegram-remarkable_date_validator
21
+ gem install remarkable_date_validator
21
22
 
22
23
  == Note on Patches/Pull Requests
23
24
 
data/Rakefile CHANGED
@@ -9,8 +9,12 @@ begin
9
9
  gem.description = "A simple date validator for Rails 3. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options. Remarkable-friendly :)"
10
10
  gem.email = "info@codegram.com"
11
11
  gem.homepage = "http://github.com/codegram/date_validator"
12
- gem.authors = ["Oriol Gual", "Josep Jaume Rey", "Josep Bach"]
12
+ gem.authors = ["Oriol Gual", "Josep Bach", "Josep Jaume Rey"]
13
+
14
+ gem.add_dependency 'activemodel', '>= 3.0.0.beta3'
15
+
13
16
  gem.add_development_dependency "rspec"
17
+
14
18
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
19
  end
16
20
  Jeweler::GemcutterTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.2.0
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{date_validator}
8
- s.version = "0.0.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Oriol Gual", "Josep Jaume Rey", "Josep M\302\252 Bach"]
12
- s.date = %q{2010-04-26}
11
+ s.authors = ["Oriol Gual", "Josep M\302\252 Bach", "Josep Jaume Rey"]
12
+ s.date = %q{2010-04-27}
13
13
  s.description = %q{A simple date validator for Rails 3. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options. Remarkable-friendly :)}
14
14
  s.email = %q{info@codegram.com}
15
15
  s.extra_rdoc_files = [
@@ -24,8 +24,10 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "date_validator.gemspec",
27
+ "init.rb",
27
28
  "lib/date_validator.rb",
28
29
  "spec/date_validator_spec.rb",
30
+ "spec/spec.opts",
29
31
  "spec/spec_helper.rb"
30
32
  ]
31
33
  s.homepage = %q{http://github.com/codegram/date_validator}
@@ -43,11 +45,15 @@ Gem::Specification.new do |s|
43
45
  s.specification_version = 3
44
46
 
45
47
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0.beta3"])
46
49
  s.add_development_dependency(%q<rspec>, [">= 0"])
47
50
  else
51
+ s.add_dependency(%q<activemodel>, [">= 3.0.0.beta3"])
48
52
  s.add_dependency(%q<rspec>, [">= 0"])
49
53
  end
50
54
  else
55
+ s.add_dependency(%q<activemodel>, [">= 3.0.0.beta3"])
51
56
  s.add_dependency(%q<rspec>, [">= 0"])
52
57
  end
53
58
  end
59
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'date_validator'
@@ -1,35 +1,48 @@
1
- class DateValidator < ActiveModel::EachValidator
2
- CHECKS = { :after => :>, :after_or_equal_to => :>=,
3
- :before => :<, :before_or_equal_to => :<=}.freeze
1
+ module ActiveModel
2
+ module Validations
3
+ class DateValidator < ActiveModel::EachValidator
4
+ CHECKS = { :after => :>, :after_or_equal_to => :>=,
5
+ :before => :<, :before_or_equal_to => :<=}.freeze
4
6
 
5
- def initialize(options)
6
- super(options.reverse_merge(:allow_nil => false))
7
- end
7
+ def initialize(options)
8
+ super(options.reverse_merge(:allow_nil => false))
9
+ end
8
10
 
9
- def check_validity!
10
- keys = CHECKS.keys
11
- options.slice(*keys).each do |option, value|
12
- next if value.is_a?(Time) || value.is_a?(Proc) || value.is_a?(Symbol)
13
- raise ArgumentError, ":#{option} must be a date, a symbol or a proc"
14
- end
15
- end
11
+ def check_validity!
12
+ keys = CHECKS.keys
13
+ options.slice(*keys).each do |option, value|
14
+ next if value.is_a?(Time) || value.is_a?(Proc) || value.is_a?(Symbol) || (defined?(Date) and value.is_a?(Date))
15
+ raise ArgumentError, ":#{option} must be a time, a date, a symbol or a proc"
16
+ end
17
+ end
16
18
 
17
- def validate_each(record, attr_name, value)
19
+ def validate_each(record, attr_name, value)
18
20
 
19
- return if options[:allow_nil] && value.nil?
21
+ return if options[:allow_nil] && value.nil?
20
22
 
21
- unless value
22
- record.errors.add(attr_name, :not_a_date, :value => value, :default => options[:message])
23
- return
24
- end
23
+ unless value
24
+ record.errors.add(attr_name, :not_a_date, :value => value, :default => options[:message])
25
+ return
26
+ end
25
27
 
26
- options.slice(*CHECKS.keys).each do |option, option_value|
27
- option_value = option_value.call(record) if option_value.is_a?(Proc)
28
- option_value = record.send(option_value) if option_value.is_a?(Symbol)
28
+ options.slice(*CHECKS.keys).each do |option, option_value|
29
+ option_value = option_value.call(record) if option_value.is_a?(Proc)
30
+ option_value = record.send(option_value) if option_value.is_a?(Symbol)
29
31
 
30
- unless value.send(CHECKS[option], option_value)
31
- record.errors.add(attr_name, option, :default => options[:message], :value => value, :date => option_value)
32
+ unless value.send(CHECKS[option], option_value)
33
+ record.errors.add(attr_name, option, :default => options[:message], :value => value, :date => option_value)
34
+ end
35
+ end
32
36
  end
33
- end
37
+
38
+ module ClassMethods
39
+
40
+ def validates_date_of(*attr_names)
41
+ validates_with DateValidator, _merge_attributes(attr_names)
42
+ end
43
+
44
+ end
45
+
46
+ end
34
47
  end
35
48
  end
@@ -1,8 +1,60 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "DateValidator" do
4
- it "should"
5
- it "fails" do
6
- fail "hey buddy, you should probably rename this file and start specing for real"
4
+
5
+ before(:each) do
6
+ TestRecord.reset_callbacks(:validate)
7
+ end
8
+
9
+ it "should check validity of the arguments" do
10
+ [3, "foo", 1..6].each do |wrong_argument|
11
+ begin
12
+ TestRecord.validates :expiration_date, :date => {:before => wrong_argument}
13
+ fail "should not accept a #{wrong_argument.class} as an option (only Time, Date, Symbol or Proc)"
14
+ rescue=>e
15
+ fail e unless e.is_a?(ArgumentError)
16
+ end
17
+ end
18
+ end
19
+
20
+ [:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
21
+ [:valid,:invalid].each do |should_be|
22
+
23
+ model_date = case check
24
+ when :after then should_be == :valid ? Time.now + 21000 : Time.now - 1
25
+ when :before then should_be == :valid ? Time.now - 21000 : Time.now + 1
26
+ when :after_or_equal_to then should_be == :valid ? Time.now + 21000 : Time.now - 21000
27
+ when :before_or_equal_to then should_be == :valid ? Time.now - 21000 : Time.now + 21000
28
+ end
29
+
30
+ it "should ensure that an attribute is #{should_be} when #{should_be == :valid ? 'respecting' : 'offending' } the #{check} check" do
31
+ TestRecord.validates :expiration_date, :date => {:"#{check}" => Time.now}
32
+ model = TestRecord.new(model_date)
33
+ should_be == :valid ? model.should(be_valid, "an attribute should be valid when respecting the #{check} check") : model.should(be_invalid, "an attribute should be invalid when offending the #{check} check")
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+ extra_types = [:proc, :symbol]
40
+ # extra_types.push(:date) if defined?(Date) and defined?(DateTime)
41
+
42
+ extra_types.each do |type|
43
+ it "should accept a #{type} as an argument to a check" do
44
+ case type
45
+ when :proc then
46
+ TestRecord.validates :expiration_date, :date => {:after => Proc.new{Time.now + 21000}}
47
+ when :symbol then
48
+ begin
49
+ TestRecord.send(:define_method, :min_date, lambda { Time.now + 21000 })
50
+ TestRecord.validates :expiration_date, :date => {:after => :min_date}
51
+ rescue=>e
52
+ fail "is not accepting a #{type} as an argument to a check"
53
+ end
54
+ # when :date then
55
+ # TestRecord.validates :expiration_date, :date => {:after => Time.now.to_date}
56
+ end
57
+ end
7
58
  end
59
+
8
60
  end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
data/spec/spec_helper.rb CHANGED
@@ -1,15 +1,18 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'date_validator'
3
+
4
+ require 'rubygems'
5
+ require 'active_model'
6
+
7
+ require 'lib/date_validator'
4
8
  require 'spec'
5
9
  require 'spec/autorun'
6
10
 
7
- require 'active_model'
8
- require 'date_validator'
9
11
 
10
12
  class TestRecord
11
13
  include ActiveModel::Validations
12
14
  attr_accessor :expiration_date
15
+
13
16
  def initialize(expiration_date)
14
17
  @expiration_date = expiration_date
15
18
  end
metadata CHANGED
@@ -4,25 +4,40 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
+ - 2
7
8
  - 0
8
- - 0
9
- version: 0.0.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Oriol Gual
13
- - Josep Jaume Rey
14
13
  - "Josep M\xC2\xAA Bach"
14
+ - Josep Jaume Rey
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-04-26 00:00:00 +02:00
19
+ date: 2010-04-27 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
- name: rspec
23
+ name: activemodel
24
24
  prerelease: false
25
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ - beta3
34
+ version: 3.0.0.beta3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
26
41
  requirements:
27
42
  - - ">="
28
43
  - !ruby/object:Gem::Version
@@ -30,7 +45,7 @@ dependencies:
30
45
  - 0
31
46
  version: "0"
32
47
  type: :development
33
- version_requirements: *id001
48
+ version_requirements: *id002
34
49
  description: A simple date validator for Rails 3. Currently supporting :after, :before, :after_or_equal_to and :before_or_equal_to options. Remarkable-friendly :)
35
50
  email: info@codegram.com
36
51
  executables: []
@@ -48,8 +63,10 @@ files:
48
63
  - Rakefile
49
64
  - VERSION
50
65
  - date_validator.gemspec
66
+ - init.rb
51
67
  - lib/date_validator.rb
52
68
  - spec/date_validator_spec.rb
69
+ - spec/spec.opts
53
70
  - spec/spec_helper.rb
54
71
  has_rdoc: true
55
72
  homepage: http://github.com/codegram/date_validator