pis_pasep_validator 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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pis_pasep_validator.gemspec
4
+ gemspec
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,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ end
@@ -0,0 +1,7 @@
1
+ class PisPasepValidator < ActiveModel::EachValidator
2
+ autoload :PisPasep, 'pis_pasep_validator/pis_pasep'
3
+
4
+ def validate_each(record, attribute, value)
5
+ record.errors.add(attribute, options[:message]) unless PisPasep.valid?(value)
6
+ end
7
+ end
@@ -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
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_model'
4
+ require 'pis_pasep_validator'
5
+ require 'ostruct'
6
+
7
+ class Person < OpenStruct
8
+ include ActiveModel::Validations
9
+ validates :identification, :pis_pasep => true
10
+ end
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