Sha256: 131546111234c23b6f5d1ce9f800889ae235cb63b2c66da5df32aa0936752483

Contents?: true

Size: 618 Bytes

Versions: 6

Compression:

Stored size: 618 Bytes

Contents

class IcoValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    record.errors.add(attribute, :invalid_format) unless valid_ico?(value.to_s)
  end

  private

  def valid_ico?(value)
    value.length == 8 && value.match(/^\d+$/) && last_number_valid?(value)
  end

  def last_number_valid?(value)
    value[7].to_i == calculate_valid_last_number(value)
  end

  def calculate_valid_last_number(value)
    sum = (0..6).inject(0) { |sum, i| sum += value[i].to_i * (8 - i) }
    mod = sum % 11

    case mod
    when 0, 10 then 1
    when 1     then 0
    else 11 - mod
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ico-validator-0.3.1 lib/ico-validator/ico_validator.rb
ico-validator-0.2.1 lib/ico-validator/ico_validator.rb
ico-validator-0.2.0 lib/ico-validator/ico_validator.rb
ico-validator-0.1.1 lib/ico-validator/ico_validator.rb
ico-validator-0.1.0 lib/ico-validator/ico_validator.rb
ico-validator-0.0.1 lib/ico-validator/ico_validator.rb