Class: RASN1::Types::SequenceOf
- Inherits:
-
Constructed
- Object
- Base
- Constructed
- RASN1::Types::SequenceOf
- Defined in:
- lib/rasn1/types/sequence_of.rb
Overview
ASN.1 SEQUENCE OF
A SEQUENCE OF is an array of one ASN.1 type.
Use with Primitive types
To encode this ASN.1 example:
Integers ::= SEQUENCE OF INTEGER
do:
# Create a SEQUENCE OF INTEGER
seqof = RASN1::Types::SequenceOf.new(RASN1::Types::Integer)
# Set integer values
seqof.value = [1, 2, 3, 4]
Use with Constructed types
SEQUENCE OF may be used to create sequence of composed types. For example:
composed_type = RASN1::Types::Sequence.new
commposed_type.value = [RASN1::Types::Integer,
RASN1::Types::OctetString]
seqof = RASN1::Types::SequenceOf.new(composed_type)
seqof << [0, 'data0']
seqof << [1, 'data1']
Use with Model
SEQUENCE OF may also be used with a Model type:
class MyModel < RASN1::Model
sequence :seq,
content: [boolean(:bool), integer(:int)]
end
seqof = RASN1::Types::SequenceOf.new(:record, MyModel)
# set values
seqof << { bool: true, int: 12 }
seqof << { bool: false, int: 65535 }
# Generate DER string
der = seqof.to_der # => String
# parse
seqof.parse! der
After parsing, a SEQUENCE OF may be accessed as an Array:
seqof[0] # => MyModel
seqof[0][:bool].value # => true
seqof[0][:int].value # => 12
Direct Known Subclasses
Constant Summary collapse
Constants inherited from Constructed
Constants inherited from Base
Base::CLASSES, Base::INDEFINITE_LENGTH, Base::MAX_TAG
Instance Attribute Summary collapse
- #of_type ⇒ Class, Base readonly
Attributes inherited from Base
#asn1_class, #default, #name, #value
Class Method Summary collapse
-
.encoded_type ⇒ 'SEQUENCE'
A SEQUENCE OF is encoded as a SEQUENCE.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
Add an item to SEQUENCE OF.
-
#[](idx) ⇒ Base
Get element of index
idx
. -
#initialize(of_type, options = {}) ⇒ SequenceOf
constructor
A new instance of SequenceOf.
- #initialize_copy(other) ⇒ Object
- #inspect(level = 0) ⇒ Object
-
#length ⇒ Integer
Get length of SEQUENCE OF (ie number of elements).
Methods inherited from Base
#==, #constructed?, encode_type, #explicit?, #implicit?, #optional?, parse, #parse!, #primitive?, #tag, #tagged?, #to_der, type, #type, #value_size
Constructor Details
#initialize(of_type, options = {}) ⇒ SequenceOf
Returns a new instance of SequenceOf.
62 63 64 65 66 |
# File 'lib/rasn1/types/sequence_of.rb', line 62 def initialize(of_type, ={}) super() @of_type = of_type @value = [] end |
Instance Attribute Details
#of_type ⇒ Class, Base (readonly)
51 52 53 |
# File 'lib/rasn1/types/sequence_of.rb', line 51 def of_type @of_type end |
Class Method Details
.encoded_type ⇒ 'SEQUENCE'
A SEQUENCE OF is encoded as a SEQUENCE.
55 56 57 |
# File 'lib/rasn1/types/sequence_of.rb', line 55 def self.encoded_type Sequence.encoded_type end |
Instance Method Details
#<<(obj) ⇒ Object
Add an item to SEQUENCE OF
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rasn1/types/sequence_of.rb', line 76 def <<(obj) if of_type_class < Primitive raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array) @value += obj.map { |item| @of_type.new(item) } elsif composed_of_type? raise ASN1Error, 'object to add should be an Array' unless obj.is_a?(Array) new_value = of_type_class.new @of_type.value.each_with_index do |type, i| type2 = type.dup type2.value = obj[i] new_value.value << type2 end @value << new_value elsif of_type_class < Model case obj when Hash @value << @of_type.new(obj) when of_type_class @value << obj else raise ASN1Error, "object to add should be a #{of_type_class} or a Hash" end end end |
#[](idx) ⇒ Base
Get element of index idx
104 105 106 |
# File 'lib/rasn1/types/sequence_of.rb', line 104 def [](idx) @value[idx] end |
#initialize_copy(other) ⇒ Object
68 69 70 71 72 |
# File 'lib/rasn1/types/sequence_of.rb', line 68 def initialize_copy(other) super @of_type = @of_type.dup @value = @value.map { |v| v.dup } end |
#inspect(level = 0) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rasn1/types/sequence_of.rb', line 114 def inspect(level=0) str = '' str << ' ' * level if level > 0 str << "#{@name} " unless @name.nil? level = level.abs str << "#{type}:\n" level += 1 @value.each do |item| case item when Base, Model next if item.optional? and item.value.nil? str << "#{item.inspect(level)}" str << "\n" unless str.end_with?("\n") else str << ' ' * level + "#{item.inspect}\n" end end str end |
#length ⇒ Integer
Get length of SEQUENCE OF (ie number of elements)
110 111 112 |
# File 'lib/rasn1/types/sequence_of.rb', line 110 def length @value.length end |