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, #identifier

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(isin) ⇒ ISIN

Returns a new instance of ISIN.



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

def initialize(isin)
  @isin = isin.to_s.strip.upcase
  isin_parts = @isin.match(ID_REGEX) || {}

  @identifier = isin_parts[:identifier]
  @country_code = isin_parts[:country_code]
  @nsin = isin_parts[:nsin]
  @check_digit = isin_parts[:check_digit].to_i if isin_parts[:check_digit]
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

#isinObject (readonly)

Returns the value of attribute isin.



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

def isin
  @isin
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:



40
41
42
43
44
# File 'lib/sec_id/isin.rb', line 40

def calculate_check_digit
  return mod_10(luhn_sum) if valid_format?

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

#restore!Object



35
36
37
38
# File 'lib/sec_id/isin.rb', line 35

def restore!
  @check_digit = calculate_check_digit
  @isin = to_s
end

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/sec_id/isin.rb', line 25

def valid?
  return false unless valid_format?

  check_digit == calculate_check_digit
end

#valid_format?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sec_id/isin.rb', line 31

def valid_format?
  identifier ? true : false
end