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

ID =

Enumerated id value

0x0a

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 Integer

#to_i, #value=, #void_value

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?, #value_size, #void_value

Constructor Details

#initialize(options = {}) ⇒ Enumerated

Returns a new instance of Enumerated.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :enum (Hash)

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

Raises:

See Also:



33
34
35
36
# File 'lib/rasn1/types/enumerated.rb', line 33

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

Instance Attribute Details

#enumHash

Returns:

  • (Hash)


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

Instance Method Details

#to_hHash

Returns:

  • (Hash)


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

def to_h
  @enum
end