Class: SecID::Base

Inherits:
Object
  • Object
show all
Includes:
IdentifierMetadata, 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 check digits should also include the Checkable concern, which provides check-digit validation, calculation, and restoration.

Examples:

Implementing a check-digit identifier

class MyIdentifier < Base
  include Checkable

  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
  ID_REGEX = /\A(?<identifier>[A-Z]{6})\z/x

  def initialize(id)
    parts = parse(id)
    @identifier = parts[:identifier]
  end
end

Direct Known Subclasses

CEI, CFI, CIK, CUSIP, FIGI, FISN, IBAN, ISIN, LEI, OCC, SEDOL, Valoren, WKN

Constant Summary

Constants included from Validatable

Validatable::ERROR_MAP

Constants included from Normalizable

Normalizable::SEPARATORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#errors, included, #valid?, #validate, #validate!

Methods included from Normalizable

included, #normalize!, #normalized, #to_pretty_s, #to_s, #to_str

Methods included from IdentifierMetadata

included

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



63
64
65
# File 'lib/sec_id/base.rb', line 63

def initialize(_sec_id_number)
  raise NotImplementedError
end

Instance Attribute Details

#full_idString (readonly)

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

Returns:

  • (String)

    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

#identifierString? (readonly)

Returns the main identifier portion (without check digit).

Returns:

  • (String, nil)

    the main identifier portion (without check digit)



50
51
52
# File 'lib/sec_id/base.rb', line 50

def identifier
  @identifier
end

Class Method Details

.inherited(subclass) ⇒ Object

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.



53
54
55
56
57
# File 'lib/sec_id/base.rb', line 53

def self.inherited(subclass)
  super
  # Skip anonymous classes and classes outside the SecID namespace (e.g. in tests)
  SecID.__send__(:register_identifier, subclass) if subclass.name&.start_with?('SecID::')
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

def ==(other)
  other.class == self.class && comparison_id == other.comparison_id
end

#as_jsonHash

Returns a JSON-compatible hash representation.

Returns:

  • (Hash)


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

def as_json(*)
  to_h
end

#hashInteger

Returns:

  • (Integer)


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

def hash
  [self.class, comparison_id].hash
end

#to_hHash

Returns a hash representation of this identifier for serialization.

Returns:

  • (Hash)

    hash with :type, :full_id, :normalized, :valid, and :components keys



83
84
85
86
87
88
89
90
91
# File 'lib/sec_id/base.rb', line 83

def to_h
  {
    type: self.class.short_name.downcase.to_sym,
    full_id: full_id,
    normalized: valid? ? normalized : nil,
    valid: valid?,
    components: components
  }
end