Class: SecID::Base
- Inherits:
-
Object
- Object
- SecID::Base
- Includes:
- Generatable, Normalizable, Validatable
- Defined in:
- lib/sec_id/base.rb
Overview
Base class for securities identifiers that provides a common interface for validation, normalization, and parsing.
Subclasses must implement:
- ID_REGEX constant with named capture groups for parsing
- initialize method that calls parse and extracts components
Subclasses with checksum should also include the Checkable concern, which provides checksum validation, calculation, and restoration.
Direct Known Subclasses
BIC, CEI, CFI, CIK, CUSIP, DTI, FIGI, FISN, IBAN, ISIN, LEI, OCC, SEDOL, UPI, Valoren, WKN
Constant Summary
Constants included from Generatable
Generatable::ALPHA, Generatable::ALPHANUMERIC, Generatable::DIGITS
Constants included from Validatable
Constants included from Normalizable
Instance Attribute Summary collapse
-
#full_id ⇒ String
readonly
The original input after normalization (stripped and uppercased).
-
#identifier ⇒ String?
readonly
The main identifier portion (without checksum).
Class Method Summary collapse
-
.detection_priority ⇒ Array
private
Composite sort key ranking detection specificity: checksum types first, then narrower length range, then registration order.
-
.example ⇒ String
A representative valid identifier string.
-
.full_name ⇒ String
The full human-readable standard name.
-
.has_check_digit? ⇒ Boolean
deprecated
Deprecated.
Use Base.has_checksum?. Kept as a v7 bridge; removed in v8.
-
.has_checksum? ⇒ Boolean
True if this identifier type uses a checksum.
-
.id_length ⇒ Integer, ...
The fixed length, valid length range, or discrete valid lengths.
-
.length_specificity ⇒ Integer
Specificity weight from ID_LENGTH: fewer valid lengths ranks more specific.
-
.length_values ⇒ Array<Integer>, Range
Valid length values, for length-table indexing.
-
.short_name ⇒ String
The unqualified class name (e.g. "ISIN", "CUSIP").
-
.type_key ⇒ Symbol
The type's registry symbol:
SecID[SecID::ISIN.type_key] == SecID::ISIN.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#as_json ⇒ Hash
Returns a JSON-compatible hash representation.
-
#deconstruct_keys(_keys) ⇒ Hash
Exposes the parsed components for
case/indestructuring. - #hash ⇒ Integer
-
#initialize(_sec_id_number) ⇒ Base
constructor
Subclasses must override this method.
-
#to_h ⇒ Hash
Returns a hash representation of this identifier for serialization.
Methods included from Validatable
#errors, #valid?, #validate, #validate!
Methods included from Normalizable
#normalize!, #normalized, #to_pretty_s, #to_s, #to_str
Constructor Details
#initialize(_sec_id_number) ⇒ Base
Subclasses must override this method.
119 120 121 |
# File 'lib/sec_id/base.rb', line 119 def initialize(_sec_id_number) raise NotImplementedError end |
Instance Attribute Details
#full_id ⇒ String (readonly)
Returns the original input after normalization (stripped and uppercased).
47 48 49 |
# File 'lib/sec_id/base.rb', line 47 def full_id @full_id end |
#identifier ⇒ String? (readonly)
Returns the main identifier portion (without checksum).
50 51 52 |
# File 'lib/sec_id/base.rb', line 50 def identifier @identifier end |
Class Method Details
.detection_priority ⇒ Array
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.
Composite sort key ranking detection specificity: checksum types first, then narrower length range, then registration order. A class the registry never saw sorts last.
64 65 66 67 |
# File 'lib/sec_id/base.rb', line 64 def detection_priority @detection_priority ||= [has_checksum? ? 0 : 1, length_specificity, SecID.identifiers.index(self) || Float::INFINITY].freeze end |
.example ⇒ String
Returns a representative valid identifier string.
90 |
# File 'lib/sec_id/base.rb', line 90 def example = self::EXAMPLE |
.full_name ⇒ String
Returns the full human-readable standard name.
73 |
# File 'lib/sec_id/base.rb', line 73 def full_name = self::FULL_NAME |
.has_check_digit? ⇒ Boolean
Use has_checksum?. Kept as a v7 bridge; removed in v8.
102 103 104 105 |
# File 'lib/sec_id/base.rb', line 102 def has_check_digit? SecID::Deprecation.warn(old: 'has_check_digit?', new: 'has_checksum?') has_checksum? end |
.has_checksum? ⇒ Boolean
Returns true if this identifier type uses a checksum.
93 94 95 96 97 |
# File 'lib/sec_id/base.rb', line 93 def has_checksum? return @has_checksum if defined?(@has_checksum) @has_checksum = ancestors.include?(SecID::Checkable) end |
.id_length ⇒ Integer, ...
Returns the fixed length, valid length range, or discrete valid lengths.
76 |
# File 'lib/sec_id/base.rb', line 76 def id_length = self::ID_LENGTH |
.length_specificity ⇒ Integer
Specificity weight from ID_LENGTH: fewer valid lengths ranks more specific.
87 |
# File 'lib/sec_id/base.rb', line 87 def length_specificity = (v = self::ID_LENGTH).is_a?(Integer) ? 1 : v.size |
.length_values ⇒ Array<Integer>, Range
Valid length values, for length-table indexing. Integer wraps to a one-element Array; Range and Array both yield their own elements.
82 |
# File 'lib/sec_id/base.rb', line 82 def length_values = (v = self::ID_LENGTH).is_a?(Integer) ? [v] : v |
.short_name ⇒ String
Returns the unqualified class name (e.g. "ISIN", "CUSIP").
70 |
# File 'lib/sec_id/base.rb', line 70 def short_name = @short_name ||= name.split('::').last |
.type_key ⇒ Symbol
The type's registry symbol: SecID[SecID::ISIN.type_key] == SecID::ISIN.
56 |
# File 'lib/sec_id/base.rb', line 56 def type_key = @type_key ||= short_name.downcase.to_sym |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
125 126 127 |
# File 'lib/sec_id/base.rb', line 125 def ==(other) other.class == self.class && comparison_id == other.comparison_id end |
#as_json ⇒ Hash
Returns a JSON-compatible hash representation.
152 153 154 |
# File 'lib/sec_id/base.rb', line 152 def as_json(*) to_h end |
#deconstruct_keys(_keys) ⇒ Hash
Exposes the parsed components for case/in destructuring. Validity is not part of
the protocol: components of unparseable input are nil, and SecID.parse returning
nil is the validity guard.
168 |
# File 'lib/sec_id/base.rb', line 168 def deconstruct_keys(_keys) = components_with_deprecation_bridge |
#hash ⇒ Integer
132 133 134 |
# File 'lib/sec_id/base.rb', line 132 def hash [self.class, comparison_id].hash end |
#to_h ⇒ Hash
Returns a hash representation of this identifier for serialization.
139 140 141 142 143 144 145 146 147 |
# File 'lib/sec_id/base.rb', line 139 def to_h { type: self.class.type_key, full_id: full_id, normalized: valid? ? normalized : nil, valid: valid?, components: components_with_deprecation_bridge } end |