Module: Sjekksum::Primitive97

Extended by:
Primitive97, Shared
Included in:
Primitive97
Defined in:
lib/sjekksum/primitive97.rb

Overview

Module for calculation and validation of Primitive97 checksums

This primitive checksum is based on the modulo 97 (calculation of check digits) and modulo 9 (reduction to a single digit).

Instance Method Summary collapse

Instance Method Details

#convert(number) ⇒ Integer, String Also known as: transform

Transforms a number by appending the Primitive97 checksum digit

Examples:

Sjekksum::Primitive97.convert(23569) #=> 235695

Parameters:

  • number (Integer, String)

    number without a checksum

Returns:

  • (Integer, String)

    final number including the checksum


51
52
53
54
# File 'lib/sjekksum/primitive97.rb', line 51

def convert number
  raise_on_type_mismatch number
  typed_conversion number
end

#of(number) ⇒ Integer Also known as: checksum

Calculates Primitive97 checksum

Examples:

Sjekksum::Primitive97.of(23569) #=> 5

Parameters:

  • number (Integer, String)

    number for which the checksum should be calculated

Returns:

  • (Integer)

    calculated checksum


20
21
22
23
# File 'lib/sjekksum/primitive97.rb', line 20

def of number
  raise_on_type_mismatch number
  (convert_to_int(number) % 97) % 9
end

#valid?(number) ⇒ Boolean Also known as: is_valid?

Primitive97 validation of provided number

Examples:

Sjekksum::Primitive97.valid?(235695) #=> true

Parameters:

  • number (Integer, String)

    number with included checksum

Returns:

  • (Boolean)

35
36
37
38
39
# File 'lib/sjekksum/primitive97.rb', line 35

def valid? number
  raise_on_type_mismatch number
  num, check = split_number(number)
  self.of(num) == check
end