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

TAG =
0x02

Constants inherited from Primitive

Primitive::ASN1_PC

Constants inherited from Base

Base::CLASSES, Base::INDEFINITE_LENGTH, Base::MAX_TAG

Instance Attribute Summary collapse

Attributes inherited from Base

#asn1_class, #default, #name, #value

Instance Method Summary collapse

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 = {}) ⇒ 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

Instance Attribute Details

#enumHash? (readonly)

Returns:

  • (Hash, nil)


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

def enum
  @enum
end

Instance Method Details

#to_iInteger

Integer value

Returns:



77
78
79
80
81
82
83
# File 'lib/rasn1/types/integer.rb', line 77

def to_i
  if @enum.nil?
    @value || @default || 0
  else
    @enum[@value || @default] || 0
  end
end

#value=(v) ⇒ String, ...

Parameters:

Returns:

  • (String, Symbol, nil)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rasn1/types/integer.rb', line 51

def value=(v)
  case v
  when String,Symbol
    raise EnumeratedError, 'TAG #@name has no :enum' if @enum.nil?

    unless @enum.has_key? v
      raise EnumeratedError, "TAG #@name: unknwon enumerated value #{v}"
    end
    @value = v
  when ::Integer
    if @enum.nil?
      @value = v
    elsif @enum.has_value? v
      @value = @enum.key(v)
    else
      raise EnumeratedError, "TAG #@name: #{v} not in enumeration"
    end
  when nil
    @value = nil
  else
    raise EnumeratedError, "TAG #@name: not in enumeration"
  end
end