Class: KontoAPI::BankAccountValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/kontoapi-rails/validators/bank_account_validator.rb

Instance Method Summary collapse

Instance Method Details

#change_method(record, field) ⇒ Object



15
16
17
18
19
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 15

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

#handle_timeout(ex, record, options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 28

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

#stop?(record, account_number, bank_code, options) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 21

def stop?(record, , bank_code, options)
   = record.send( change_method(record, options[:account_number_field]) )
  bank_code_changed      = record.send( change_method(record, options[:bank_code_field]) )
  return true unless  || bank_code_changed
  return true if options[:allow_nil] && (.nil? || bank_code.nil?)
end

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 6

def validate(record)
    = record.send(:"#{options[:account_number_field]}")
  bank_code       = record.send(:"#{options[:bank_code_field]}")
  return true if stop?(record, , bank_code, options)
  record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => , :blz => bank_code )
rescue Timeout::Error => ex
  handle_timeout(ex, record, options)
end