Sha256: 57175f413faa4a3aa351edad650654a1d5a7b304fe1def8fa281e72aeb7d8de0

Contents?: true

Size: 551 Bytes

Versions: 1

Compression:

Stored size: 551 Bytes

Contents

# frozen_string_literal: true

class CnpjValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    cnpj = ValidatesCnpj::Cnpj.new(value)

    if cnpj.valid?
      record.send("#{attribute}=", cnpj.number) if options[:mask]
    else
      ruby_prior_version_three =
        Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.0.0')

      if ruby_prior_version_three
        record.errors.add(attribute, :invalid, options)
      else
        record.errors.add(attribute, :invalid, **options)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
validates_cnpj-3.1.0 lib/validates_cnpj/cnpj_validator.rb