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 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

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



66
67
68
# File 'lib/sec_id/base.rb', line 66

def initialize(_sec_id_number)
  raise NotImplementedError
end

Instance Attribute Details

#full_numberString (readonly)

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

Returns:

  • (String)

    the original input after normalization (stripped and uppercased)



43
44
45
# File 'lib/sec_id/base.rb', line 43

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)



46
47
48
# File 'lib/sec_id/base.rb', line 46

def identifier
  @identifier
end

Class Method Details

.valid?(id) ⇒ Boolean

Parameters:

  • id (String)

    the identifier to validate

Returns:

  • (Boolean)


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

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

.valid_format?(id) ⇒ Boolean

Parameters:

  • id (String)

    the identifier to check

Returns:

  • (Boolean)


57
58
59
# File 'lib/sec_id/base.rb', line 57

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

Instance Method Details

#to_sString Also known as: to_str

Returns:

  • (String)


83
84
85
# File 'lib/sec_id/base.rb', line 83

def to_s
  identifier.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  valid_format?
end

#valid_format?Boolean

Override in subclasses for additional format validation.

Returns:

  • (Boolean)


78
79
80
# File 'lib/sec_id/base.rb', line 78

def valid_format?
  !identifier.nil?
end