Class: KontoAPI::BankAccountValidator
- Inherits:
-
ActiveModel::Validator
- Object
- ActiveModel::Validator
- KontoAPI::BankAccountValidator
- Defined in:
- lib/kontoapi-rails/validators/bank_account_validator.rb
Instance Method Summary collapse
- #change_method(record, field) ⇒ Object
- #handle_timeout(ex, record, options) ⇒ Object
- #stop?(record, account_number, bank_code, options) ⇒ Boolean
- #validate(record) ⇒ Object
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, ) case [:on_timeout] when :fail record.errors.add(:"#{[:account_number_field]}", :timeout) when :ignore # nop when :retry raise 'not implemented yet' end end |
#stop?(record, account_number, bank_code, options) ⇒ Boolean
21 22 23 24 25 26 |
# File 'lib/kontoapi-rails/validators/bank_account_validator.rb', line 21 def stop?(record, account_number, bank_code, ) account_number_changed = record.send( change_method(record, [:account_number_field]) ) bank_code_changed = record.send( change_method(record, [:bank_code_field]) ) return true unless account_number_changed || bank_code_changed return true if [:allow_nil] && (account_number.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) account_number = record.send(:"#{[:account_number_field]}") bank_code = record.send(:"#{[:bank_code_field]}") return true if stop?(record, account_number, bank_code, ) record.errors.add(:"#{[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => account_number, :blz => bank_code ) rescue Timeout::Error => ex handle_timeout(ex, record, ) end |