Sha256: e6adcff162f4b3b35c5df44162239b8a3be042c2124cd7b551814988a5545ccd

Contents?: true

Size: 612 Bytes

Versions: 2

Compression:

Stored size: 612 Bytes

Contents

require 'active_model'

class RoutingNumberValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    record_error(record, attribute, value) unless value =~ regexp && valid_routing_number?(value)
  end
  
  private
  
  def record_error(record, attribute, value)
    record.errors.add(attribute, :invalid_iban)
  end
  
  def regexp
    /^[0-9]{9}$/
  end

  def valid_routing_number?(route_number)
    d = route_number.split(//)

    ((3 * (d[0].to_i + d[3].to_i + d[6].to_i)) + (7 * (d[1].to_i + d[4].to_i + d[7].to_i)) + (d[2].to_i + d[5].to_i + d[8].to_i)) % 10 == 0
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bank-validator-0.2.1 lib/active_model/routing_number_validator.rb
bank-validator-0.2.0 lib/active_model/routing_number_validator.rb