Sha256: 5d56ca727b38f1698466793c26469a35cdf5f59473fb4900a3ce7e49cf38b847

Contents?: true

Size: 1010 Bytes

Versions: 3

Compression:

Stored size: 1010 Bytes

Contents

class GenericID
  def number_length; 0; end;
  def check_digits_length; 1; end;

  def initialize number
    @number = clear_number(number)

    if number_length > 0 
      raise ArgumentError.new "invalid number" if @number.length != number_length
    end
  end

  def check_digits
    @check_digits ||= begin
      @number[-check_digits_length, check_digits_length]
    end
  end

  def sequential
    @sequential ||= begin
      @number[0, @number.length - check_digits_length]
    end
  end

  def valid?
    @valid ||= begin
      valid_number == @number and not invalid_sequential?
    rescue TypeError
      false
    end
  end

  def invalid_sequential?
    @invalid_sequential ||=begin
      sequential.chars.to_a.uniq.count.eql? 1
    end
  end

  def to_s
    "#{sequential}-#{check_digits}"
  end

  protected
  def mod11 number
    Mod11.new(number)
  end

  def valid_number
    mod11(sequential) * check_digits_length
  end

  def clear_number number
    number.to_s.scan(/\d/).join
  end
end


Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
brid-0.1.3 lib/brid/generic_id.rb
brid-0.1.2 lib/brid/generic_id.rb
brid-0.1.1 lib/brid/generic_id.rb