Class: SecId::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sec_id/base.rb

Overview

Base class for securities identifiers that provides a common interface for validation, check-digit calculation, and parsing.

Subclasses must implement:

  • ID_REGEX constant with named capture groups for parsing

  • initialize method that calls parse and extracts components

  • calculate_check_digit method (only if has_check_digit? returns true)

Subclasses may override:

  • has_check_digit? to return false for identifiers without check digits (e.g., CIK, OCC)

  • valid_format? for additional format validation beyond regex matching

  • to_s for custom string representation

Examples:

Implementing a check-digit identifier

class MyIdentifier < Base
  ID_REGEX = /\A(?<identifier>[A-Z]{6})(?<check_digit>\d)?\z/x

  def initialize(id)
    parts = parse(id)
    @identifier = parts[:identifier]
    @check_digit = parts[:check_digit]&.to_i
  end

  def calculate_check_digit
    validate_format_for_calculation!
    mod10(some_algorithm)
  end
end

Implementing a non-check-digit identifier

class SimpleId < Base
  def has_check_digit?
    false
  end
end

Direct Known Subclasses

CIK, CUSIP, FIGI, IBAN, ISIN, LEI, OCC, SEDOL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_sec_id_number) ⇒ Base

Subclasses must override this method.

Parameters:

  • _sec_id_number (String)

    the identifier string to parse

Raises:

  • (NotImplementedError)

    always raised in base class



111
112
113
# File 'lib/sec_id/base.rb', line 111

def initialize(_sec_id_number)
  raise NotImplementedError
end

Instance Attribute Details

#check_digitInteger? (readonly)

Returns the check digit value.

Returns:

  • (Integer, nil)

    the check digit value



75
76
77
# File 'lib/sec_id/base.rb', line 75

def check_digit
  @check_digit
end

#full_numberString (readonly)

Returns the original input after normalization (stripped and uppercased).

Returns:

  • (String)

    the original input after normalization (stripped and uppercased)



69
70
71
# File 'lib/sec_id/base.rb', line 69

def full_number
  @full_number
end

#identifierString? (readonly)

Returns the main identifier portion (without check digit).

Returns:

  • (String, nil)

    the main identifier portion (without check digit)



72
73
74
# File 'lib/sec_id/base.rb', line 72

def identifier
  @identifier
end

Class Method Details

.check_digit(id) ⇒ Integer

Returns the calculated check digit.

Parameters:

  • id (String)

    the identifier to calculate check digit for

Returns:

  • (Integer)

    the calculated check digit

Raises:



102
103
104
# File 'lib/sec_id/base.rb', line 102

def check_digit(id)
  new(id).calculate_check_digit
end

.restore!(id_without_check_digit) ⇒ String

Restores (calculates) the check digit and returns the full identifier.

Parameters:

  • id_without_check_digit (String)

    identifier without or with incorrect check digit

Returns:

  • (String)

    the full identifier with correct check digit

Raises:



95
96
97
# File 'lib/sec_id/base.rb', line 95

def restore!(id_without_check_digit)
  new(id_without_check_digit).restore!
end

.valid?(id) ⇒ Boolean

Parameters:

  • id (String)

    the identifier to validate

Returns:

  • (Boolean)


80
81
82
# File 'lib/sec_id/base.rb', line 80

def valid?(id)
  new(id).valid?
end

.valid_format?(id) ⇒ Boolean

Parameters:

  • id (String)

    the identifier to check

Returns:

  • (Boolean)


86
87
88
# File 'lib/sec_id/base.rb', line 86

def valid_format?(id)
  new(id).valid_format?
end

Instance Method Details

#calculate_check_digitInteger

Subclasses must override this method.

Returns:

  • (Integer)

    the calculated check digit

Raises:

  • (NotImplementedError)

    always raised in base class

  • (InvalidFormatError)

    if the identifier format is invalid (in subclasses)



149
150
151
# File 'lib/sec_id/base.rb', line 149

def calculate_check_digit
  raise NotImplementedError
end

#has_check_digit?Boolean

Override in subclasses to return false for identifiers without check digits.

Returns:

  • (Boolean)


118
119
120
# File 'lib/sec_id/base.rb', line 118

def has_check_digit?
  true
end

#restore!String

Returns the full identifier with correct check digit.

Returns:

  • (String)

    the full identifier with correct check digit

Raises:



139
140
141
142
# File 'lib/sec_id/base.rb', line 139

def restore!
  @check_digit = calculate_check_digit
  @full_number = to_s
end

#to_sString Also known as: to_str

Returns:

  • (String)


154
155
156
# File 'lib/sec_id/base.rb', line 154

def to_s
  "#{identifier}#{check_digit}"
end

#valid?Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'lib/sec_id/base.rb', line 123

def valid?
  return valid_format? unless has_check_digit?
  return false unless valid_format?

  check_digit == calculate_check_digit
end

#valid_format?Boolean

Override in subclasses for additional format validation.

Returns:

  • (Boolean)


133
134
135
# File 'lib/sec_id/base.rb', line 133

def valid_format?
  !identifier.nil?
end