Permalink
Browse files

extrating doc logic from entities

  • Loading branch information...
1 parent 10bdc85 commit 2111fae287aac614dd2d5849d2cd386028afc782 @mhfs committed Apr 6, 2012
Showing with 198 additions and 110 deletions.
  1. +5 −2 application.rb
  2. +33 −0 lib/cnpj.rb
  3. +6 −28 lib/company.rb
  4. +32 −0 lib/cpf.rb
  5. +18 −0 lib/document.rb
  6. +14 −11 lib/entity.rb
  7. +6 −27 lib/person.rb
  8. +38 −0 spec/cnpj_spec.rb
  9. +4 −21 spec/company_spec.rb
  10. +38 −0 spec/cpf_spec.rb
  11. +4 −21 spec/person_spec.rb
View
7 application.rb
@@ -13,6 +13,9 @@
require 'lib/entity'
require 'lib/person'
require 'lib/company'
+require 'lib/document'
+require 'lib/cnpj'
+require 'lib/cpf'
Faker::Config.locale = "pt-br"
@@ -35,9 +38,9 @@
if params[:document].empty?
@result = {:status => :notice, :message => "Informe o CPF ou CNPJ."}
else
- @result = if Person.new(params[:document]).valid?
+ @result = if Cpf.new(params[:document]).valid?
{:status => :success, :message => "CPF Válido."}
- elsif Company.new(params[:document]).valid?
+ elsif Cnpj.new(params[:document]).valid?
{:status => :success, :message => "CNPJ Válido."}
else
{:status => :error, :message => "Documento inválido."}
View
33 lib/cnpj.rb
@@ -0,0 +1,33 @@
+class Cnpj < Document
+ def self.generate
+ digits = []
+ 8.times { digits << rand(9) }
+ digits += [0, 0, 0, 1]
+
+ 2.times { digits << verification_digit_for(digits) }
+ new(digits.join)
+ end
+
+ def valid?
+ return false unless number.size == 14
+ original = number.split('').map(&:to_i)
+ new = original[0..11]
+ 2.times { new << self.class.verification_digit_for(new) }
+ original == new
+ end
+
+ def to_s
+ human_number
+ end
+
+ def human_number
+ number =~ /(\d{2})\.?(\d{3})\.?(\d{3})\/?(\d{4})-?(\d{2})/
+ "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
+ end
+
+ private
+
+ def self.increment_value(i)
+ i < 9 ? i + 1 : 2
+ end
+end
View
34 lib/company.rb
@@ -1,34 +1,12 @@
class Company < Entity
- def valid?
- original = document.gsub(/[a-zA-Z\.\(\)\/\-|\s]/,'').split('').map(&:to_i)
- return false unless original.size == 14
- new = original[0..11]
- 2.times { new << verification_digit_for(new) }
- original == new
- end
private
- # generate a CNPJ
- def generate_document
- digits = []
- 8.times { digits << rand(9) }
- digits += [0, 0, 0, 1]
-
- 2.times { digits << verification_digit_for(digits) }
- format_number(digits)
- end
-
- def generate_name
- Faker::Company.name
- end
-
- def increment_value(i)
- i < 9 ? i + 1 : 2
- end
+ def generate_doc
+ Cnpj.generate
+ end
- def format_number(array)
- array.join =~ /(\d{2})\.?(\d{3})\.?(\d{3})\/?(\d{4})-?(\d{2})/
- "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}"
- end
+ def generate_name
+ Faker::Company.name
+ end
end
View
32 lib/cpf.rb
@@ -0,0 +1,32 @@
+class Cpf < Document
+ def self.generate
+ digits = []
+ 9.times { digits << rand(9) }
+
+ 2.times { digits << self.verification_digit_for(digits) }
+ new(digits.join)
+ end
+
+ def valid?
+ return false unless number.length == 11
+ original = number.split('').map(&:to_i)
+ new = original[0..8]
+ 2.times { new << self.class.verification_digit_for(new) }
+ original == new
+ end
+
+ def to_s
+ human_number
+ end
+
+ def human_number
+ number =~ /(\d{3})\.?(\d{3})\.?(\d{3})-?(\d{2})/
+ "#{$1}.#{$2}.#{$3}-#{$4}"
+ end
+
+ private
+
+ def self.increment_value(i)
+ i + 1
+ end
+end
View
18 lib/document.rb
@@ -0,0 +1,18 @@
+class Document
+
+ attr_accessor :number
+
+ def initialize(number)
+ self.number = number.strip.gsub(/[a-zA-Z\.\(\)\/\-|\s]/,'')
+ end
+
+ def self.verification_digit_for(known_digits)
+ i = 1
+ sums = known_digits.reverse.collect do |d|
+ i = increment_value(i)
+ d * i
+ end
+ vd = 11 - sums.inject(0){|sum,item| sum + item} % 11
+ vd < 10 ? vd : 0
+ end
+end
View
25 lib/entity.rb
@@ -1,19 +1,22 @@
class Entity
attr_accessor :name, :document
- def initialize(doc = nil)
- self.document = doc.nil? ? generate_document : doc
+ def initialize
+ self.document = generate_doc
self.name = generate_name
end
+ def document_number
+ document.number
+ end
+
private
- def verification_digit_for(known_digits)
- i = 1
- sums = known_digits.reverse.collect do |d|
- i = increment_value(i)
- d * i
- end
- vd = 11 - sums.inject(0){|sum,item| sum + item} % 11
- vd < 10 ? vd : 0
- end
+
+ def generate_doc
+ raise NotImplementedError
+ end
+
+ def generate_name
+ raise NotImplementedError
+ end
end
View
33 lib/person.rb
@@ -1,33 +1,12 @@
class Person < Entity
- def valid?
- original = document.gsub(/[a-zA-Z\.\(\)\/\-|\s]/,'').split('').map(&:to_i)
- return false unless original.size == 11
- new = original[0..8]
- 2.times { new << verification_digit_for(new) }
- original == new
- end
private
- # generate a cpf
- def generate_document
- digits = []
- 9.times { digits << rand(9) }
-
- 2.times { digits << verification_digit_for(digits) }
- format_number(digits)
- end
-
- def generate_name
- Faker::Name.name
- end
-
- def increment_value(i)
- i + 1
- end
+ def generate_doc
+ Cpf.generate
+ end
- def format_number(array)
- array.join =~ /(\d{3})\.?(\d{3})\.?(\d{3})-?(\d{2})/
- "#{$1}.#{$2}.#{$3}-#{$4}"
- end
+ def generate_name
+ Faker::Name.name
+ end
end
View
38 spec/cnpj_spec.rb
@@ -0,0 +1,38 @@
+require "spec_helper"
+
+describe Cnpj do
+ let(:cnpj) { Cnpj.new("12.123.123/0001-01") }
+
+ it "should initialize by number and clean it" do
+ cnpj.number.should eq("12123123000101")
+ end
+
+ it "should give humanized number" do
+ cnpj.human_number.should eq("12.123.123/0001-01")
+ end
+
+ it "should to_s to humanized version" do
+ cnpj.to_s.should eq("12.123.123/0001-01")
+ end
+
+ it "should validate valid CNPJ" do
+ Cnpj.new('24.270.523/0001-16').should be_valid
+ Cnpj.new('24270523000116').should be_valid
+ end
+
+ it "should invalidate invalid CNPJ" do
+ Cnpj.new('24.270.523/0001-66').should_not be_valid
+ Cnpj.new('123456').should_not be_valid
+ Cnpj.new('').should_not be_valid
+ end
+
+ it "should generate random CNPJs" do
+ cnpjs = []
+ 10.times do
+ cnpj = described_class.generate
+ cnpj.should be_valid
+ cnpjs.should_not include(cnpj.number)
+ cnpjs << cnpj.number
+ end
+ end
+end
View
25 spec/company_spec.rb
@@ -8,28 +8,11 @@
should respond_to :document
end
- it "should generate valid CNPJ" do
- subject.document.length.should == 18
- subject.should be_valid
+ it "should generate document" do
+ subject.document.should be_instance_of(Cnpj)
end
- it "should create random CNPJs" do
- cnpjs = []
- 10.times do
- cnpj = Company.new.document
- cnpjs.should_not include(cnpj)
- cnpjs << cnpj
- end
- end
-
- it "should validate valid CNPJ" do
- Company.new('24.270.523/0001-16').should be_valid
- Company.new('24270523000116').should be_valid
- end
-
- it "should invalidate invalid CNPJ" do
- Company.new('24.270.523/0001-66').should_not be_valid
- Company.new('123456').should_not be_valid
- Company.new('').should_not be_valid
+ it "should generate name" do
+ subject.name.should be_instance_of(String)
end
end
View
38 spec/cpf_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe Cpf do
+ let(:cpf) { Cpf.new("123.123.123-12") }
+
+ it "should initialize by number and clean it" do
+ cpf.number.should eq("12312312312")
+ end
+
+ it "should give humanized number" do
+ cpf.human_number.should eq("123.123.123-12")
+ end
+
+ it "should to_s to humanized version" do
+ cpf.to_s.should eq("123.123.123-12")
+ end
+
+ it "should validate valid CPF" do
+ Cpf.new('082.424.648-98').should be_valid
+ Cpf.new('08242464898').should be_valid
+ end
+
+ it "should invalidate invalid CPF" do
+ Cpf.new('082.424.648-XX').should_not be_valid
+ Cpf.new('123456').should_not be_valid
+ Cpf.new('').should_not be_valid
+ end
+
+ it "should generate random CPFs" do
+ cpfs = []
+ 10.times do
+ cpf = Cpf.generate
+ cpf.should be_valid
+ cpfs.should_not include(cpf.number)
+ cpfs << cpf.number
+ end
+ end
+end
View
25 spec/person_spec.rb
@@ -8,28 +8,11 @@
should respond_to :document
end
- it "should generate valid CPF" do
- subject.document.length.should == 14
- subject.should be_valid
+ it "should generate document" do
+ subject.document.should be_instance_of(Cpf)
end
- it "should create random CPFs" do
- cpfs = []
- 10.times do
- cpf = Person.new.document
- cpfs.should_not include(cpf)
- cpfs << cpf
- end
- end
-
- it "should validate valid CPF" do
- Person.new('082.424.648-98').should be_valid
- Person.new('08242464898').should be_valid
- end
-
- it "should invalidate invalid CPF" do
- Person.new('082.424.648-XX').should_not be_valid
- Person.new('123456').should_not be_valid
- Person.new('').should_not be_valid
+ it "should generate name" do
+ subject.name.should be_instance_of(String)
end
end

0 comments on commit 2111fae

Please sign in to comment.