Module: SecID::Checkable
- Defined in:
- lib/sec_id/concerns/checkable.rb
Overview
Provides checksum validation and calculation for securities identifiers. Include this module in classes that have a checksum as part of their format.
Including classes must implement:
calculate_checksummethod that returns the calculated checksum value
This module provides:
- Character-to-digit mapping constants
- Luhn algorithm variants for checksum calculation
valid?override that validates format and checksumrestoremethod returning full identifier string without mutationrestore!method to calculate and set the checksum, returningselfchecksumattribute- Class-level convenience methods:
restore,restore!,checksum
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CHAR_TO_DIGIT =
Character-to-digit mapping for single-digit conversion. Maps alphanumeric characters to values 0-38 (A=10, B=11, ..., Z=35, *=36, @=37, #=38). Used by CUSIP, FIGI, SEDOL, LEI, and IBAN for checksum calculations.
{ '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 18, 'J' => 19, 'K' => 20, 'L' => 21, 'M' => 22, 'N' => 23, 'O' => 24, 'P' => 25, 'Q' => 26, 'R' => 27, 'S' => 28, 'T' => 29, 'U' => 30, 'V' => 31, 'W' => 32, 'X' => 33, 'Y' => 34, 'Z' => 35, '*' => 36, '@' => 37, '#' => 38 }.freeze
- BYTE_TO_DIGIT =
ASCII byte lookup avoids allocating a String for each checksum character.
CHAR_TO_DIGIT.transform_keys(&:ord).freeze
- DECIMAL_VALUES =
Decimal expansions reused by LEI and IBAN without allocating replacement strings.
CHAR_TO_DIGIT.transform_values { |value| value.to_s.freeze }.freeze
- LUHN_DOUBLE =
Doubled decimal digits with their digits summed, for expanded ISIN Luhn.
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9].freeze
Class Method Summary collapse
-
.included(base) ⇒ void
private
Extends the including identifier class with the concern's class methods and checksum reader.
Instance Method Summary collapse
-
#calculate_checksum ⇒ Integer, String
Subclasses must override this method to implement their checksum algorithm.
-
#restore ⇒ String
Returns the full identifier string with correct checksum without mutation.
-
#restore! ⇒ self
Calculates and sets the checksum, updating full_id.
- #to_s ⇒ String
-
#valid? ⇒ Boolean
Validates format and checksum.
Class Method Details
.included(base) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Extends the including identifier class with the concern's class methods and checksum reader.
59 60 61 62 |
# File 'lib/sec_id/concerns/checkable.rb', line 59 def self.included(base) base.attr_reader :checksum base.extend(ClassMethods) end |
Instance Method Details
#calculate_checksum ⇒ Integer, String
Subclasses must override this method to implement their checksum algorithm.
122 123 124 |
# File 'lib/sec_id/concerns/checkable.rb', line 122 def calculate_checksum raise NotImplementedError end |
#restore ⇒ String
Returns the full identifier string with correct checksum without mutation.
103 104 105 |
# File 'lib/sec_id/concerns/checkable.rb', line 103 def restore "#{identifier}#{calculate_checksum.to_s.rjust(checksum_width, '0')}" end |
#restore! ⇒ self
Calculates and sets the checksum, updating full_id.
111 112 113 114 115 |
# File 'lib/sec_id/concerns/checkable.rb', line 111 def restore! @checksum = calculate_checksum @full_id = to_s self end |
#to_s ⇒ String
127 128 129 |
# File 'lib/sec_id/concerns/checkable.rb', line 127 def to_s "#{identifier}#{checksum&.to_s&.rjust(checksum_width, '0')}" end |
#valid? ⇒ Boolean
Validates format and checksum.
95 96 97 |
# File 'lib/sec_id/concerns/checkable.rb', line 95 def valid? super && checksum == calculate_checksum end |