pis_pasep 1.0.0

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/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Rodrigo Manhães
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.
data/README.rst ADDED
@@ -0,0 +1,12 @@
1
+ pis_pasep
2
+ =========
3
+
4
+ .. image:: http://travis-ci.org/rodrigomanhaes/pis_pasep.png
5
+ :target: http://travis-ci.org/rodrigomanhaes/pis_pasep
6
+
7
+ .. image:: https://gemnasium.com/rodrigomanhaes/flexible_date.png
8
+ :target: https://gemnasium.com/rodrigomanhaes/flexible_date
9
+
10
+ Permite incluir campos PIS/PASEP em modelos do ActiveRecord. Veja casos de utilização nas specs.
11
+
12
+ Completamente inspirado (e algumas partes de código extraídas literalmente) do brcpfcnpj do Brazilian-Rails.
data/lib/pis_pasep.rb ADDED
@@ -0,0 +1,9 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ %w(pis_pasep pis_pasep_active_record).each {|req| require File.dirname(__FILE__) + "/pis_pasep/#{req}"}
5
+
6
+ %w(rubygems active_record active_support).each {|req| require req }
7
+
8
+ module PisPasep
9
+ end
@@ -0,0 +1,55 @@
1
+ module PisPasep
2
+ class Base
3
+ attr_reader :number
4
+
5
+ def initialize(number)
6
+ @number = number
7
+ @number = @number =~ PIS_PASEP_REGEX ? format_number! : @number
8
+ end
9
+
10
+ def valid?
11
+ return true if @number.nil?
12
+ return false unless format_valid?
13
+ number_valid?
14
+ end
15
+
16
+ def to_s
17
+ number
18
+ end
19
+
20
+ def ==(outro)
21
+ self.number == outro.number
22
+ end
23
+
24
+ private
25
+
26
+ DIGITS = 11
27
+ PIS_PASEP_REGEX = /^(\d{3})\.?(\d{5})\.?(\d{2})\-?(\d{1})$/
28
+
29
+ def format_valid?
30
+ return false unless @number =~ PIS_PASEP_REGEX
31
+ @number.gsub(/[^0-9]*/, "").size == DIGITS
32
+ end
33
+
34
+ def number_valid?
35
+ num = @number.gsub(/[^0-9]*/, "")
36
+ dv = num[-1].to_i
37
+ sum = 0
38
+ coefficient = 2
39
+ (num.size - 2).downto(0) do |i|
40
+ digit = num[i].to_i
41
+ sum += digit * coefficient
42
+ coefficient += 1
43
+ coefficient = 2 if coefficient > 9
44
+ end
45
+ found_checksum = 11 - sum % 11
46
+ found_checksum = 0 if found_checksum >= 10
47
+ found_checksum == dv
48
+ end
49
+
50
+ def format_number!
51
+ @number =~ PIS_PASEP_REGEX
52
+ @number = "#{$1}.#{$2}.#{$3}-#{$4}"
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_record'
4
+
5
+ module PisPasep
6
+ module ActiveRecord
7
+ def use_as_pis_pasep(*args)
8
+ args.each do |attribute|
9
+ add_composed_class(attribute, 'PisPasep::Base')
10
+ module_eval create_code(attribute, 'PisPasep::Base')
11
+ end
12
+ end
13
+
14
+ def add_composed_class(name, klass)
15
+ options = {:class_name => klass, :mapping => [name.to_s, 'number'], :allow_nil => true }
16
+ constructor = Proc.new {|number| eval(klass).new(number) }
17
+ converter = Proc.new {|value| eval(klass).new(value) }
18
+ begin
19
+ composed_of name, options.merge({:constructor => constructor, :converter => converter })
20
+ rescue Exception
21
+ composed_of name, options { eval(klass).new(name[:number]) }
22
+ end
23
+ end
24
+
25
+ def create_code(attribute, klass)
26
+ """
27
+ validate :#{attribute}_valid?
28
+
29
+ def #{attribute}_valid?
30
+ value = read_attribute('#{attribute}')
31
+ if !value.nil? && value.strip != '' && !#{attribute}.nil? && !#{attribute}.valid?
32
+ self.errors.add('#{attribute}', 'número inválido')
33
+ end
34
+ end
35
+
36
+ def #{attribute}=(value)
37
+ if value.blank?
38
+ write_attribute('#{attribute}', nil)
39
+ elsif value.kind_of?(#{eval(klass)})
40
+ write_attribute('#{attribute}', value.number)
41
+ else
42
+ begin
43
+ c = #{eval(klass)}.new(value)
44
+ c.valid? ? write_attribute('#{attribute}', c.number) : write_attribute('#{attribute}', value)
45
+ rescue
46
+ @#{attribute} = value
47
+ end
48
+ end
49
+ end
50
+ """
51
+ end
52
+ end
53
+ end
54
+
55
+ ActiveRecord::Base.extend PisPasep::ActiveRecord
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pis_pasep
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rodrigo Manhães
9
+ autorequire: pis_pasep
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.11.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.11.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.5
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.5
62
+ description: PIS/PASEP validations for pure Ruby and ActiveRecord apps
63
+ email:
64
+ - rmanhaes@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/pis_pasep/pis_pasep_active_record.rb
70
+ - lib/pis_pasep/pis_pasep.rb
71
+ - lib/pis_pasep.rb
72
+ - README.rst
73
+ - LICENSE.txt
74
+ homepage: https://github.com/rodrigomanhaes/pis_pasep
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project: pis_pasep
94
+ rubygems_version: 1.8.24
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: PIS/PASEP validations for pure Ruby and ActiveRecord apps
98
+ test_files: []