class RASN1::Types::Enumerated

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

Constants

ID

Enumerated id value

Public Class Methods

new(value_or_options={}, options={}) click to toggle source

@overload initialize(options={})

@option options [Hash] :enum enumeration hash. Keys are names, and values
  are integers. This key is mandatory.
@raise [EnumeratedError] +:enum+ key is not present
@raise [EnumeratedError] +:default+ value is unknown

@overload initialize(value, options={})

@param [Object] value value to set for this ASN.1 object
@option options [Hash] :enum enumeration hash. Keys are names, and values
  are integers. This key is mandatory.
@raise [EnumeratedError] +:enum+ key is not present
@raise [EnumeratedError] +:default+ value is unknown

@see Base#initialize common options to all ASN.1 types

Calls superclass method
# File lib/rasn1/types/enumerated.rb, line 40
def initialize(value_or_options={}, options={})
  super
  raise EnumeratedError, 'no enumeration given' if @enum.nil?
end

Public Instance Methods

to_h() click to toggle source

@return [Hash]

# File lib/rasn1/types/enumerated.rb, line 46
def to_h
  @enum
end