Class: SecId::Base
- Inherits:
-
Object
- Object
- SecId::Base
- 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
Instance Attribute Summary collapse
-
#check_digit ⇒ Integer?
readonly
The check digit value.
-
#full_number ⇒ String
readonly
The original input after normalization (stripped and uppercased).
-
#identifier ⇒ String?
readonly
The main identifier portion (without check digit).
Class Method Summary collapse
-
.check_digit(id) ⇒ Integer
The calculated check digit.
-
.restore!(id_without_check_digit) ⇒ String
Restores (calculates) the check digit and returns the full identifier.
- .valid?(id) ⇒ Boolean
- .valid_format?(id) ⇒ Boolean
Instance Method Summary collapse
-
#calculate_check_digit ⇒ Integer
Subclasses must override this method.
-
#has_check_digit? ⇒ Boolean
Override in subclasses to return false for identifiers without check digits.
-
#initialize(_sec_id_number) ⇒ Base
constructor
Subclasses must override this method.
-
#restore! ⇒ String
The full identifier with correct check digit.
- #to_s ⇒ String (also: #to_str)
- #valid? ⇒ Boolean
-
#valid_format? ⇒ Boolean
Override in subclasses for additional format validation.
Constructor Details
#initialize(_sec_id_number) ⇒ Base
Subclasses must override this method.
111 112 113 |
# File 'lib/sec_id/base.rb', line 111 def initialize(_sec_id_number) raise NotImplementedError end |
Instance Attribute Details
#check_digit ⇒ Integer? (readonly)
Returns the check digit value.
75 76 77 |
# File 'lib/sec_id/base.rb', line 75 def check_digit @check_digit end |
#full_number ⇒ String (readonly)
Returns 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 |
#identifier ⇒ String? (readonly)
Returns 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.
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.
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
80 81 82 |
# File 'lib/sec_id/base.rb', line 80 def valid?(id) new(id).valid? end |
.valid_format?(id) ⇒ 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_digit ⇒ Integer
Subclasses must override this method.
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.
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.
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_s ⇒ String Also known as: to_str
154 155 156 |
# File 'lib/sec_id/base.rb', line 154 def to_s "#{identifier}#{check_digit}" end |
#valid? ⇒ 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.
133 134 135 |
# File 'lib/sec_id/base.rb', line 133 def valid_format? !identifier.nil? end |