Class: RASN1::Types::Enumerated

Inherits:
Integer show all
Defined in:
lib/rasn1/types/enumerated.rb

Overview

ASN.1 Enumerated

An enumerated type permits to assign names to integer values. It may be defined different ways:

enum = RASN1::Types::Enumerated.new(enum: { 'a' => 0, 'b' => 1, 'c' => 2 })
enum = RASN1::Types::Enumerated.new(enum: { a: 0, b: 1, c: 2 })

Its value should be setting as an Integer or a String/symbol:

enum.value = :b
enum.value = 1   # equivalent to :b

But its value is always stored as named integer:

enum.value = :b
enum.value      # => :b
enum.value = 0
enum.value      # => :a

A EnumeratedError is raised when set value is not in enumeration.

Author:

  • Sylvain Daubert

Constant Summary collapse

TAG =

Enumerated tag value

0x0a

Constants inherited from Primitive

Primitive::ASN1_PC

Constants inherited from Base

Base::CLASSES, Base::CLASS_MASK, Base::INDEFINITE_LENGTH, Base::MAX_TAG, Base::UNDUPPABLE_TYPES

Instance Attribute Summary collapse

Attributes inherited from Base

#asn1_class, #default, #name, #value

Instance Method Summary collapse

Methods inherited from Integer

#to_i, #value=

Methods inherited from Base

#==, #constructed?, encode_type, #explicit?, #implicit?, #initialize_copy, #inspect, #optional?, parse, #parse!, #primitive?, #tag, #tagged?, #to_der, #type, type, #value_size

Constructor Details

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

Returns a new instance of Enumerated.

Overloads:

  • #initialize(options = {}) ⇒ Enumerated

    Options Hash (options):

    • :enum (Hash)

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

    Raises:

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

    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:

Raises:

See Also:



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

def initialize(value_or_options={}, options={})
  super
  raise EnumeratedError, 'no enumeration given' if @enum.nil?
end

Instance Attribute Details

#enumHash

Returns:

  • (Hash)


# File 'lib/rasn1/types/enumerated.rb', line 22

Instance Method Details

#to_hHash

Returns:

  • (Hash)


46
47
48
# File 'lib/rasn1/types/enumerated.rb', line 46

def to_h
  @enum
end