rn 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 2.0.0@rn
data/.travis.yml ADDED
@@ -0,0 +1,28 @@
1
+ language: ruby
2
+ rvm:
3
+ - ruby-head
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - 1.9.2
7
+ - 1.8.7
8
+ - ree
9
+ - jruby-head
10
+ - jruby-19mode
11
+ - jruby-18mode
12
+ - rbx-19mode
13
+ - rbx-18mode
14
+ gemfile:
15
+ - gemfiles/3.2.gemfile
16
+ - gemfiles/4.0.gemfile
17
+ matrix:
18
+ exclude:
19
+ - rvm: 1.9.2
20
+ gemfile: gemfiles/4.0.gemfile
21
+ - rvm: 1.8.7
22
+ gemfile: gemfiles/4.0.gemfile
23
+ - rvm: ree
24
+ gemfile: gemfiles/4.0.gemfile
25
+ - rvm: jruby-18mode
26
+ gemfile: gemfiles/4.0.gemfile
27
+ - rvm: rbx-18mode
28
+ gemfile: gemfiles/4.0.gemfile
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rn.gemspec
4
+ gemspec
5
+
6
+ gem 'activemodel', '~> 4.0.0.beta1'
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ralph Rooding
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ [![Build Status](https://travis-ci.org/rrooding/rn.png?branch=master)](https://travis-ci.org/rrooding/rn)
2
+ [![Code Climate](https://codeclimate.com/github/rrooding/rn.png)](https://codeclimate.com/github/rrooding/rn)
3
+
4
+ # Rn
5
+
6
+ This gem adds a validator for the Belgium government registration numbers (also known as *rijksregisternummer* or *RN* for short). You can use it to check if the registration number is a valid number, you can also optionally check against a users birthday.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'rn'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rn
21
+
22
+ ## Usage
23
+
24
+ Using the RnValidator:
25
+
26
+ class User < ActiveRecord::Base
27
+ validates :number, rn: true
28
+ end
29
+
30
+ This will only check if the number conforms to the rules specified by the Belgian government.
31
+
32
+ You can also validate against the users birthday:
33
+
34
+ class User < ActiveRecord::Base
35
+ validates :number, rn: { birthday: true }
36
+ end
37
+
38
+ This will look for the users birthday in the `birthday` property which has to be a `Date`. If you want to use another method or column, you can do that as follows:
39
+
40
+ class User < ActiveRecord::Base
41
+ validates :number, rn: { birthday: true, birthday_column: :date_of_birth }
42
+ end
43
+
44
+ ## I18n
45
+
46
+ Add the following to your locale file:
47
+
48
+ en:
49
+ activemodel:
50
+ errors:
51
+ models:
52
+ MODEL:
53
+ attributes:
54
+ ATTRIBUTE:
55
+ invalid_registration_number: 'is not a valid registration number'
56
+ no_matching_birthday: 'does not match birthday'
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run rspec'
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activemodel', '~> 3.2.13'
4
+ gem 'rn', :path => '../'
5
+
6
+ gemspec :path => '../'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activemodel', '~> 4.0.0.beta1'
4
+ gem 'rn', :path => '../'
5
+
6
+ gemspec :path => '../'
data/lib/rn.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'rn/version'
2
+ require 'rn/registration_number'
3
+ require 'rn/validator'
@@ -0,0 +1,46 @@
1
+ require 'date'
2
+
3
+ module Rn
4
+ class RegistrationNumber
5
+ def initialize(registration_number)
6
+ raise ArgumentError.new('cannot be empty') if registration_number.nil? || registration_number.empty?
7
+ raise ArgumentError.new('must be 11 characters') if registration_number.length != 11
8
+ raise ArgumentError.new('can only contain 0-9') unless (registration_number =~ /\D/).nil?
9
+
10
+ @number = registration_number
11
+
12
+ @year, @month, @day, @follow, @control = @number.scan(/\A(\d{2})(\d{2})(\d{2})(\d{3})(\d{2})\z/).flatten
13
+ end
14
+
15
+ def birthday
16
+ @birthday ||= parse_birthday
17
+ end
18
+
19
+ def valid?
20
+ pre_2000_control? || post_2000_control?
21
+ end
22
+
23
+ private
24
+
25
+ def parse_birthday
26
+ base = pre_2000_control? ? 1900 : 2000
27
+ Date.new(@year.to_i + base, @month.to_i, @day.to_i)
28
+ end
29
+
30
+ def pre_2000_control?
31
+ @control.to_i == 97 - (control_sequence.to_i % 97)
32
+ end
33
+
34
+ def post_2000_control?
35
+ @control.to_i == 97 - (extended_control_sequence.to_i % 97)
36
+ end
37
+
38
+ def control_sequence
39
+ [@year, @month, @day, @follow].join
40
+ end
41
+
42
+ def extended_control_sequence
43
+ ['2', control_sequence].join
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ require 'active_model'
2
+ require 'active_model/validations'
3
+
4
+ class RnValidator < ActiveModel::EachValidator
5
+ def validate_each(record, attribute, value)
6
+ check_birthday = options.fetch(:birthday, false)
7
+ birthday_accessor = options.fetch(:birthday_column, :birthday)
8
+
9
+ begin
10
+ number = Rn::RegistrationNumber.new(value)
11
+
12
+ record.errors.add(attribute, :invalid_registration_number, options) unless number.valid?
13
+
14
+ if check_birthday
15
+ birthday = record.send(birthday_accessor)
16
+ record.errors.add(attribute, :no_matching_birthday, options) if number.birthday != birthday
17
+ end
18
+ rescue ArgumentError => e
19
+ record.errors.add(attribute, e.message, options)
20
+ end
21
+ end
22
+ end
data/lib/rn/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Rn
2
+ VERSION = "0.0.1"
3
+ end
data/rn.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rn/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "rn"
8
+ gem.version = Rn::VERSION
9
+ gem.authors = ["Ralph Rooding"]
10
+ gem.email = ["ralph@izerion.com"]
11
+ gem.description = %q{Rails validations for Belgian Government Registration Numbers (Rijksregisternummer)}
12
+ gem.summary = %q{Rails validations for Belgian Government Registration Numbers (Rijksregisternummer)}
13
+ gem.homepage = "https://github.com/rrooding/rn"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'guard-rspec'
23
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rn::RegistrationNumber do
4
+ subject { described_class.new(number) }
5
+
6
+ let(:number) { '86100500176' }
7
+
8
+ context 'invalid arguments' do
9
+ describe 'nil' do
10
+ it 'raises an argument error' do
11
+ expect { described_class.new(nil) }.to raise_error(ArgumentError, 'cannot be empty')
12
+ end
13
+ end
14
+
15
+ describe 'empty string' do
16
+ it 'raises an argument error' do
17
+ expect { described_class.new('') }.to raise_error(ArgumentError, 'cannot be empty')
18
+ end
19
+ end
20
+
21
+ describe 'with non numeric characters' do
22
+ it 'raises an argument error' do
23
+ expect { described_class.new('1234567aa89') }.to raise_error(ArgumentError, 'can only contain 0-9')
24
+ end
25
+ end
26
+
27
+ describe 'too short' do
28
+ it 'raises an argument error' do
29
+ expect { described_class.new('1234567890') }.to raise_error(ArgumentError, 'must be 11 characters')
30
+ end
31
+ end
32
+
33
+ describe 'too long' do
34
+ it 'raises an argument error' do
35
+ expect { described_class.new('123456789012') }.to raise_error(ArgumentError, 'must be 11 characters')
36
+ end
37
+ end
38
+ end
39
+
40
+ describe '#birthday' do
41
+ context '< 2000' do
42
+ it 'is extracted' do
43
+ expect(subject.birthday).to eq Date.parse('05-10-1986')
44
+ end
45
+ end
46
+
47
+ context '= 2000' do
48
+ let(:number) { '00100500145' }
49
+
50
+ it 'is extracted' do
51
+ expect(subject.birthday).to eq Date.parse('05-10-2000')
52
+ end
53
+ end
54
+
55
+ context '> 2000' do
56
+ let(:number) { '10100500161' }
57
+
58
+ it 'is extracted' do
59
+ expect(subject.birthday).to eq Date.parse('05-10-2010')
60
+ end
61
+ end
62
+ end
63
+
64
+ describe '#valid?' do
65
+ context '< 2000' do
66
+ context 'valid registration number' do
67
+ it 'returns true' do
68
+ expect(subject).to be_valid
69
+ end
70
+ end
71
+
72
+ context 'invalid registration number' do
73
+ let(:number) { '86100500177' } # control + 1
74
+
75
+ it 'returns false' do
76
+ expect(subject).to_not be_valid
77
+ end
78
+ end
79
+ end
80
+
81
+ context '= 2000' do
82
+ context 'valid registration number' do
83
+ let(:number) { '00100500145' }
84
+
85
+ it 'returns true' do
86
+ expect(subject).to be_valid
87
+ end
88
+ end
89
+
90
+ context 'invalid registration number' do
91
+ let(:number) { '00100500146' } # control + 1
92
+
93
+ it 'returns false' do
94
+ expect(subject).to_not be_valid
95
+ end
96
+ end
97
+ end
98
+
99
+ context '> 2000' do
100
+ context 'valid registration number' do
101
+ let(:number) { '10100500161' }
102
+
103
+ it 'returns true' do
104
+ expect(subject).to be_valid
105
+ end
106
+ end
107
+
108
+ context 'invalid registration number' do
109
+ let(:number) { '10100500162' } # control + 1
110
+
111
+ it 'returns false' do
112
+ expect(subject).to_not be_valid
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ class RnValidatorBase
4
+ include ActiveModel::Validations
5
+ attr_accessor :number
6
+ end
7
+
8
+ class RnValidatorTest < RnValidatorBase
9
+ validates :number, :rn => true
10
+ end
11
+
12
+ class RnValidatorBirthday < RnValidatorBase
13
+ attr_accessor :birthday
14
+ validates :number, :rn => { :birthday => true }
15
+ end
16
+
17
+ describe RnValidator do
18
+ subject { RnValidatorTest.new }
19
+
20
+ context 'valid number' do
21
+ before do
22
+ subject.number = '86100500176'
23
+ end
24
+
25
+ it 'is valid' do
26
+ expect(subject).to be_valid
27
+ end
28
+ end
29
+
30
+ context 'invalid control' do
31
+ before do
32
+ subject.number = '86100500177' # control + 1
33
+
34
+ subject.valid?
35
+ end
36
+
37
+ it 'is invalid' do
38
+ expect(subject).to_not be_valid
39
+ end
40
+
41
+ it 'adds an error on :number' do
42
+ expect(subject.errors[:number]).to be_present
43
+ end
44
+ end
45
+
46
+ context 'invalid number' do
47
+ before do
48
+ subject.number = '1234'
49
+
50
+ subject.valid?
51
+ end
52
+
53
+ it 'is invalid' do
54
+ expect(subject).to_not be_valid
55
+ end
56
+
57
+ it 'adds an error on :number' do
58
+ expect(subject.errors[:number]).to be_present
59
+ end
60
+ end
61
+
62
+ context 'blank number' do
63
+ before do
64
+ subject.number = ''
65
+
66
+ subject.valid?
67
+ end
68
+
69
+ it 'is invalid' do
70
+ expect(subject).to_not be_valid
71
+ end
72
+
73
+ it 'adds an error on :number' do
74
+ expect(subject.errors[:number]).to be_present
75
+ end
76
+ end
77
+
78
+ context 'nil number' do
79
+ before do
80
+ subject.number = nil
81
+
82
+ subject.valid?
83
+ end
84
+
85
+ it 'is invalid' do
86
+ expect(subject).to_not be_valid
87
+ end
88
+
89
+ it 'adds an error on :number' do
90
+ expect(subject.errors[:number]).to be_present
91
+ end
92
+ end
93
+
94
+ context 'with birthday check' do
95
+ subject { RnValidatorBirthday.new }
96
+
97
+ before do
98
+ subject.number = '86100500176'
99
+ end
100
+
101
+ context 'with matching birthday' do
102
+ before do
103
+ subject.birthday = Date.parse('05-10-1986')
104
+ end
105
+
106
+ it 'is valid' do
107
+ expect(subject).to be_valid
108
+ end
109
+ end
110
+
111
+ context 'with different birthday' do
112
+ before do
113
+ subject.birthday = Date.parse('06-10-1986')
114
+
115
+ subject.valid?
116
+ end
117
+
118
+ it 'is invalid' do
119
+ expect(subject).to_not be_valid
120
+ end
121
+
122
+ it 'adds an error on :number' do
123
+ expect(subject.errors[:number]).to be_present
124
+ end
125
+ end
126
+
127
+ context 'with empty birthday' do
128
+ before do
129
+ subject.birthday = nil
130
+
131
+ subject.valid?
132
+ end
133
+
134
+ it 'is invalid' do
135
+ expect(subject).to_not be_valid
136
+ end
137
+
138
+ it 'adds an error on :number' do
139
+ expect(subject.errors[:number]).to be_present
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+ require 'rn'
7
+
8
+ RSpec.configure do |config|
9
+ config.before :each do
10
+ end
11
+
12
+ config.after :each do
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ralph Rooding
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: guard-rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Rails validations for Belgian Government Registration Numbers (Rijksregisternummer)
63
+ email:
64
+ - ralph@izerion.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rvmrc
71
+ - .travis.yml
72
+ - Gemfile
73
+ - Guardfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - gemfiles/3.2.gemfile
78
+ - gemfiles/4.0.gemfile
79
+ - lib/rn.rb
80
+ - lib/rn/registration_number.rb
81
+ - lib/rn/validator.rb
82
+ - lib/rn/version.rb
83
+ - rn.gemspec
84
+ - spec/rn/registration_number_spec.rb
85
+ - spec/rn_validator_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/rrooding/rn
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.25
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Rails validations for Belgian Government Registration Numbers (Rijksregisternummer)
111
+ test_files:
112
+ - spec/rn/registration_number_spec.rb
113
+ - spec/rn_validator_spec.rb
114
+ - spec/spec_helper.rb