Sha256: 7263773ed087bbd3c90317c8e71fa2cc0edf375ecbbea0884a3aa45a658574e2
Contents?: true
Size: 1.03 KB
Versions: 2
Compression:
Stored size: 1.03 KB
Contents
require 'active_model' require 'validates_cpf/cpf' module ActiveModel module Validations class CpfValidator < ActiveModel::EachValidator include ValidatesCpf def validate_each(record, attr_name, value) return if (options[:allow_nil] and value.nil?) or (options[:allow_blank] and value.blank?) return if (options[:if] == false) or (options[:unless] == true) return if (options[:on].to_s == 'create' and not record.new_record?) or (options[:on].to_s == 'update' and record.new_record?) if value.to_s.gsub(/[^0-9]/, '').length <= 11 if (not value.to_s.match(/\A\d{11}\z/) and not value.to_s.match(/\A\d{3}\.\d{3}\.\d{3}\-\d{2}\z/)) or not Cpf.valid?(value) record.errors.add(attr_name) end end end end module HelperMethods def validates_cpf(*attr_names) raise ArgumentError, "You need to supply at least one attribute" if attr_names.empty? validates_with CpfValidator, _merge_attributes(attr_names) end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
cpf_validation-0.2.0 | lib/validates_cpf.rb |
cpf_validation-0.1.0 | lib/validates_cpf.rb |