orgno_validator 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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in orgno_validator.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rspec/core/rake_task'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ RSpec::Core::RakeTask.new do |t|
9
+ t.rspec_opts = %w(--format documentation --colour)
10
+ end
11
+
12
+ Rake::RDocTask.new do |rdoc|
13
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
14
+
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = "orgno_validator #{version}"
17
+ rdoc.rdoc_files.include('README*')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end
20
+
21
+ task :default => 'spec'
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+
3
+ class OrgnoValidator < ActiveModel::EachValidator
4
+
5
+ def validate_each(record, attribute, value)
6
+ unless OrgnoValidator.valid_orgno?(value)
7
+ record.errors.add(attribute, options[:message] || :invalid)
8
+ end
9
+ end
10
+
11
+ private
12
+
13
+ BASE_MOD_11_WEIGHTS = [2, 3, 4, 5, 6, 7]
14
+ ORGNO_LENGTH = 9
15
+
16
+ class << self
17
+ attr_accessor :orgno_mod_11_weights
18
+ end
19
+
20
+ def self.mod_11_weights_generator(length)
21
+ weights = []
22
+ (0..length-2).each do |index|
23
+ base_index = index % BASE_MOD_11_WEIGHTS.length
24
+ weights << BASE_MOD_11_WEIGHTS[base_index]
25
+ end
26
+ weights.reverse!
27
+ end
28
+
29
+ @orgno_mod_11_weights = OrgnoValidator.mod_11_weights_generator(ORGNO_LENGTH)
30
+
31
+ def self.valid_orgno?(orgno)
32
+ return false if !orgno || orgno.to_s.length != ORGNO_LENGTH
33
+
34
+ orgno_digits = orgno.to_s.each_char.map { |d| d.to_i }
35
+ sum = 0
36
+
37
+ (0..orgno_digits.length-2).each do |i|
38
+ sum += orgno_digits[i].to_i * @orgno_mod_11_weights[i]
39
+ end
40
+
41
+ remainder = sum % 11
42
+ control_digit = remainder == 0 ? 0 : 11 - remainder
43
+
44
+ return false if control_digit == 10
45
+ return control_digit == orgno_digits.last
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["André Bonkowski"]
5
+ gem.email = ["andre@kodemaker.no"]
6
+ gem.description = %q{Validate Norwegian "organisasjonsnummer"}
7
+ gem.summary = %q{Simple validator that validate Norwegian "organisasjonsnummer}
8
+ gem.homepage = ""
9
+
10
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
11
+ gem.files = `git ls-files`.split("\n")
12
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
13
+ gem.name = "orgno_validator"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = '0.0.1'
16
+ gem.add_dependency("activemodel", ">= 0")
17
+ gem.add_development_dependency("rspec", ">= 0")
18
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ class TestCompany < TestModel
4
+ validates :orgno, :orgno => true
5
+ end
6
+
7
+ class TestCompanyAllowsNil < TestModel
8
+ validates :orgno, :orgno => {:allow_nil => true}
9
+ end
10
+
11
+ class TestCompanyAllowsNilFalse < TestModel
12
+ validates :orgno, :orgno => {:allow_nil => false}
13
+ end
14
+
15
+ class TestCompanyWithMessage < TestModel
16
+ validates :organisasjonsnummer, :orgno => {:message => 'is not looking very good!'}
17
+ end
18
+
19
+ describe OrgnoValidator do
20
+
21
+ describe "validation" do
22
+ context "given the valid orgno" do
23
+ [ "810243562", "948776901", "957929621"].each do |orgno|
24
+ it "#{orgno.inspect} should be valid" do
25
+ TestCompany.new(:orgno => orgno).should be_valid
26
+ end
27
+ end
28
+ end
29
+
30
+ context "given the invalid orgno" do
31
+ [ "810243561", "948776900", "957929620"].each do |orgno|
32
+ it "#{orgno.inspect} should not be valid" do
33
+ TestCompany.new(:orgno => orgno).should_not be_valid
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "error messages" do
40
+ context "when the message is not defined" do
41
+ subject { TestCompany.new :orgno => '948776900' }
42
+ before { subject.valid? }
43
+
44
+ it "should add the default message" do
45
+ subject.errors[:orgno].should include "is invalid"
46
+ end
47
+ end
48
+
49
+ context "when the message is defined" do
50
+ subject { TestCompanyWithMessage.new :organisasjonsnummer => '948776900' }
51
+ before { subject.valid? }
52
+
53
+ it "should add the customized message" do
54
+ subject.errors[:organisasjonsnummer].should include "is not looking very good!"
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "nil email" do
60
+ it "should not be valid when :allow_nil option is missing" do
61
+ TestCompany.new(:orgno => nil).should_not be_valid
62
+ end
63
+
64
+ it "should be valid when :allow_nil options is set to true" do
65
+ TestCompanyAllowsNil.new(:orgno => nil).should be_valid
66
+ end
67
+
68
+ it "should not be valid when :allow_nil option is set to false" do
69
+ TestCompanyAllowsNilFalse.new(:orgno => nil).should_not be_valid
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'active_model'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'orgno_validator'
9
+
10
+ class TestModel
11
+ include ActiveModel::Validations
12
+
13
+ def initialize(attributes = {})
14
+ @attributes = attributes
15
+ end
16
+
17
+ def read_attribute_for_validation(key)
18
+ @attributes[key]
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orgno_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - André Bonkowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-11-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &70364189810140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70364189810140
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70364189808940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70364189808940
36
+ description: Validate Norwegian "organisasjonsnummer"
37
+ email:
38
+ - andre@kodemaker.no
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Rakefile
46
+ - lib/orgno_validator.rb
47
+ - orgno_validator.gemspec
48
+ - spec/orgno_validator_spec.rb
49
+ - spec/spec_helper.rb
50
+ homepage: ''
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project:
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Simple validator that validate Norwegian "organisasjonsnummer
74
+ test_files:
75
+ - spec/orgno_validator_spec.rb
76
+ - spec/spec_helper.rb