Sha256: 2b1330f21eb792eaf3fd00853476fc55765693c364416fb28aa4fcfba7653cb3

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, (options[:message] || :invalid_routing_number))
  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.3 lib/active_model/routing_number_validator.rb