pis_pasep_validator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +21 -0
- data/README.markdown +17 -0
- data/Rakefile +11 -0
- data/lib/pis_pasep_validator.rb +7 -0
- data/lib/pis_pasep_validator/pis_pasep.rb +54 -0
- data/pis_pasep_validator.gemspec +19 -0
- data/test/pis_pasep_validator_test.rb +47 -0
- data/test/test_helper.rb +10 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2012 nohup brasil
|
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.
|
21
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# pis_pasep_validator
|
2
|
+
|
3
|
+
PIS/PASEP validation for ActiveModel
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem "pis_pasep_validator"
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
class Person < ActiveRecord::Base
|
12
|
+
validates :pis, :pis_pasep => true
|
13
|
+
end
|
14
|
+
|
15
|
+
## License
|
16
|
+
|
17
|
+
Copyright (c) 2012 nohup brasil, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
class PisPasepValidator::PisPasep
|
2
|
+
def self.valid?(number)
|
3
|
+
new(number).valid?
|
4
|
+
end
|
5
|
+
|
6
|
+
attr_reader :number
|
7
|
+
|
8
|
+
def initialize(number)
|
9
|
+
@number = number.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def digits
|
13
|
+
@digits ||= number.scan(/\d/).map(&:to_i)
|
14
|
+
end
|
15
|
+
|
16
|
+
def plain
|
17
|
+
@plain ||= digits.join
|
18
|
+
end
|
19
|
+
|
20
|
+
def digit_verificator
|
21
|
+
digits[-1]
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?
|
25
|
+
formatted? && !black_listed? && digits_matches?
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def formatted?
|
31
|
+
plain =~ /^\d{11}$/ || number =~ /^\d{3}\.\d{5}\.\d{2}-\d{1}$/
|
32
|
+
end
|
33
|
+
|
34
|
+
def black_listed?
|
35
|
+
plain =~ /^12345678909|(\d)\1{10}$/
|
36
|
+
end
|
37
|
+
|
38
|
+
def digits_matches?
|
39
|
+
sum = 0
|
40
|
+
coefficient = 2
|
41
|
+
|
42
|
+
(digits.size - 2).downto(0).each do |i|
|
43
|
+
sum += digits[i] * coefficient
|
44
|
+
coefficient += 1
|
45
|
+
coefficient = 2 if coefficient > 9
|
46
|
+
end
|
47
|
+
|
48
|
+
result = sum % 11
|
49
|
+
result = 11 - result
|
50
|
+
|
51
|
+
result = 0 if result >= 10
|
52
|
+
result == digit_verificator
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "pis_pasep_validator"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Ricardo H."]
|
8
|
+
s.email = ["ricardohsd@gmail.com"]
|
9
|
+
s.homepage = ""
|
10
|
+
s.summary = %q{PIS/PASEP validator for ActiveModel}
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_dependency 'activemodel', '>= 3.0'
|
18
|
+
s.add_development_dependency 'rake', '>= 0.8.7'
|
19
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PisPasepValidatorTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_black_list
|
6
|
+
%w(11111111111 22222222222 77777777777).each do |number|
|
7
|
+
assert_invalid number
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_invalid
|
12
|
+
assert_invalid '099075865'
|
13
|
+
assert_invalid '123.4567.891-3'
|
14
|
+
assert_invalid '09907586561'
|
15
|
+
assert_invalid '234.624.576-57'
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_blank_values
|
19
|
+
assert_invalid ''
|
20
|
+
assert_invalid false
|
21
|
+
assert_invalid nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_masked
|
25
|
+
assert_valid '107.63096.72-2'
|
26
|
+
assert_valid '123.45678.91-9'
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_unmasked
|
30
|
+
assert_valid '12345678919'
|
31
|
+
assert_valid '10763096722'
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def assert_invalid(identification)
|
37
|
+
assert person(:identification => identification).invalid?
|
38
|
+
end
|
39
|
+
|
40
|
+
def assert_valid(identification)
|
41
|
+
assert person(:identification => identification).valid?
|
42
|
+
end
|
43
|
+
|
44
|
+
def person(attributes = {})
|
45
|
+
Person.new(attributes)
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pis_pasep_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ricardo H.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: &2152154120 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152154120
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &2152153360 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.8.7
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2152153360
|
36
|
+
description:
|
37
|
+
email:
|
38
|
+
- ricardohsd@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- MIT-LICENSE
|
46
|
+
- README.markdown
|
47
|
+
- Rakefile
|
48
|
+
- lib/pis_pasep_validator.rb
|
49
|
+
- lib/pis_pasep_validator/pis_pasep.rb
|
50
|
+
- pis_pasep_validator.gemspec
|
51
|
+
- test/pis_pasep_validator_test.rb
|
52
|
+
- test/test_helper.rb
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.15
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: PIS/PASEP validator for ActiveModel
|
77
|
+
test_files:
|
78
|
+
- test/pis_pasep_validator_test.rb
|
79
|
+
- test/test_helper.rb
|