Method: RASN1::Types::Integer#initialize

Defined in:
lib/rasn1/types/integer.rb

#initialize(options = {}) ⇒ Integer #initialize(value, options = {}) ⇒ Integer

Returns a new instance of Integer.

Overloads:

  • #initialize(options = {}) ⇒ Integer

    Options Hash (options):

    • :enum (Hash)

      enumeration hash. Keys are names, and values are integers.

    Raises:

    • (EnumeratedError)

      :default value is unknown when :enum key is present

  • #initialize(value, options = {}) ⇒ Integer

    Parameters:

    • value (Object)

      value to set for this ASN.1 object

    Options Hash (options):

    • :enum (Hash)

      enumeration hash. Keys are names, and values are integers. This key is mandatory.

    Raises:

    • (EnumeratedError)

      :default value is unknown when :enum key is present

See Also:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rasn1/types/integer.rb', line 22

def initialize(value_or_options={}, options={})
  super
  opts = value_or_options.is_a?(Hash) ? value_or_options : options
  @enum = opts[:enum]

  return if @enum.nil?

  # To ensure @value has the correct type
  self.value = @value

  case @default
  when String,Symbol
    unless @enum.has_key? @default
      raise EnumeratedError, "TAG #@name: unknwon enumerated default value #@default"
    end
  when ::Integer
    if @enum.has_value? @default
      @default = @enum.key(@default)
    else
      raise EnumeratedError, "TAG #@name: default value #@default not in enumeration"
    end
  when nil
  else
    raise TypeError, "TAG #@name: #{@default.class} not handled as default value"
  end
end