birth_date_validator 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b5a31fcb07e05495ee29fe4ef37595b5d0edb6d
4
+ data.tar.gz: a8236b243349fa2d66ea3403a892ed388b89bb6b
5
+ SHA512:
6
+ metadata.gz: 27c7d82bd110439e65e370ce5678703d5353878538b3448af7d1b7d346c6bdaf034ce2082582a6eb92b6b1d26cb56a4933ed527810fa614917b4e463d5b3d351
7
+ data.tar.gz: 174951b37ecd6863530592bfd546df0dfae4bf32e67a06ab82724db469a871143bcb3114584b764b4d63d3241c7719f74f7ac3f93bba5eb6e1a75ab041413e87
@@ -0,0 +1,23 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ class BirthDateValidator < ActiveModel::EachValidator
12
+ def validate_each(record, attribute, value)
13
+ invalid = if options[:almost].present?
14
+ value.year > options[:almost].year
15
+ elsif options[:less_then].present?
16
+ value.year <= options[:less_then].year
17
+ elsif options[:range].present?
18
+ value.year > options[:range].first.year || value.year < options[:range].last.year
19
+ end
20
+
21
+ record.errors.add(attribute, options.fetch(:message, :invalid)) if invalid
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ class BirthDateValidator
12
+ VERSION = '0.1'
13
+ end
@@ -0,0 +1,10 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+ require 'birth_date_validator/validator'
@@ -0,0 +1,67 @@
1
+ require './spec_helper'
2
+
3
+ describe BirthDateValidator do
4
+ describe 'user has to have almost 18 years' do
5
+ context '19 years old' do
6
+ it 'is valid' do
7
+ expect(TestUserOverAge.new(birth_date: 19.years.ago)).to be_valid
8
+ end
9
+ end
10
+
11
+ context '17 years old' do
12
+ subject { TestUserOverAge.new(birth_date: 17.years.ago) }
13
+ before { subject.valid? }
14
+
15
+ it 'is not valid' do
16
+ expect(subject).to_not be_valid
17
+ expect(subject.errors[:birth_date][0]).to include 'older'
18
+ end
19
+ end
20
+
21
+ context '18 years old' do
22
+ it 'is valid' do
23
+ expect(TestUserOverAge.new(birth_date: 18.years.ago)).to be_valid
24
+ end
25
+ end
26
+ end
27
+
28
+ describe 'user has to have less then 18 years' do
29
+ context '19 years old' do
30
+ it 'is not valid' do
31
+ expect(TestUserUnderAge.new(birth_date: 19.years.ago)).to_not be_valid
32
+ end
33
+ end
34
+
35
+ context '17 years old' do
36
+ it 'is valid' do
37
+ expect(TestUserUnderAge.new(birth_date: 17.years.ago)).to be_valid
38
+ end
39
+ end
40
+
41
+ context '18 years old' do
42
+ it 'is not valid' do
43
+ expect(TestUserUnderAge.new(birth_date: 18.years.ago)).to_not be_valid
44
+ end
45
+ end
46
+ end
47
+
48
+ describe 'user has to have from 18 and 30 years' do
49
+ context '17 years old' do
50
+ it 'is not valid' do
51
+ expect(TestUserRange.new(birth_date: 17.years.ago)).to_not be_valid
52
+ end
53
+ end
54
+
55
+ context '30 years old' do
56
+ it 'is valid' do
57
+ expect(TestUserRange.new(birth_date: 30.years.ago)).to be_valid
58
+ end
59
+ end
60
+
61
+ context '31 years old' do
62
+ it 'is not valid' do
63
+ expect(TestUserRange.new(birth_date: 31.years.ago)).to_not be_valid
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,28 @@
1
+ require 'rspec'
2
+ require 'active_model'
3
+ require 'active_support/all'
4
+ require 'birth_date_validator'
5
+
6
+ class TestModel
7
+ include ActiveModel::Validations
8
+
9
+ def initialize(attributes = {})
10
+ @attributes = attributes
11
+ end
12
+
13
+ def read_attribute_for_validation(key)
14
+ @attributes[key]
15
+ end
16
+ end
17
+
18
+ class TestUserOverAge < TestModel
19
+ validates :birth_date, birth_date: { almost: 18.years.ago, message: 'come back when you will be older' }
20
+ end
21
+
22
+ class TestUserUnderAge < TestModel
23
+ validates :birth_date, birth_date: { less_then: 18.years.ago }
24
+ end
25
+
26
+ class TestUserRange < TestModel
27
+ validates :birth_date, birth_date: { range: 18.years.ago..30.years.ago }
28
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: birth_date_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Capuano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Set through various parameters the minimum or the range of ages in which
70
+ your users have to be.
71
+ email: webmaster@giovannicapuano.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/birth_date_validator.rb
77
+ - lib/birth_date_validator/validator.rb
78
+ - lib/birth_date_validator/version.rb
79
+ - spec/birth_date_validator_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/RoxasShadow
82
+ licenses:
83
+ - WTFPL
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Birth date validator for ActiveModel.
105
+ test_files:
106
+ - spec/birth_date_validator_spec.rb
107
+ - spec/spec_helper.rb