Class: SecId::ISIN

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

Overview

Constant Summary collapse

ID_REGEX =
/\A
  (?<identifier>
    (?<country_code>[A-Z]{2})
    (?<nsin>[A-Z0-9]{9}))
  (?<check_digit>\d)?
\z/x.freeze

Constants inherited from Base

Base::CHAR_TO_DIGIT, Base::CHAR_TO_DIGITS

Instance Attribute Summary collapse

Attributes inherited from Base

#check_digit, #full_number, #identifier

Instance Method Summary collapse

Methods inherited from Base

check_digit, restore!, #restore!, #to_s, valid?, #valid?, valid_format?, #valid_format?

Constructor Details

#initialize(isin) ⇒ ISIN

Returns a new instance of ISIN.



15
16
17
18
19
20
21
# File 'lib/sec_id/isin.rb', line 15

def initialize(isin)
  isin_parts = parse isin
  @identifier = isin_parts[:identifier]
  @country_code = isin_parts[:country_code]
  @nsin = isin_parts[:nsin]
  @check_digit = isin_parts[:check_digit]&.to_i
end

Instance Attribute Details

#country_codeObject (readonly)

Returns the value of attribute country_code.



13
14
15
# File 'lib/sec_id/isin.rb', line 13

def country_code
  @country_code
end

#nsinObject (readonly)

Returns the value of attribute nsin.



13
14
15
# File 'lib/sec_id/isin.rb', line 13

def nsin
  @nsin
end

Instance Method Details

#calculate_check_digitObject

Raises:



23
24
25
26
27
# File 'lib/sec_id/isin.rb', line 23

def calculate_check_digit
  return mod_10(luhn_sum) if valid_format?

  raise InvalidFormatError, "ISIN '#{full_number}' is invalid and check-digit cannot be calculated!"
end