Module: Sjekksum::Primitive

Extended by:
Primitive, Shared
Included in:
Primitive
Defined in:
lib/sjekksum/primitive.rb

Overview

Module for calculation and validation of Primitive checksums

The primitive checksum is just a reduced digit sum (modulo 9).

Instance Method Summary collapse

Instance Method Details

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

Transforms a number by appending the Primitive checksum digit

Examples:

Sjekksum::Primitive.convert(232323) #=> 2323236

Parameters:

  • number (Integer, String)

    number without a checksum

Returns:

  • (Integer, String)

    final number including the checksum


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

def convert number
  raise_on_type_mismatch number
  typed_conversion number
end

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

Calculates Primitive checksum

Examples:

Sjekksum::Primitive.of(232323) #=> 6

Parameters:

  • number (Integer, String)

    number for which the checksum should be calculated

Returns:

  • (Integer)

    calculated checksum


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

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

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

Primitive validation of provided number

Examples:

Sjekksum::Primitive.valid?(232323) #=> true

Parameters:

  • number (Integer, String)

    number with included checksum

Returns:

  • (Boolean)

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

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