Sha256: b899d4c7ee26a355d0907b22884ddbf583ee33b86c73b29efb8cfea47f8916bc
Contents?: true
Size: 1.42 KB
Versions: 3
Compression:
Stored size: 1.42 KB
Contents
require 'active_model/validator' module KontoAPI class BankAccountValidator < ActiveModel::Validator def validate(record) account_number = record.send(:"#{options[:account_number_field]}") bank_code = record.send(:"#{options[:bank_code_field]}") return true if stop?(record, account_number, bank_code, options) record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => account_number, :blz => bank_code ) rescue Timeout::Error => ex handle_timeout(ex, record, options) end def change_method(record, field) ["#{field}_changed?", "encrypted_#{field}_changed?"].each do |m| return m.to_sym if record.respond_to?( m.to_sym ) end end def stop?(record, account_number, bank_code, options) account_number_changed = record.send( change_method(record, options[:account_number_field]) ) bank_code_changed = record.send( change_method(record, options[:bank_code_field]) ) return true unless account_number_changed || bank_code_changed return true if options[:allow_nil] && (account_number.nil? || bank_code.nil?) end def handle_timeout(ex, record, options) case options[:on_timeout] when :fail record.errors.add(:"#{options[:account_number_field]}", :timeout) when :ignore # nop when :retry raise 'not implemented yet' end end end end
Version data entries
3 entries across 3 versions & 1 rubygems