Class: RASN1::Types::Integer

Inherits:
Primitive show all
Defined in:
lib/rasn1/types/integer.rb

Overview

ASN.1 Integer

Author:

  • Sylvain Daubert

Direct Known Subclasses

Enumerated

Constant Summary collapse

ID =

Integer id value

2

Constants inherited from Primitive

Primitive::ASN1_PC

Constants inherited from Base

Base::CLASSES, Base::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MULTI_OCTETS_ID

Instance Attribute Summary collapse

Attributes inherited from Base

#asn1_class, #default, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#==, #can_build?, constrained?, #constructed?, #do_parse_explicit_with_tracing, #do_parse_with_tracing, encoded_type, #explicit?, #id, #implicit?, #initialize_copy, #inspect, #optional?, parse, #parse!, #primitive?, #specific_initializer, start_tracing, stop_tracing, #tagged?, #to_der, #trace, type, #type, #value, #value?, #value_size

Constructor Details

#initialize(options = {}) ⇒ Integer

Returns a new instance of Integer.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

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

See Also:



18
19
20
21
# File 'lib/rasn1/types/integer.rb', line 18

def initialize(options={})
  super
  initialize_enum(@options[:enum])
end

Instance Attribute Details

#enumHash? (readonly)

Returns:

  • (Hash, nil)


9
10
11
# File 'lib/rasn1/types/integer.rb', line 9

def enum
  @enum
end

Instance Method Details

#to_iInteger

Integer value

Returns:



47
48
49
50
51
52
53
54
55
# File 'lib/rasn1/types/integer.rb', line 47

def to_i
  val = value? ? @value : @default
  case val
  when String, Symbol
    @enum[val]
  else
    val || 0
  end
end

#value=(val) ⇒ void

This method returns an undefined value.

Parameters:

  • val (Integer, String, Symbol, nil)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rasn1/types/integer.rb', line 25

def value=(val)
  @no_value = false
  case val
  when String, Symbol
    value_from_string_or_symbol(val)
  when ::Integer
    value_from_integer(val)
  when nil
    @no_value = true
    @value = void_value
  else
    raise EnumeratedError, "#{@name}: not in enumeration"
  end
end

#void_valueInteger

Returns:



41
42
43
# File 'lib/rasn1/types/integer.rb', line 41

def void_value
  0
end