br-cpf 1.0.0 → 1.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/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in br-cpf.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
@@ -1,14 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ br-cpf (1.0.1)
5
+ ffi (~> 1.0)
6
+
1
7
  GEM
2
8
  remote: http://rubygems.org/
3
9
  specs:
10
+ ffi (1.0.9)
4
11
  rake (0.8.7)
5
- rake-compiler (0.7.1)
6
- rake (>= 0.8.3, < 0.9)
7
12
  rspec (1.3.0)
8
13
 
9
14
  PLATFORMS
10
15
  ruby
11
16
 
12
17
  DEPENDENCIES
13
- rake-compiler
14
- rspec (~> 1.3.0)
18
+ br-cpf!
19
+ rake
20
+ rspec (~> 1.3)
@@ -0,0 +1,102 @@
1
+ # BR::CPF ![Travis-ci status](https://secure.travis-ci.org/bbcoimbra/br-cpf.png?branch=master)
2
+
3
+ Lib implementada em C (gera uma lib compilada nativamente) que calcula e valida CPFs.
4
+ Recomendada para Validar CPFs ou Gerar CPFs quando o sistema requer que sejam feitas mais de 1000 Validações/Gerações por segundo.
5
+
6
+ ## Instalação
7
+
8
+ sudo gem install br-cpf
9
+
10
+ ## Exemplos
11
+
12
+ ### Usar
13
+
14
+ ```ruby
15
+ require 'rubygems'
16
+ require 'br/cpf'
17
+ ```
18
+
19
+ ### Validar
20
+
21
+ ```ruby
22
+ BR::CPF.valid? '00000000191'
23
+ #> true
24
+
25
+ BR::CPF.valid? 12345678909
26
+ #> true
27
+
28
+ BR::CPF.valid? 12345678900
29
+ #> false
30
+ ```
31
+
32
+ ### Gerar CPF
33
+
34
+ ```ruby
35
+ raiz = 1
36
+ BR::CPF.new(raiz).to_s
37
+ #> '00000000191'
38
+ ```
39
+
40
+ ## Benchmark sobre uma implementação puro Ruby
41
+ Comparação de validação com a brcpfcnpj (Brazilian Rails)
42
+
43
+ ```bash
44
+ $ cat benchmark.rb
45
+ ```
46
+
47
+ ```ruby
48
+ require "rubygems"
49
+ require "br/cpf"
50
+ require "brcpfcnpj"
51
+ require "benchmark"
52
+
53
+ [1, 10, 100, 1000, 10_000, 100_000, 1_000_000].each do |n|
54
+ puts "Teste com n # #{n}"
55
+ Benchmark.bm do |x|
56
+ x.report { n.times { BR::CPF.valid?(rand(999_999_999_99).to_s.rjust(11, '0')) } }
57
+ x.report { n.times { Cpf.new(rand(999_999_999_99).to_s.rjust(11, '0')).valido? } }
58
+ end
59
+ end
60
+ ```
61
+
62
+ Resultados
63
+
64
+ ```
65
+ Teste com n = 1
66
+ user system total real
67
+ 0.000000 0.000000 0.000000 ( 0.000053)
68
+ 0.000000 0.000000 0.000000 ( 0.000243)
69
+ Teste com n = 10
70
+ user system total real
71
+ 0.000000 0.000000 0.000000 ( 0.000080)
72
+ 0.000000 0.000000 0.000000 ( 0.001187)
73
+ Teste com n = 100
74
+ user system total real
75
+ 0.000000 0.000000 0.000000 ( 0.002193)
76
+ 0.020000 0.000000 0.020000 ( 0.011373)
77
+ Teste com n = 1000
78
+ user system total real
79
+ 0.020000 0.000000 0.020000 ( 0.017329)
80
+ 0.150000 0.000000 0.150000 ( 0.169375)
81
+ Teste com n = 10000
82
+ user system total real
83
+ 0.100000 0.000000 0.100000 ( 0.094112)
84
+ 1.490000 0.010000 1.500000 ( 1.512164)
85
+ Teste com n = 100000
86
+ user system total real
87
+ 0.850000 0.000000 0.850000 ( 0.874577)
88
+ 15.050000 0.010000 15.060000 ( 15.193196)
89
+ Teste com n = 1000000
90
+ user system total real
91
+ 8.570000 0.010000 8.580000 ( 8.631888)
92
+ 150.620000 0.210000 150.830000 (152.158936)
93
+ ```
94
+
95
+ ## Desvantagens
96
+ Por precisar compilar a extensão em C, acaba incompatível com sistemas que não tem um compilador em C (Windows).
97
+
98
+ E quando o ritmo de comparação for menor que 1000 por segundo a diferença é pequena, por volta de de 0.15 segundos. Talvez o acumulo de dependência não valha a pena.
99
+
100
+ ## Copyright
101
+
102
+ Copyright (c) 2010 Bruno Coimbra. Veja o arquivo LICENSE para mais detalhes.
data/Rakefile CHANGED
@@ -1,2 +1,17 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'spec/rake/spectask'
5
+ Spec::Rake::SpecTask.new
6
+
7
+ task :default => [:compile_ext, :spec]
8
+
9
+ task :compile_ext do
10
+ require 'fileutils'
11
+ root_dir = pwd
12
+ cd 'ext/CPF'
13
+ system 'ruby extconf.rb'
14
+ system 'make'
15
+ cp "CPF.so", "#{root_dir}/lib/"
16
+ cd root_dir
17
+ end
@@ -2,7 +2,10 @@ require 'ffi'
2
2
  module BR
3
3
  module CPFWrapper
4
4
  extend FFI::Library
5
- ffi_lib File.join(File.dirname(__FILE__), "..", "..", "CPF.so")
5
+ path = File.join(File.dirname(__FILE__), "..", "..")
6
+ lib = File.join(path, 'CPF.so')
7
+ lib = File.join(path, 'CPF.bundle') unless File.exists?(lib)
8
+ ffi_lib lib
6
9
  attach_function :calc_check_digit, [:long_long], :long_long
7
10
  attach_function :is_valid, [:long_long], :bool
8
11
  end
@@ -1,5 +1,5 @@
1
1
  module BR
2
2
  class CPF
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,55 +1,57 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: br-cpf
3
- version: !ruby/object:Gem::Version
4
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
5
+ version: 1.0.1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Bruno Coimbra
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-04 00:00:00.000000000 -03:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2012-10-08 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
16
  name: ffi
17
- requirement: &78340280 !ruby/object:Gem::Requirement
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
18
  none: false
19
- requirements:
19
+ requirements:
20
20
  - - ~>
21
- - !ruby/object:Gem::Version
22
- version: '1.0'
21
+ - !ruby/object:Gem::Version
22
+ version: "1.0"
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *78340280
26
- - !ruby/object:Gem::Dependency
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &78340020 !ruby/object:Gem::Requirement
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
29
  none: false
30
- requirements:
30
+ requirements:
31
31
  - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '1.3'
32
+ - !ruby/object:Gem::Version
33
+ version: "1.3"
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *78340020
37
- description: ''
38
- email:
36
+ version_requirements: *id002
37
+ description: ""
38
+ email:
39
39
  - bbcoimbra@gmail.com
40
40
  executables: []
41
- extensions:
41
+
42
+ extensions:
42
43
  - ext/CPF/extconf.rb
43
44
  extra_rdoc_files: []
44
- files:
45
+
46
+ files:
45
47
  - .document
46
48
  - .gitignore
47
49
  - Gemfile
48
50
  - Gemfile.lock
49
51
  - LICENSE
50
- - README.rdoc
52
+ - README.markdown
51
53
  - Rakefile
52
- - VERSION
54
+ - br-cpf.gemspec
53
55
  - ext/CPF/cpf.c
54
56
  - ext/CPF/cpf.h
55
57
  - ext/CPF/extconf.rb
@@ -60,29 +62,41 @@ files:
60
62
  - spec/faker_cpf.rb
61
63
  - spec/spec.opts
62
64
  - spec/spec_helper.rb
63
- has_rdoc: true
64
- homepage: ''
65
+ homepage: ""
65
66
  licenses: []
67
+
66
68
  post_install_message:
67
69
  rdoc_options: []
68
- require_paths:
70
+
71
+ require_paths:
69
72
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
73
+ required_ruby_version: !ruby/object:Gem::Requirement
71
74
  none: false
72
- requirements:
73
- - - ! '>='
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3017461016806407857
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
83
  none: false
78
- requirements:
79
- - - ! '>='
80
- - !ruby/object:Gem::Version
81
- version: '0'
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3017461016806407857
88
+ segments:
89
+ - 0
90
+ version: "0"
82
91
  requirements: []
92
+
83
93
  rubyforge_project:
84
- rubygems_version: 1.6.2
94
+ rubygems_version: 1.8.24
85
95
  signing_key:
86
96
  specification_version: 3
87
97
  summary: Lib implemented in C that calculates and validates CPF using FFI.
88
- test_files: []
98
+ test_files:
99
+ - spec/br-cpf_spec.rb
100
+ - spec/faker_cpf.rb
101
+ - spec/spec.opts
102
+ - spec/spec_helper.rb
@@ -1,88 +0,0 @@
1
- = BR::CPF
2
-
3
- Lib implementada em C (gera uma lib compilada nativamente) que calcula e valida CPFs.
4
- Recomendada para Validar CPFs ou Gerar CPFs quando o sistema requer que sejam feitas mais de 1000 Validações/Gerações por segundo.
5
-
6
- == Instalação
7
-
8
- sudo gem install br-cpf
9
-
10
- == Exemplos
11
-
12
- === Usar
13
- require 'rubygems'
14
- require 'br/cpf'
15
-
16
- === Validar
17
-
18
- BR::CPF.valid? '00000000191'
19
- => true
20
-
21
- BR::CPF.valid? 12345678909
22
- => true
23
-
24
- BR::CPF.valid? 12345678900
25
- => false
26
-
27
- === Gerar CPF
28
-
29
- raiz = 1
30
- BR::CPF.new(raiz).to_s
31
- => '00000000191'
32
-
33
- == Benchmark sobre uma implementação puro Ruby
34
- Comparação de validação com a brcpfcnpj (Brazilian Rails)
35
-
36
- $ cat benchmark.rb
37
- require "rubygems"
38
- require "br/cpf"
39
- require "brcpfcnpj"
40
- require "benchmark"
41
-
42
- [1, 10, 100, 1000, 10_000, 100_000, 1_000_000].each do |n|
43
- puts "Teste com n = #{n}"
44
- Benchmark.bm do |x|
45
- x.report { n.times { BR::CPF.valid?(rand(999_999_999_99).to_s.rjust(11, '0')) } }
46
- x.report { n.times { Cpf.new(rand(999_999_999_99).to_s.rjust(11, '0')).valido? } }
47
- end
48
- end
49
-
50
- Resultados
51
-
52
- Teste com n = 1
53
- user system total real
54
- 0.000000 0.000000 0.000000 ( 0.000053)
55
- 0.000000 0.000000 0.000000 ( 0.000243)
56
- Teste com n = 10
57
- user system total real
58
- 0.000000 0.000000 0.000000 ( 0.000080)
59
- 0.000000 0.000000 0.000000 ( 0.001187)
60
- Teste com n = 100
61
- user system total real
62
- 0.000000 0.000000 0.000000 ( 0.002193)
63
- 0.020000 0.000000 0.020000 ( 0.011373)
64
- Teste com n = 1000
65
- user system total real
66
- 0.020000 0.000000 0.020000 ( 0.017329)
67
- 0.150000 0.000000 0.150000 ( 0.169375)
68
- Teste com n = 10000
69
- user system total real
70
- 0.100000 0.000000 0.100000 ( 0.094112)
71
- 1.490000 0.010000 1.500000 ( 1.512164)
72
- Teste com n = 100000
73
- user system total real
74
- 0.850000 0.000000 0.850000 ( 0.874577)
75
- 15.050000 0.010000 15.060000 ( 15.193196)
76
- Teste com n = 1000000
77
- user system total real
78
- 8.570000 0.010000 8.580000 ( 8.631888)
79
- 150.620000 0.210000 150.830000 (152.158936)
80
-
81
- == Desvantagens
82
- Por precisar compilar a extensão em C, acaba incompatível com sistemas que não tem um compilador em C (Windows).
83
-
84
- E quando o ritmo de comparação for menor que 1000 por segundo a diferença é pequena, por volta de de 0.15 segundos. Talvez o acumulo de dependência não valha a pena.
85
-
86
- == Copyright
87
-
88
- Copyright (c) 2010 Bruno Coimbra. Veja o arquivo LICENSE para mais detalhes.
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.2