Sha256: 02b85cfa4a249b9ff109ff4956a594feeab1571cea6baadc34018a86a4c31e21

Contents?: true

Size: 645 Bytes

Versions: 1

Compression:

Stored size: 645 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_routing_number || options[:message]))
  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

1 entries across 1 versions & 1 rubygems

Version Path
bank-validator-0.2.2 lib/active_model/routing_number_validator.rb