Skip to content

Commit

Permalink
Merge branch 'master' of github.com:javiertoledo/spanish_vat_validators
Browse files Browse the repository at this point in the history
Conflicts:
	lib/spanish_vat_validators.rb
	lib/spanish_vat_validators/version.rb
  • Loading branch information
javiertoledo committed Apr 17, 2013
2 parents 270c10a + 3f0cbd3 commit 3926583
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/spanish_vat_validators.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#encoding: utf-8
require "spanish_vat_validators/version"

module ActiveModel::Validations
Expand All @@ -8,16 +9,20 @@ def message(kind='vat')
end

# Validates NIF
def validate_nif(value)
def validate_nif(v)
return false if v.nil? || v.empty?
value = v.upcase
return false unless value.match(/[0-9]{8}[a-z]/i)
letters = "TRWAGMYFPDXBNJZSQVHLCKE"
check = value.slice!(value.length - 1..value.length - 1).upcase
check = value.slice!(value.length - 1..value.length - 1)
calculated_letter = letters[value.to_i % 23].chr
return check === calculated_letter
end

# Validates CIF
def validate_cif(value)
def validate_cif(v)
return false if v.nil? || v.empty?
value = v.clone
return false unless value.match(/[a-wyz][0-9]{7}[0-9a-z]/i)
pares = 0
impares = 0
Expand Down Expand Up @@ -49,7 +54,9 @@ def validate_cif(value)

# Validates NIE
# a NIE is really a NIF with first number changed to capital X, Y or Z letter
def validate_nie(value)
def validate_nie(v)
return false if v.nil? || v.empty?
value = v.upcase
return false unless value.match(/[xyz][0-9]{7}[a-z]/i)
value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]]
validate_nif(value)
Expand All @@ -60,39 +67,39 @@ def validate_nie(value)
class ValidSpanishVatValidator < ActiveModel::EachValidator
include SpanishVatValidatorsHelpers
def validate_each(record, attribute, value)
record.errors[attribute] = message unless validate_nif(value.clone) or validate_cif(value.clone) or validate_nie(value.clone)
record.errors[attribute] = message unless validate_nif(value) or validate_cif(value) or validate_nie(value)
end
end

# Validates any Spanish person number
class ValidSpanishIdValidator < ActiveModel::EachValidator
include SpanishVatValidatorsHelpers
def validate_each(record, attribute, value)
record.errors[attribute] = message unless validate_nif(value.clone) or validate_nie(value.clone)
record.errors[attribute] = message unless validate_nif(value) or validate_nie(value)
end
end

# Validates NIF number only
class ValidNifValidator < ActiveModel::EachValidator
include SpanishVatValidatorsHelpers
def validate_each(record, attribute,value)
record.errors[attribute] = message('nif') unless validate_nif(value.clone)
record.errors[attribute] = message('nif') unless validate_nif(value)
end
end

# Validates CIF number only
class ValidCifValidator < ActiveModel::EachValidator
include SpanishVatValidatorsHelpers
def validate_each(record, attribute,value)
record.errors[attribute] = message('cif') unless validate_cif(value.clone)
record.errors[attribute] = message('cif') unless validate_cif(value)
end
end

# Validates NIE number only
class ValidNieValidator < ActiveModel::EachValidator
include SpanishVatValidatorsHelpers
def validate_each(record, attribute,value)
record.errors[attribute] = message('nie') unless validate_nie(value.clone)
record.errors[attribute] = message('nie') unless validate_nie(value)
end
end

Expand Down

0 comments on commit 3926583

Please sign in to comment.