remarkable_date_validator 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = remarkable_date_validator
2
2
 
3
- Remarkable matchers for date_validator!
3
+ Remarkable matchers for date_validator! (a date validator for Rails 3: http://github.com/codegram/date_validator/)
4
4
 
5
5
  Easy peasy. Add gemcutter sources:
6
6
 
@@ -26,9 +26,7 @@ Enjoy!
26
26
  * Make your feature addition or bug fix.
27
27
  * Add tests for it. This is important so I don't break it in a
28
28
  future version unintentionally.
29
- * Commit, do not mess with rakefile, version, or history.
30
- (if you want to have your own version, that is fine but
31
- bump version in a commit by itself I can ignore when I pull)
29
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
32
30
  * Send me a pull request. Bonus points for topic branches.
33
31
 
34
32
  == Copyright
data/Rakefile CHANGED
@@ -13,6 +13,9 @@ begin
13
13
 
14
14
  gem.add_dependency "remarkable", '>=4.0.0.alpha1'
15
15
  gem.add_dependency 'remarkable_activemodel', '>=4.0.0.alpha1'
16
+ gem.add_dependency 'date_validator', '>=0.4'
17
+ gem.add_development_dependency 'active_record', '>=3.0.0.beta3'
18
+ gem.add_development_dependency 'rspec'
16
19
 
17
20
  end
18
21
  Jeweler::GemcutterTasks.new
@@ -20,6 +23,12 @@ rescue LoadError
20
23
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
24
  end
22
25
 
26
+ require 'spec/rake/spectask'
27
+ Spec::Rake::SpecTask.new(:spec) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.spec_files = FileList['spec/**/*_spec.rb']
30
+ end
31
+
23
32
  require 'rake/rdoctask'
24
33
  Rake::RDocTask.new do |rdoc|
25
34
  if File.exist?('VERSION')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -47,7 +47,7 @@ module Remarkable
47
47
  #
48
48
  def after_maximum?
49
49
  @maximum_values.each do |key, value|
50
- return false, :date => value unless bad?(value + 1.day, default_message_for(key))
50
+ return false, :date => value unless bad?(value + 5.days, default_message_for(key))
51
51
  end
52
52
  true
53
53
  end
@@ -57,7 +57,7 @@ module Remarkable
57
57
  #
58
58
  def before_minimum?
59
59
  @minimum_values.each do |key, value|
60
- return false, :date => value unless bad?(value - 1.day, default_message_for(key))
60
+ return false, :date => value unless bad?(value - 5.days, default_message_for(key))
61
61
  end
62
62
  true
63
63
  end
@@ -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{remarkable_date_validator}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oriol Gual", "Josep M\302\252 Bach", "Josep Jaume Rey"]
12
- s.date = %q{2010-04-27}
12
+ s.date = %q{2010-05-14}
13
13
  s.description = %q{Remarkable matchers for date_validator}
14
14
  s.email = %q{info@codegram.com}
15
15
  s.extra_rdoc_files = [
@@ -27,14 +27,21 @@ Gem::Specification.new do |s|
27
27
  "lib/en.yml",
28
28
  "lib/remarkable_date_validator.rb",
29
29
  "lib/remarkable_date_validator/validate_date_of_matcher.rb",
30
- "lib/validate_date_of_matcher.rb",
31
- "remarkable_date_validator.gemspec"
30
+ "remarkable_date_validator.gemspec",
31
+ "spec/model_builder.rb",
32
+ "spec/remarkable_date_validator_spec.rb",
33
+ "spec/spec_helper.rb"
32
34
  ]
33
35
  s.homepage = %q{http://github.com/codegram/remarkable_date_validator}
34
36
  s.rdoc_options = ["--charset=UTF-8"]
35
37
  s.require_paths = ["lib"]
36
38
  s.rubygems_version = %q{1.3.6}
37
39
  s.summary = %q{Remarkable matchers for date_validator}
40
+ s.test_files = [
41
+ "spec/model_builder.rb",
42
+ "spec/remarkable_date_validator_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
38
45
 
39
46
  if s.respond_to? :specification_version then
40
47
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -43,12 +50,22 @@ Gem::Specification.new do |s|
43
50
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
51
  s.add_runtime_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
45
52
  s.add_runtime_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
53
+ s.add_runtime_dependency(%q<date_validator>, [">= 0.4"])
54
+ s.add_development_dependency(%q<active_record>, [">= 3.0.0.beta3"])
55
+ s.add_development_dependency(%q<rspec>, [">= 0"])
46
56
  else
47
57
  s.add_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
48
58
  s.add_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
59
+ s.add_dependency(%q<date_validator>, [">= 0.4"])
60
+ s.add_dependency(%q<active_record>, [">= 3.0.0.beta3"])
61
+ s.add_dependency(%q<rspec>, [">= 0"])
49
62
  end
50
63
  else
51
64
  s.add_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
52
65
  s.add_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
66
+ s.add_dependency(%q<date_validator>, [">= 0.4"])
67
+ s.add_dependency(%q<active_record>, [">= 3.0.0.beta3"])
68
+ s.add_dependency(%q<rspec>, [">= 0"])
53
69
  end
54
70
  end
71
+
@@ -0,0 +1,100 @@
1
+ # This is based on Remarkable based on Shoulda model builder for Test::Unit.
2
+ #
3
+ module ModelBuilder
4
+ def self.included(base)
5
+ return unless base.name =~ /^Spec/
6
+
7
+ base.class_eval do
8
+ after(:each) do
9
+ if @defined_constants
10
+ @defined_constants.each do |class_name|
11
+ Object.send(:remove_const, class_name)
12
+ end
13
+ end
14
+
15
+ if @created_tables
16
+ @created_tables.each do |table_name|
17
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{table_name}")
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ base.extend ClassMethods
24
+ end
25
+
26
+ def create_table(table_name, &block)
27
+ connection = ActiveRecord::Base.connection
28
+
29
+ begin
30
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
31
+ connection.create_table(table_name, &block)
32
+ @created_tables ||= []
33
+ @created_tables << table_name
34
+ connection
35
+ rescue Exception => e
36
+ connection.execute("DROP TABLE IF EXISTS #{table_name}")
37
+ raise e
38
+ end
39
+ end
40
+
41
+ def define_constant(class_name, base, &block)
42
+ class_name = class_name.to_s.camelize
43
+
44
+ klass = Class.new(base)
45
+ Object.const_set(class_name, klass)
46
+
47
+ klass.class_eval(&block) if block_given?
48
+
49
+ @defined_constants ||= []
50
+ @defined_constants << class_name
51
+
52
+ klass
53
+ end
54
+
55
+ def define_model_class(class_name, &block)
56
+ define_constant(class_name, ActiveRecord::Base, &block)
57
+ end
58
+
59
+ def define_model(name, columns = {}, &block)
60
+ class_name = name.to_s.pluralize.classify
61
+ table_name = class_name.tableize
62
+
63
+ table = columns.delete(:table) || lambda {|table|
64
+ columns.each do |name, type|
65
+ table.column name, *type
66
+ end
67
+ }
68
+
69
+ create_table(table_name, &table)
70
+
71
+ klass = define_model_class(class_name, &block)
72
+ instance = klass.new
73
+
74
+ self.class.subject { instance } if self.class.respond_to?(:subject)
75
+ instance
76
+ end
77
+
78
+ module ClassMethods
79
+ # This is a macro to run validations of boolean optionals such as :allow_nil
80
+ # and :allow_blank. This macro tests all scenarios. The specs must have a
81
+ # define_and_validate method defined.
82
+ #
83
+ def create_optional_boolean_specs(optional, base, options={})
84
+ base.describe "with #{optional} option" do
85
+ it { should define_and_validate(options.merge(optional => true)).send(optional) }
86
+ it { should define_and_validate(options.merge(optional => false)).send(optional, false) }
87
+ it { should_not define_and_validate(options.merge(optional => true)).send(optional, false) }
88
+ it { should_not define_and_validate(options.merge(optional => false)).send(optional) }
89
+ end
90
+ end
91
+
92
+ def create_message_specs(base)
93
+ base.describe "with message option" do
94
+ it { should define_and_validate(:message => 'valid_message').message('valid_message') }
95
+ it { should_not define_and_validate(:message => 'not_valid').message('valid_message') }
96
+ end
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper.rb")
2
+
3
+ require 'date_validator'
4
+
5
+ create_table "test_records" do end
6
+
7
+ define_model('TestRecord', :name => :string, :expiration_date => :date)
8
+
9
+ describe Remarkable::ActiveModel::Matchers::ValidateDateOfMatcher do
10
+ before :each do
11
+ TestRecord.reset_callbacks(:validate)
12
+ @model = TestRecord.new
13
+ end
14
+
15
+ describe "validate_date_of" do
16
+
17
+ [:after, :before, :after_or_equal_to, :before_or_equal_to].each do |check|
18
+
19
+ it "should validate that an attribute expects to be #{check} a date" do
20
+ TestRecord.validates :expiration_date, :date => {:"#{check}" => Time.now}
21
+ validate_date_of(:expiration_date, :"#{check}" => Time.now).matches?(@model).should be_true
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+
3
+ RAILS_ENV = "test"
4
+ RAILS_VERSION = ENV['RAILS_VERSION'] || '3.0.0.beta3'
5
+
6
+ # gem 'activesupport', RAILS_VERSION
7
+ # require 'active_support'
8
+
9
+ gem 'activerecord', RAILS_VERSION
10
+ require 'active_record'
11
+
12
+ ActiveRecord::Base.establish_connection(
13
+ :adapter => 'sqlite3',
14
+ :database => ':memory:'
15
+ )
16
+
17
+
18
+ gem 'remarkable', '>=4.0.0.alpha1'
19
+ gem 'remarkable_activemodel', '>=4.0.0.alpha1'
20
+ gem 'remarkable_activerecord', '>=4.0.0.alpha1'
21
+
22
+ require 'remarkable_activemodel'
23
+ require 'remarkable_activerecord'
24
+
25
+ dir = File.dirname(__FILE__)
26
+ require File.join(dir, "..", "lib", "remarkable_date_validator")
27
+ require File.join(dir, "model_builder")
28
+ include ModelBuilder
29
+
30
+ Remarkable.include_matchers!(Remarkable::ActiveModel, Rspec::Core::ExampleGroup)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Oriol Gual
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-04-27 00:00:00 +02:00
19
+ date: 2010-05-14 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,46 @@ dependencies:
49
49
  version: 4.0.0.alpha1
50
50
  type: :runtime
51
51
  version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: date_validator
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 0
61
+ - 4
62
+ version: "0.4"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: active_record
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 3
74
+ - 0
75
+ - 0
76
+ - beta3
77
+ version: 3.0.0.beta3
78
+ type: :development
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: rspec
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
52
92
  description: Remarkable matchers for date_validator
53
93
  email: info@codegram.com
54
94
  executables: []
@@ -69,8 +109,10 @@ files:
69
109
  - lib/en.yml
70
110
  - lib/remarkable_date_validator.rb
71
111
  - lib/remarkable_date_validator/validate_date_of_matcher.rb
72
- - lib/validate_date_of_matcher.rb
73
112
  - remarkable_date_validator.gemspec
113
+ - spec/model_builder.rb
114
+ - spec/remarkable_date_validator_spec.rb
115
+ - spec/spec_helper.rb
74
116
  has_rdoc: true
75
117
  homepage: http://github.com/codegram/remarkable_date_validator
76
118
  licenses: []
@@ -101,5 +143,7 @@ rubygems_version: 1.3.6
101
143
  signing_key:
102
144
  specification_version: 3
103
145
  summary: Remarkable matchers for date_validator
104
- test_files: []
105
-
146
+ test_files:
147
+ - spec/model_builder.rb
148
+ - spec/remarkable_date_validator_spec.rb
149
+ - spec/spec_helper.rb
@@ -1,112 +0,0 @@
1
- module Remarkable
2
- module ActiveModel
3
- module Matchers
4
- class ValidateDateOfMatcher < Remarkable::ActiveModel::Base #:nodoc:
5
- arguments :collection => :attributes, :as => :attribute
6
-
7
- optional :after_or_equal_to, :after,
8
- :before_or_equal_to, :before, :message
9
-
10
- optional :allow_nil, :default => true
11
-
12
- collection_assertions :only_date_values?, :allow_nil?,
13
- :before_minimum?, :after_maximum?
14
-
15
- # Before assertions, we rearrange the values.
16
- #
17
- # Notice that :before gives a maximum value while :after given
18
- # a minimum value.
19
- #
20
- before_assert do
21
- @maximum_values = {}
22
- @minimum_values = {}
23
-
24
- if value = @options[:before]
25
- @maximum_values[:before] = value - 1.day
26
- elsif value = @options[:after]
27
- @minimum_values[:after] = value + 1.day
28
- elsif value = @options[:before_or_equal_to]
29
- @maximum_values[:before_or_equal_to] = value
30
- elsif value = @options[:after_or_equal_to]
31
- @minimum_values[:after_or_equal_to] = value
32
- end
33
- end
34
-
35
- private
36
-
37
- def allow_nil?
38
- super(default_message_for(:not_a_date))
39
- end
40
-
41
- def only_date_values?
42
- bad?("abcd", default_message_for(:not_a_date))
43
- end
44
-
45
- # Check after_maximum? for before and
46
- # before_or_equal_to options.
47
- #
48
- def after_maximum?
49
- @maximum_values.each do |key, value|
50
- return false, :date => value unless bad?(value + 1.day, default_message_for(key))
51
- end
52
- true
53
- end
54
-
55
- # Check before_minimum? for after and
56
- # after_or_equal_to options.
57
- #
58
- def before_minimum?
59
- @minimum_values.each do |key, value|
60
- return false, :date => value unless bad?(value - 1.day, default_message_for(key))
61
- end
62
- true
63
- end
64
-
65
- # Returns the default message for each key (:after, :before, ...).
66
- # If the user provided a message, we use it, otherwise we should use
67
- # the given key as message.
68
- #
69
- # For example, a default_message_for(:after), if none is provided, will be
70
- # :after. So we have create :after_message in the options hash, that when
71
- # called later, will return :after.
72
- #
73
- def default_message_for(key)
74
- if @options[:message]
75
- :message
76
- else
77
- @options[:"#{key}_message"] = key
78
- :"#{key}_message"
79
- end
80
- end
81
- end
82
-
83
- # Ensures that the given attributes accepts only Time, Date, Symbol or Proc objects.
84
- #
85
- # == Options
86
- #
87
- # * <tt>:before</tt> - when supplied, checks if attributes are only valid when before given value
88
- # * <tt>:after</tt> - when supplied, checks if attributes are only valid when after given value
89
- # * <tt>:before_or_equal_to</tt> - when supplied, checks if attributes are only valid when before or equal to given value
90
- # * <tt>:after_or_equal_to</tt> - when supplied, checks if attributes are only valid when after or equal to given value
91
- # * <tt>:message</tt> - value the test expects to find in <tt>errors[:attribute]</tt>.
92
- # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.not_a_date')</tt>
93
- #
94
- # == Examples
95
- #
96
- # it { should validate_date_of(:expiration_date, :after => Time.now) }
97
- # it { should validate_date_of(:expiration_date, :before => Time.now) }
98
- #
99
- # should_validate_date_of :bought_at, :checked_at
100
- # should_validate_date_of :bought_at, :after => Time.now
101
- #
102
- # should_validate_date_of :bought_at do |m|
103
- # m.after = Time.now
104
- # end
105
- #
106
- def validate_date_of(*attributes, &block)
107
- ValidateDateOfMatcher.new(*attributes, &block).spec(self)
108
- end
109
-
110
- end
111
- end
112
- end