Class: SecID::Valoren

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

Overview

Note:

Valoren identifiers have no check digit and validation is based solely on format.

Valoren (Swiss Security Number) - a numeric identifier for securities in Switzerland, Liechtenstein, and Belgium.

Format: 5-9 numeric digits

Examples:

Validate a Valoren

SecID::Valoren.valid?('3886335')    #=> true
SecID::Valoren.valid?('003886335')  #=> true

Normalize a Valoren to 9 digits

SecID::Valoren.normalize('3886335')  #=> '003886335'

See Also:

Constant Summary collapse

FULL_NAME =

Human-readable name of the standard.

'Valoren Number'
ID_LENGTH =

Valid length(s) of a normalized identifier.

(5..9)
EXAMPLE =

A representative valid identifier.

'3886335'
VALID_CHARS_REGEX =

Pattern matching the identifier's permitted character set.

/\A[0-9]+\z/
ID_REGEX =

Regular expression for parsing Valoren components.

/\A
  (?=\d{5,9}\z)(?<padding>0*)(?<identifier>[1-9]\d{4,8})
\z/x
ISIN_COUNTRY_CODES =

Valid country codes for Valoren to ISIN conversion.

Set.new(%w[CH LI]).freeze

Constants included from Generatable

Generatable::ALPHA, Generatable::ALPHANUMERIC, Generatable::DIGITS

Constants included from Validatable

SecID::Validatable::ERROR_MAP

Constants included from Normalizable

Normalizable::SEPARATORS

Instance Attribute Summary collapse

Attributes inherited from Base

#full_id, #identifier

Instance Method Summary collapse

Methods inherited from Base

#==, #as_json, example, full_name, has_check_digit?, #hash, id_length, length_specificity, length_values, short_name, #to_h

Methods included from Validatable

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

Methods included from Normalizable

#to_str

Constructor Details

#initialize(valoren) ⇒ Valoren

Returns a new instance of Valoren.

Parameters:

  • valoren (String, Integer)

    the Valoren to parse



41
42
43
44
45
# File 'lib/sec_id/valoren.rb', line 41

def initialize(valoren)
  valoren_parts = parse(valoren)
  @padding = valoren_parts[:padding]
  @identifier = valoren_parts[:identifier]
end

Instance Attribute Details

#paddingString? (readonly)

Returns the leading zeros in the Valoren.

Returns:

  • (String, nil)

    the leading zeros in the Valoren



38
39
40
# File 'lib/sec_id/valoren.rb', line 38

def padding
  @padding
end

Instance Method Details

#normalize!self

Returns:

  • (self)

Raises:



74
75
76
77
78
# File 'lib/sec_id/valoren.rb', line 74

def normalize!
  super
  @padding = @full_id[0, self.class::ID_LENGTH.max - @identifier.length]
  self
end

#normalizedString

Returns the normalized 9-digit Valoren.

Returns:

  • (String)

    the normalized 9-digit Valoren

Raises:



67
68
69
70
# File 'lib/sec_id/valoren.rb', line 67

def normalized
  validate!
  @identifier.rjust(self.class::ID_LENGTH.max, '0')
end

#to_isin(country_code = 'CH') ⇒ ISIN

Returns a new ISIN instance with calculated check digit.

Parameters:

  • country_code (String) (defaults to: 'CH')

    the ISO 3166-1 alpha-2 country code (default: 'CH')

Returns:

  • (ISIN)

    a new ISIN instance with calculated check digit

Raises:



57
58
59
60
61
62
63
# File 'lib/sec_id/valoren.rb', line 57

def to_isin(country_code = 'CH')
  unless ISIN_COUNTRY_CODES.include?(country_code)
    raise InvalidFormatError, "'#{country_code}' is not a valid Valoren country code!"
  end

  ISIN.new(country_code + normalized).restore!
end

#to_pretty_sString?

Returns:

  • (String, nil)


48
49
50
51
52
# File 'lib/sec_id/valoren.rb', line 48

def to_pretty_s
  return nil unless valid?

  identifier.reverse.scan(/.{1,3}/).join(' ').reverse
end

#to_sString

Returns:

  • (String)


81
82
83
# File 'lib/sec_id/valoren.rb', line 81

def to_s
  full_id
end