-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffce530
commit eba8010
Showing
20 changed files
with
215 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.rvmrc | ||
.config | ||
.yardoc | ||
.coveralls.yml | ||
*.swp | ||
Gemfile.lock | ||
pkg/* | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
tmp | ||
.*~ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
--colour --format documentation | ||
--colour --format documentation --profile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
rvm: | ||
- 1.8.7 | ||
- 1.9.2 | ||
- 1.9.3 | ||
- jruby | ||
- ree | ||
gemfile: | ||
- gemfiles/Gemfile.rails3 | ||
- gemfiles/Gemfile.rails4 | ||
matrix: | ||
exclude: | ||
- rvm: 1.8.7 | ||
gemfile: gemfiles/Gemfile.rails4 | ||
- rvm: 1.9.2 | ||
gemfile: gemfiles/Gemfile.rails4 | ||
- rvm: ree | ||
gemfile: gemfiles/Gemfile.rails4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source :rubygems | ||
|
||
gem 'activesupport', '3.2.14' | ||
|
||
gemspec :path => '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source :rubygems | ||
|
||
gem 'activesupport', '>= 4.0.0' | ||
|
||
gemspec :path => '../' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class CnpjValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
record.errors.add(attribute, :invalid, options) unless ValidatesCnpj::Cnpj.new(value).valid? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'shoulda-matchers' | ||
require 'active_support/core_ext/array/wrap' | ||
|
||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
def require_a_valid_cnpj(attr = :cnpj) | ||
RequireAValidCnpjMatcher.new(attr) | ||
end | ||
|
||
class RequireAValidCnpjMatcher < ValidationMatcher | ||
def description | ||
'require a valid CNPJ number' | ||
end | ||
|
||
def matches?(subject) | ||
@subject = subject | ||
disallows_value_of('123456') && allows_value_of('51.114.450/0001-46') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,6 @@ | ||
require "validates_cnpj/cnpj" | ||
require 'cnpj_validator' | ||
require 'require_a_valid_cnpj_matcher' if defined?(::Shoulda) | ||
|
||
#Rspec Matchers | ||
require "validates_cnpj/shoulda-matchers/require_a_valid_cnpj_matcher" if defined?(::Shoulda) | ||
require "validates_cnpj/remarkable/require_a_valid_cnpj_matcher" if defined?(::Remarkable) | ||
|
||
class CnpjValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
key = :"activerecord.errors.models.#{record.class.name.downcase}.attributes.#{attribute.to_s}.invalid" | ||
record.errors[attribute] << I18n.t(key, :default => :"activerecord.errors.messages.invalid") unless CNPJ.new(value).valid? | ||
end | ||
module ValidatesCnpj | ||
autoload :Cnpj, 'validates_cnpj/cnpj' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,54 @@ | ||
class CNPJ | ||
def initialize(number) | ||
number =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\d{2})$/ | ||
@number = number | ||
@pure_number = $1 | ||
@result = $2 | ||
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.\/-]/, "") | ||
format_number! if @pure_number | ||
end | ||
|
||
def valid? | ||
return true if @number.nil? | ||
return false unless @pure_number | ||
check_cnpj | ||
end | ||
|
||
def number | ||
@number | ||
end | ||
|
||
private | ||
|
||
def check_cnpj | ||
return false if @cleaned_number.length != 14 or @cleaned_number.scan(/\d/).uniq.length == 1 | ||
@result == first_digit_verifier + second_digit_verifier | ||
end | ||
|
||
def first_digit_verifier | ||
sum = multiply_and_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number) | ||
digit_verifier(sum%11).to_s | ||
end | ||
|
||
def second_digit_verifier | ||
sum = multiply_and_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier) | ||
digit_verifier(sum%11).to_s | ||
end | ||
|
||
def multiply_and_sum(array, number) | ||
multiplied = [] | ||
number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] } | ||
multiplied.inject { |s,e| s + e } | ||
end | ||
|
||
def digit_verifier(rest) | ||
rest < 2 ? 0 : 11 - rest | ||
end | ||
|
||
def format_number! | ||
@cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/ | ||
@number = "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}" | ||
module ValidatesCnpj | ||
class Cnpj | ||
def initialize(number) | ||
number =~ /^(\d{2}\.?\d{3}\.?\d{3}\/?\d{4})-?(\d{2})$/ | ||
@number = number | ||
@pure_number = $1 | ||
@result = $2 | ||
@cleaned_number = @pure_number.nil? ? nil : @number.gsub(/[\.\/-]/, "") | ||
format_number! if @pure_number | ||
end | ||
|
||
def valid? | ||
return true if @number.blank? | ||
return false unless @pure_number | ||
check_cnpj | ||
end | ||
|
||
def number | ||
@number | ||
end | ||
|
||
private | ||
|
||
def check_cnpj | ||
return false if @cleaned_number.length != 14 or @cleaned_number.scan(/\d/).uniq.length == 1 | ||
@result == first_digit_verifier + second_digit_verifier | ||
end | ||
|
||
def first_digit_verifier | ||
sum = multiply_and_sum([5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number) | ||
digit_verifier(sum%11).to_s | ||
end | ||
|
||
def second_digit_verifier | ||
sum = multiply_and_sum([6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2], @pure_number + first_digit_verifier) | ||
digit_verifier(sum%11).to_s | ||
end | ||
|
||
def multiply_and_sum(array, number) | ||
multiplied = [] | ||
number.scan(/\d{1}/).each_with_index { |e, i| multiplied[i] = e.to_i * array[i] } | ||
multiplied.inject { |s,e| s + e } | ||
end | ||
|
||
def digit_verifier(rest) | ||
rest < 2 ? 0 : 11 - rest | ||
end | ||
|
||
def format_number! | ||
@cleaned_number =~ /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/ | ||
@number = "#{$1}.#{$2}.#{$3}/#{$4}-#{$5}" | ||
end | ||
end | ||
end |
29 changes: 0 additions & 29 deletions
29
lib/validates_cnpj/remarkable/require_a_valid_cnpj_matcher.rb
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
lib/validates_cnpj/shoulda-matchers/require_a_valid_cnpj_matcher.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Admin < ActiveRecord::Base | ||
class Admin | ||
attr_accessor :cnpj | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
class Company < ActiveRecord::Base | ||
class Company | ||
include ActiveModel::Validations | ||
include ActiveModel::Conversion | ||
extend ActiveModel::Naming | ||
|
||
attr_accessor :cnpj, :name | ||
|
||
validates :cnpj, :cnpj => true | ||
|
||
def initialize(attributes = {}) | ||
attributes.each do |key, value| | ||
instance_variable_set("@#{key}", value) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
require "rubygems" | ||
require "rspec" | ||
require "active_record" | ||
require 'rspec' | ||
require 'active_model' | ||
require 'coveralls' | ||
|
||
Dir.glob(File.dirname(__FILE__) + "/../lib/**/*.rb").each { |file| require file } | ||
Dir.glob(File.dirname(__FILE__) + "/fake_app/**/*.rb").each { |file| require file } | ||
Coveralls.wear! | ||
|
||
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") | ||
|
||
CreateCompanies.migrate(:up) | ||
CreateAdmins.migrate(:up) | ||
Dir.glob(File.dirname(__FILE__) + '/../lib/**/*.rb').each { |file| require file } | ||
Dir.glob(File.dirname(__FILE__) + '/fake_app/**/*.rb').each { |file| require file } |
File renamed without changes.
Oops, something went wrong.