valideez 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +1 -1
- data/lib/valideez/base.rb +4 -1
- data/lib/valideez/common.rb +3 -1
- data/lib/valideez/iban.rb +36 -0
- data/lib/valideez/regon.rb +39 -0
- data/lib/valideez/regon_validator.rb +18 -0
- data/lib/valideez/siret.rb +36 -0
- data/lib/valideez/siret_validator.rb +18 -0
- data/lib/valideez/version.rb +1 -1
- data/spec/iban_spec.rb +15 -0
- data/spec/regon_spec.rb +27 -0
- data/spec/regon_validator_spec.rb +40 -0
- data/spec/siret_spec.rb +9 -0
- data/spec/siret_validator_spec.rb +28 -0
- data/spec/spec_helper.rb +4 -0
- metadata +26 -11
data/LICENSE.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2011 Zaiste!
|
1
|
+
Copyright (c) 2011 Zaiste!
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
4
|
|
data/lib/valideez/base.rb
CHANGED
@@ -4,17 +4,20 @@ module Valideez
|
|
4
4
|
attr_accessor :validable
|
5
5
|
|
6
6
|
def initialize(val)
|
7
|
-
@val = val.to_s.gsub(/-/, '')
|
7
|
+
@val = val.to_s.gsub(/-/, '').gsub(/\s+/, '')
|
8
8
|
@validable = []
|
9
9
|
end
|
10
10
|
|
11
11
|
def valid?
|
12
|
+
# p - Array of methods (Strings) which can be used for validation
|
12
13
|
p = validable.select{ |var| respond_to?("validate_#{var.to_s}", true) }
|
14
|
+
# execute validations, stop and return false if current validations return false
|
13
15
|
p.take_while { |var| send("validate_#{var.to_s}") }.size == p.size
|
14
16
|
end
|
15
17
|
|
16
18
|
private
|
17
19
|
|
20
|
+
# Dynamically assign instance variables using opts Hash
|
18
21
|
def assign(opts)
|
19
22
|
opts.each do |attr, val|
|
20
23
|
# tricky: don't create an instance variable if
|
data/lib/valideez/common.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Iban < Valideez::Base
|
3
|
+
include Valideez::Common
|
4
|
+
|
5
|
+
def initialize(val, options = {})
|
6
|
+
super val
|
7
|
+
|
8
|
+
assign({
|
9
|
+
format: /\A[A-Z][A-Z]\d{26}\Z/,
|
10
|
+
length: 28,
|
11
|
+
modulo: 97,
|
12
|
+
sum_control: true,
|
13
|
+
}.merge(options))
|
14
|
+
|
15
|
+
@arr = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate_sum_control
|
19
|
+
mod = checksum % modulo
|
20
|
+
mod === 1
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def checksum
|
26
|
+
@arr = val[4..28] + char_number(val[0..1]) + val[2..3]
|
27
|
+
@arr.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
def char_number(chars)
|
31
|
+
chars.split("").collect { |e| e.upcase.ord - 55 }.join
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Regon < Valideez::Base
|
3
|
+
include Valideez::Common
|
4
|
+
|
5
|
+
def initialize(val, options = {})
|
6
|
+
super val
|
7
|
+
|
8
|
+
assign({
|
9
|
+
:format => /\A(\d{9}|\d{14})\Z/,
|
10
|
+
:length => [9, 14],
|
11
|
+
:mask => [8, 9, 2, 3, 4, 5, 6, 7],
|
12
|
+
:mask_long => [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8],
|
13
|
+
:modulo => 11,
|
14
|
+
:sum_control => true,
|
15
|
+
}.merge(options))
|
16
|
+
|
17
|
+
@arr = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_long?
|
21
|
+
val.size === 14
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_sum_control
|
25
|
+
mod = checksum % modulo
|
26
|
+
mod === @arr.shift
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def checksum
|
32
|
+
@arr = val.split("").collect(&:to_i)
|
33
|
+
# there is the other mask for 14 digit regon
|
34
|
+
m = is_long? ? mask_long : mask
|
35
|
+
m.inject(0) {|sum, w| sum + w * @arr.shift}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class RegonValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
@message = options[:message] || "invalid format"
|
6
|
+
record.errors[attribute] << @message unless Valideez::Regon.new(value).valid?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveModel
|
11
|
+
module Validations
|
12
|
+
module HelperMethods
|
13
|
+
def validates_regon_of(*attr_names)
|
14
|
+
validates_with RegonValidator, _merge_attributes(attr_names)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Valideez
|
2
|
+
class Siret < Valideez::Base
|
3
|
+
include Valideez::Common
|
4
|
+
|
5
|
+
def initialize(val, options = {})
|
6
|
+
super val
|
7
|
+
|
8
|
+
assign({
|
9
|
+
:format => /\A\d{14}\Z/,
|
10
|
+
:length => 14,
|
11
|
+
:mask => (1..14).to_a.map { |u| u % 2 + 1 },
|
12
|
+
:modulo => 10,
|
13
|
+
:sum_control => true,
|
14
|
+
}.merge(options))
|
15
|
+
|
16
|
+
@arr = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_sum_control
|
20
|
+
mod = checksum % modulo
|
21
|
+
mod === 0
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def checksum
|
27
|
+
@arr = val.split("").collect(&:to_i)
|
28
|
+
mask.inject(0) do |sum, w|
|
29
|
+
val = w * @arr.shift
|
30
|
+
val -= 9 if val >= 10
|
31
|
+
sum + val
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class SiretValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
@message = options[:message] || "invalid format"
|
6
|
+
record.errors[attribute] << @message unless Valideez::Siret.new(value).valid?
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ActiveModel
|
11
|
+
module Validations
|
12
|
+
module HelperMethods
|
13
|
+
def validates_siret_of(*attr_names)
|
14
|
+
validates_with SiretValidator, _merge_attributes(attr_names)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/valideez/version.rb
CHANGED
data/spec/iban_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Valideez::Iban do
|
4
|
+
it "should be valid" do
|
5
|
+
%w[PL89114020040000330247593733].each do |n|
|
6
|
+
Valideez::Iban.new(n).should be_valid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should not be valid" do
|
11
|
+
%w[PL89114020040000330247593734].each do |n|
|
12
|
+
Valideez::Iban.new(n).should_not be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/regon_spec.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Valideez::Regon do
|
4
|
+
it "should be valid" do
|
5
|
+
%w[590096454 123456785 732065814].each do |n|
|
6
|
+
Valideez::Regon.new(n).should be_valid
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be valid 14-digit regon" do
|
11
|
+
%w[12345678512347 23511332857188].each do |n|
|
12
|
+
Valideez::Regon.new(n).should be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be invalid" do
|
17
|
+
%w[590096453 59009645412 390096454 ag5435 00000000 732065813].each do |n|
|
18
|
+
Valideez::Regon.new(n).should_not be_valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be invalid 14-digit regon" do
|
23
|
+
%w[12345678512348 12345678512346 1234567851237 23511332857181].each do |n|
|
24
|
+
Valideez::Regon.new(n).should_not be_valid
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class RegonBastard
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
attr_accessor :regon
|
9
|
+
validates :regon, :presence => true, :regon => true
|
10
|
+
end
|
11
|
+
|
12
|
+
describe RegonValidator do
|
13
|
+
before :each do
|
14
|
+
@model = RegonBastard.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid" do
|
18
|
+
@model.should_not be_valid
|
19
|
+
@model.regon = '590096454'
|
20
|
+
@model.should be_valid
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be invalid" do
|
24
|
+
@model.should_not be_valid
|
25
|
+
@model.regon = '590096453'
|
26
|
+
@model.should_not be_valid
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be valid 14-digit regon" do
|
30
|
+
@model.should_not be_valid
|
31
|
+
@model.regon = '12345678512347'
|
32
|
+
@model.should be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be invalid 14-digit regon" do
|
36
|
+
@model.should_not be_valid
|
37
|
+
@model.regon = '12345678512348'
|
38
|
+
@model.should_not be_valid
|
39
|
+
end
|
40
|
+
end
|
data/spec/siret_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class SiretBastard
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
8
|
+
attr_accessor :siret
|
9
|
+
validates :siret, :presence => true, :siret => true
|
10
|
+
end
|
11
|
+
|
12
|
+
describe SiretValidator do
|
13
|
+
before :each do
|
14
|
+
@model = SiretBastard.new
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be valid" do
|
18
|
+
@model.should_not be_valid
|
19
|
+
@model.siret = '73282932000074'
|
20
|
+
@model.should be_valid
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should be invalid" do
|
24
|
+
@model.should_not be_valid
|
25
|
+
@model.siret = '7862001184'
|
26
|
+
@model.should_not be_valid
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valideez
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-02-09 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &2202048880 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2202048880
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &2202047980 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2202047980
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &2202046920 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 3.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2202046920
|
47
47
|
description: Your favourite validations with Ruby
|
48
48
|
email:
|
49
49
|
- oh@zaiste.net
|
@@ -61,20 +61,30 @@ files:
|
|
61
61
|
- lib/valideez/base.rb
|
62
62
|
- lib/valideez/bastard.rb
|
63
63
|
- lib/valideez/common.rb
|
64
|
+
- lib/valideez/iban.rb
|
64
65
|
- lib/valideez/nip.rb
|
65
66
|
- lib/valideez/nip_validator.rb
|
66
67
|
- lib/valideez/pesel.rb
|
67
68
|
- lib/valideez/pesel_validator.rb
|
68
69
|
- lib/valideez/phone.rb
|
69
70
|
- lib/valideez/phone_validator.rb
|
71
|
+
- lib/valideez/regon.rb
|
72
|
+
- lib/valideez/regon_validator.rb
|
73
|
+
- lib/valideez/siret.rb
|
74
|
+
- lib/valideez/siret_validator.rb
|
70
75
|
- lib/valideez/version.rb
|
71
76
|
- spec/bastard_spec.rb
|
77
|
+
- spec/iban_spec.rb
|
72
78
|
- spec/nip_spec.rb
|
73
79
|
- spec/nip_validator_spec.rb
|
74
80
|
- spec/pesel_spec.rb
|
75
81
|
- spec/pesel_validator_spec.rb
|
76
82
|
- spec/phone_spec.rb
|
77
83
|
- spec/phone_validator_spec.rb
|
84
|
+
- spec/regon_spec.rb
|
85
|
+
- spec/regon_validator_spec.rb
|
86
|
+
- spec/siret_spec.rb
|
87
|
+
- spec/siret_validator_spec.rb
|
78
88
|
- spec/spec_helper.rb
|
79
89
|
- valideez.gemspec
|
80
90
|
homepage: http://dev.zaiste.net/gem/valideez
|
@@ -91,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
101
|
version: '0'
|
92
102
|
segments:
|
93
103
|
- 0
|
94
|
-
hash: -
|
104
|
+
hash: -3669528909321109052
|
95
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
106
|
none: false
|
97
107
|
requirements:
|
@@ -100,19 +110,24 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
110
|
version: '0'
|
101
111
|
segments:
|
102
112
|
- 0
|
103
|
-
hash: -
|
113
|
+
hash: -3669528909321109052
|
104
114
|
requirements: []
|
105
115
|
rubyforge_project: valideez
|
106
|
-
rubygems_version: 1.8.
|
116
|
+
rubygems_version: 1.8.11
|
107
117
|
signing_key:
|
108
118
|
specification_version: 3
|
109
119
|
summary: Let's valideez lousy objects once and forever
|
110
120
|
test_files:
|
111
121
|
- spec/bastard_spec.rb
|
122
|
+
- spec/iban_spec.rb
|
112
123
|
- spec/nip_spec.rb
|
113
124
|
- spec/nip_validator_spec.rb
|
114
125
|
- spec/pesel_spec.rb
|
115
126
|
- spec/pesel_validator_spec.rb
|
116
127
|
- spec/phone_spec.rb
|
117
128
|
- spec/phone_validator_spec.rb
|
129
|
+
- spec/regon_spec.rb
|
130
|
+
- spec/regon_validator_spec.rb
|
131
|
+
- spec/siret_spec.rb
|
132
|
+
- spec/siret_validator_spec.rb
|
118
133
|
- spec/spec_helper.rb
|