pasep-pis-nit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49607d004efbcee250604f8cb29ca0a6a3a58d10
4
+ data.tar.gz: b6b8e6fec87043f9aacae6a812ed1fd4c168ce39
5
+ SHA512:
6
+ metadata.gz: 1b874894807b06d4737e9bcfa4ecbb8d8cffd900f285f890ebe829048f0db09b36047b015a16fb1f2152b41b6cb29042266cb9dedc29f5a610f455f80ee73ed5
7
+ data.tar.gz: 504e7acb1a323c29a78179dceb27785b12f6022d5c85de9aa0e4a041c6181f60304b01e9423072515285be2e5e5257b7944c75f680672a1af31fe653fe2aa297
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paseppisnit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jefferson Queiroz Venerando
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,35 @@
1
+ # pasep-pis-nit
2
+
3
+ Allows ActiveModel backed classes to validate PASEP/PIS/NIT fields.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pasep-pis-nit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pasep-pis-nit
20
+
21
+ ## Usage
22
+
23
+ Add to your model:
24
+
25
+ ```ruby
26
+ validates :my_pasep_pis_nit_attribute, :pasep_pis_nit => true
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/shamanime/pasep-pis-nit/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ require 'pasep-pis-nit/version'
2
+ require 'pasep-pis-nit/pis'
3
+ require 'pasep-pis-nit/validator'
@@ -0,0 +1,57 @@
1
+ module PasepPisNit
2
+ class Pis
3
+ # http://www.macoratti.net/alg_pis.htm
4
+ attr_reader :numero
5
+
6
+ def initialize(numero)
7
+ @numero = numero
8
+ @match = @numero =~ PASEP_PIS_NIT_REGEX
9
+ @numero_puro = $1
10
+ @para_verificacao = $2
11
+ @numero = (@match ? format_number! : nil)
12
+ end
13
+
14
+ def to_s
15
+ @numero || ''
16
+ end
17
+
18
+ def ==(outro_doc)
19
+ self.numero == outro_doc.numero
20
+ end
21
+
22
+ # Verifica se o número possui o formato correto e se
23
+ # constitui um número de documento válido
24
+ def valido?
25
+ return false unless @match
26
+ verifica_numero
27
+ end
28
+
29
+ private
30
+ PASEP_PIS_NIT_REGEX = /\A(\d{3}\.?\d{4}\.?\d{3})-?(\d)\Z/ # 125.6483.635-8
31
+ PESO = %w(3 2 9 8 7 6 5 4 3 2)
32
+
33
+ def verifica_numero
34
+ @numero_puro.gsub!(/[\.\/-]/, '')
35
+ return false if @numero_puro.length != 10
36
+
37
+ total = soma_digitos
38
+
39
+ resto = total % 11
40
+
41
+ verificador = 11 - resto
42
+ verificador = 0 if (verificador == 10 || verificador == 11)
43
+ verificador.to_s == @para_verificacao
44
+ end
45
+
46
+ def soma_digitos
47
+ soma = 0
48
+ PESO.each_with_index { |p, idx| soma += p.to_i * @numero_puro[idx].to_i }
49
+ soma
50
+ end
51
+
52
+ def format_number!
53
+ @numero =~ /^(\d{3})\.?(\d{4})\.?(\d{3})-?(\d)$/
54
+ @numero = "#{$1}.#{$2}.#{$3}-#{$4}"
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,6 @@
1
+ class PasepPisNitValidator < ::ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ return if value.nil?
4
+ record.errors.add(attribute, options[:message] || :invalid) unless PasepPisNit::Pis.new(value).valido?
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module PasepPisNit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pasep-pis-nit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pasep-pis-nit'
8
+ spec.version = PasepPisNit::VERSION
9
+ spec.authors = ['Jefferson Queiroz Venerando']
10
+ spec.email = ['jefferson@shamani.me']
11
+ spec.summary = %q{Validação de PASEP/PIS/NIT para sua aplicação.}
12
+ spec.description = %q{Validação de PASEP/PIS/NIT para sua aplicação.}
13
+ spec.homepage = 'https://github.com/shamanime/pasep-pis-nit'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency "activerecord", "~> 4.0"
22
+ spec.add_dependency "activemodel", "~> 4.0"
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '>= 0'
27
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ class TestPerson < TestModel
4
+ validates :pasep_pis_nit, :pasep_pis_nit => true
5
+ end
6
+
7
+ class TestPersonAllowsNil < TestModel
8
+ validates :pasep_pis_nit, :pasep_pis_nit => {:allow_blank => true}
9
+ end
10
+
11
+ class TestPersonAllowsNilFalse < TestModel
12
+ validates :pasep_pis_nit, :pasep_pis_nit => {:allow_blank => false}
13
+ end
14
+
15
+ class TestPersonWithMessage < TestModel
16
+ validates :pasep_pis_nit_address, :pasep_pis_nit => {:message => 'is not looking very good!'}
17
+ end
18
+
19
+ describe PasepPisNitValidator do
20
+
21
+ describe "validation" do
22
+ context "given the valid PASEP/PIS/NIT" do
23
+ [
24
+ '125.5256.164-2',
25
+ '125.1765.831-7',
26
+ '125.4779.406-5',
27
+ '125.6132.272-8',
28
+ '125.4657.521-1',
29
+ '125.7768.118-8',
30
+ '125.1632.712-0',
31
+ '125.2545.275-7',
32
+ '125.4607.879-0',
33
+ '125.6395.441-1',
34
+ '125.0151.564-3',
35
+ '125.8227.898-1',
36
+ '12561488324',
37
+ '12588968467',
38
+ '12547781800'
39
+ ].each do |pasep_pis_nit|
40
+
41
+ it "#{pasep_pis_nit.inspect} should be valid" do
42
+ expect(TestPerson.new(:pasep_pis_nit => pasep_pis_nit).valid?).to be_truthy
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ context "given the invalid PASEP/PIS/NIT" do
50
+ [
51
+ "",
52
+ "f@s",
53
+ "f@s.c",
54
+ "@bar",
55
+ "test@",
56
+ "! \#$%\`|@invalid-characters-in-local.org",
57
+ "<>@[]\`|@125.5256.164-2",
58
+ "125.5256.164-2\n<script>alert('hello')</script>",
59
+ "127 2226 164 2",
60
+ '129.9996.164-2',
61
+ '125.3365.831-7',
62
+ '125.4779.406-0',
63
+ '125.6162.272-8',
64
+ '235.4757.521-1',
65
+ '1245.628.118-8',
66
+ '525.5332.712-6',
67
+ '267.4545.267-7',
68
+ '275.4607.879-0',
69
+ '235.6395.443-1',
70
+ '225.0151.564-3',
71
+ '12a.8227.898-1',
72
+ '15.6148.8342-4',
73
+ '18896.8416-7',
74
+ '125477818300'
75
+ ].each do |pasep_pis_nit|
76
+
77
+ it "#{pasep_pis_nit.inspect} should not be valid" do
78
+ expect(TestPerson.new(:pasep_pis_nit => pasep_pis_nit).valid?).to be_falsey
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "error messages" do
86
+ context "when the message is not defined" do
87
+ subject { TestPerson.new :pasep_pis_nit => '15.6148.8342-4' }
88
+ before { subject.valid? }
89
+
90
+ it "should add the default message" do
91
+ expect(subject.errors[:pasep_pis_nit]).to include "is invalid"
92
+ end
93
+ end
94
+
95
+ context "when the message is defined" do
96
+ subject { TestPersonWithMessage.new :pasep_pis_nit_address => '15.6148.8342-4' }
97
+ before { subject.valid? }
98
+
99
+ it "should add the customized message" do
100
+ expect(subject.errors[:pasep_pis_nit_address]).to include "is not looking very good!"
101
+ end
102
+ end
103
+ end
104
+
105
+ describe "blank PASEP/PIS/NIT" do
106
+ it "should not be valid when :allow_blank option is missing" do
107
+ person = TestPerson.new(:pasep_pis_nit => '')
108
+ expect(person.valid?).to be_falsey
109
+ end
110
+
111
+ it "should be valid when :allow_blank options is set to true" do
112
+ person = TestPersonAllowsNil.new(:pasep_pis_nit => '')
113
+ expect(person.valid?).to be_truthy
114
+ end
115
+
116
+ it "should not be valid when :allow_blank option is set to false" do
117
+ person = TestPersonAllowsNilFalse.new(:pasep_pis_nit => '')
118
+ expect(person.valid?).to_not be_truthy
119
+ end
120
+ end
121
+ 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 'pasep-pis-nit'
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,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pasep-pis-nit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jefferson Queiroz Venerando
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Validação de PASEP/PIS/NIT para sua aplicação.
84
+ email:
85
+ - jefferson@shamani.me
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/pasep-pis-nit.rb
96
+ - lib/pasep-pis-nit/pis.rb
97
+ - lib/pasep-pis-nit/validator.rb
98
+ - lib/pasep-pis-nit/version.rb
99
+ - pasep-pis-nit.gemspec
100
+ - spec/pasep_pis_nit_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: https://github.com/shamanime/pasep-pis-nit
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.2
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Validação de PASEP/PIS/NIT para sua aplicação.
126
+ test_files:
127
+ - spec/pasep_pis_nit_spec.rb
128
+ - spec/spec_helper.rb