Sha256: 8f19e5971e267532180529f670081f22a112769d441705d17c0161491dac081b

Contents?: true

Size: 943 Bytes

Versions: 1

Compression:

Stored size: 943 Bytes

Contents

# frozen_string_literal: true

# Based on http://www.swissiban.com/fr.htm
module ActiveModel
  module Validations
    class BankFieldValidator < EachValidator
      def validate_each(record, attribute, value)
        return if value.blank? && options[:allow_blank]
        return if value.nil? && options[:allow_nil]

        return if value.to_s =~ SwissBankValidator::BANK_REGEX

        record.errors.add(
          attribute,
          :invalid_bank_field,
          message: options[:message]
        )
      end
    end

    module ClassMethods
      # Validates whether or not the specified field bank complient.
      #
      #   class Account < ActiveRecord::Base
      #     validates_bank_field_of :name
      #   end
      #
      def validates_bank_field_of(*attr_names)
        validates_with BankFieldValidator, _merge_attributes(attr_names)
      end

      alias validates_bank_field validates_bank_field_of
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
swiss_bank_validator-0.6.0 lib/swiss_bank_validator/validates_bank_field_of.rb