Sha256: 5c9502492503152b42acc97cfa652c828dccb0abd082fa83b92762c0b8d07ab8

Contents?: true

Size: 948 Bytes

Versions: 1

Compression:

Stored size: 948 Bytes

Contents

require 'active_model/validator'

module KontoAPI
  class BankAccountValidator < ActiveModel::Validator

    DEFAULTS = {
      :account_number_field => :account_number,
      :bank_code_field      => :bank_code,
      :allow_nil            => true,
      :on_timeout           => :ignore
    }

    def validate(record)
      record_options  = options.reverse_merge(DEFAULTS)
      account_number  = record.send(:"#{record_options[:account_number_field]}")
      bank_code       = record.send(:"#{record_options[:bank_code_field]}")
      record.errors.add(:"#{record_options[:account_number_field]}", :invalid) unless KontoAPI::valid?(account_number, bank_code)
    rescue Timeout::Error => ex
      case record_options[:on_timeout]
      when :fail
        record.errors.add(:"#{record_options[:account_number_field]}", :timeout)
      when :ignore
        # nop
      when :retry
        raise 'not implemented yet'
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kontoapi-rails-0.0.1 lib/kontoapi-rails/validators/bank_account_validator.rb