remarkable_date_validator 0.4.0

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Josep Mª Bach
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = remarkable_date_validator
2
+
3
+ Remarkable matchers for date_validator!
4
+
5
+ Easy peasy. Add gemcutter sources:
6
+
7
+ gem sources -a http://gemcutter.org/
8
+
9
+ Install date_validator if you haven't already:'
10
+
11
+ gem install date_validator
12
+
13
+ ...and there go the lovely matchers!
14
+
15
+ gem install remarkable_date_validator
16
+
17
+ In your model specs, then:
18
+
19
+ should_validate_date_of :whatever_date, :date => {:before => Time.now + 2.years, :after_or_equal_to => Time.now}
20
+
21
+ Enjoy!
22
+
23
+ == Note on Patches/Pull Requests
24
+
25
+ * Fork the project.
26
+ * Make your feature addition or bug fix.
27
+ * Add tests for it. This is important so I don't break it in a
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)
32
+ * Send me a pull request. Bonus points for topic branches.
33
+
34
+ == Copyright
35
+
36
+ Copyright (c) 2010 Codegram. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "remarkable_date_validator"
8
+ gem.summary = "Remarkable matchers for date_validator"
9
+ gem.description = "Remarkable matchers for date_validator"
10
+ gem.email = "info@codegram.com"
11
+ gem.homepage = "http://github.com/codegram/remarkable_date_validator"
12
+ gem.authors = ["Oriol Gual", "Josep Mª Bach", "Josep Jaume Rey"]
13
+
14
+ gem.add_dependency "remarkable", '>=4.0.0.alpha1'
15
+ gem.add_dependency 'remarkable_activemodel', '>=4.0.0.alpha1'
16
+
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/rdoctask'
24
+ Rake::RDocTask.new do |rdoc|
25
+ if File.exist?('VERSION')
26
+ version = File.read('VERSION')
27
+ else
28
+ version = ""
29
+ end
30
+
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = "remarkable_date_validator #{version}"
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
data/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ if RAILS_ENV == "test"
2
+
3
+ require 'remarkable_activemodel'
4
+
5
+ Remarkable.include_matchers!(Remarkable::ActiveModel, Rspec::Core::ExampleGroup)
6
+
7
+ end
data/lib/en.yml ADDED
@@ -0,0 +1,21 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ remarkable:
6
+ active_model:
7
+ validate_date_of:
8
+ description: "ensure date format and limits of {{attributes}}"
9
+ expectations:
10
+ only_date_values: "{{subject_name}} to allow only date values for {{value}}"
11
+ after_maximum: "{{subject_name}} to be invalid when {{attribute}} is after {{value}}"
12
+ before_minimum: "{{subject_name}} to be invalid when {{attribute}} is before {{value}}"
13
+ optionals:
14
+ before:
15
+ positive: "is less than {{inspect}}"
16
+ after:
17
+ positive: "is after {{inspect}}"
18
+ before_or_equal_to:
19
+ positive: "is before or equal to {{inspect}}"
20
+ after_or_equal_to:
21
+ positive: "is after or equal to {{inspect}}"
@@ -0,0 +1,5 @@
1
+ require 'remarkable'
2
+ require 'remarkable_activemodel'
3
+
4
+ require 'remarkable_date_validator/validate_date_of_matcher'
5
+
@@ -0,0 +1,112 @@
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
@@ -0,0 +1,112 @@
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
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{remarkable_date_validator}
8
+ s.version = "0.4.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Oriol Gual", "Josep M\302\252 Bach", "Josep Jaume Rey"]
12
+ s.date = %q{2010-04-27}
13
+ s.description = %q{Remarkable matchers for date_validator}
14
+ s.email = %q{info@codegram.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "init.rb",
27
+ "lib/en.yml",
28
+ "lib/remarkable_date_validator.rb",
29
+ "lib/remarkable_date_validator/validate_date_of_matcher.rb",
30
+ "lib/validate_date_of_matcher.rb",
31
+ "remarkable_date_validator.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/codegram/remarkable_date_validator}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.6}
37
+ s.summary = %q{Remarkable matchers for date_validator}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
45
+ s.add_runtime_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
46
+ else
47
+ s.add_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
48
+ s.add_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<remarkable>, [">= 4.0.0.alpha1"])
52
+ s.add_dependency(%q<remarkable_activemodel>, [">= 4.0.0.alpha1"])
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remarkable_date_validator
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
+ platform: ruby
11
+ authors:
12
+ - Oriol Gual
13
+ - "Josep M\xC2\xAA Bach"
14
+ - Josep Jaume Rey
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-04-27 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: remarkable
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 4
31
+ - 0
32
+ - 0
33
+ - alpha1
34
+ version: 4.0.0.alpha1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: remarkable_activemodel
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 4
46
+ - 0
47
+ - 0
48
+ - alpha1
49
+ version: 4.0.0.alpha1
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Remarkable matchers for date_validator
53
+ email: info@codegram.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - init.rb
69
+ - lib/en.yml
70
+ - lib/remarkable_date_validator.rb
71
+ - lib/remarkable_date_validator/validate_date_of_matcher.rb
72
+ - lib/validate_date_of_matcher.rb
73
+ - remarkable_date_validator.gemspec
74
+ has_rdoc: true
75
+ homepage: http://github.com/codegram/remarkable_date_validator
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.6
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Remarkable matchers for date_validator
104
+ test_files: []
105
+