Class: RASN1::Wrapper
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- RASN1::Wrapper
- Defined in:
- lib/rasn1/wrapper.rb
Overview
This class is used to wrap a Types::Base or Model instance to force its options.
Usage
This class may be used to wrap another RASN1 object by 3 ways:
-
wrap an object to modify its options,
-
implicitly wrap an object (i.e. change its tag),
-
explicitly wrap an object (i.e wrap the object in another explicit ASN.1 tag)
Defined Under Namespace
Classes: ExplicitWrapper
Instance Method Summary collapse
- #asn1_class ⇒ Symbol
- #constructed? ⇒ Boolean
-
#element ⇒ Types::Base, Model
Return Wrapped element.
-
#explicit? ⇒ Boolean
Say if wrapper is an explicit one (i.e. add tag and length to its element).
- #id ⇒ ::Integer
-
#implicit? ⇒ Boolean
Say if wrapper is an implicit one (i.e. change tag of its element).
-
#initialize(element, options = {}) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #inspect(level = 0) ⇒ String
-
#parse!(der, ber: false) ⇒ Integer
Parse a DER string.
- #primitive? ⇒ Boolean
-
#to_der ⇒ String
Convert wrapper and its element to a DER string.
- #value? ⇒ Boolean
Constructor Details
#initialize(element, options = {}) ⇒ Wrapper
Returns a new instance of Wrapper.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rasn1/wrapper.rb', line 56 def initialize(element, ={}) opts = explicit_implicit() if explicit? generate_explicit_wrapper(opts) element. = element..merge((opts)) @options = opts else opts[:value] = element.value element. = element..merge(opts) @options = {} end raise RASN1::Error, 'Cannot be implicit and explicit' if explicit? && implicit? super(element) end |
Instance Method Details
#asn1_class ⇒ Symbol
149 150 151 152 153 |
# File 'lib/rasn1/wrapper.rb', line 149 def asn1_class return element.asn1_class unless @options.key?(:class) @options[:class] end |
#constructed? ⇒ Boolean
157 158 159 160 161 |
# File 'lib/rasn1/wrapper.rb', line 157 def constructed? return element.constructed? unless @options.key?(:constructed) @options[:constructed] end |
#element ⇒ Types::Base, Model
Return Wrapped element
131 132 133 |
# File 'lib/rasn1/wrapper.rb', line 131 def element __getobj__ end |
#explicit? ⇒ Boolean
Say if wrapper is an explicit one (i.e. add tag and length to its element)
75 76 77 |
# File 'lib/rasn1/wrapper.rb', line 75 def explicit? !!@explicit end |
#id ⇒ ::Integer
137 138 139 140 141 142 143 144 145 |
# File 'lib/rasn1/wrapper.rb', line 137 def id if implicit? @implicit elsif explicit? @explicit else element.id end end |
#implicit? ⇒ Boolean
Say if wrapper is an implicit one (i.e. change tag of its element)
81 82 83 |
# File 'lib/rasn1/wrapper.rb', line 81 def implicit? !!@implicit end |
#inspect(level = 0) ⇒ String
171 172 173 174 175 |
# File 'lib/rasn1/wrapper.rb', line 171 def inspect(level=0) return super(level) unless explicit? @explicit_wrapper.inspect(level) << ' ' << super(level) end |
#parse!(der, ber: false) ⇒ Integer
Parse a DER string. This method updates object.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rasn1/wrapper.rb', line 104 def parse!(der, ber: false) if implicit? el = generate_implicit_element parsed = el.parse!(der, ber: ber) element.value = el.value parsed elsif explicit? parsed = @explicit_wrapper.parse!(der, ber: ber) element.parse!(@explicit_wrapper.value, ber: ber) if parsed.positive? parsed else element.parse!(der, ber: ber) end end |
#primitive? ⇒ Boolean
165 166 167 |
# File 'lib/rasn1/wrapper.rb', line 165 def primitive? !constructed? end |
#to_der ⇒ String
Convert wrapper and its element to a DER string
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rasn1/wrapper.rb', line 87 def to_der if implicit? el = generate_implicit_element el.to_der elsif explicit? @explicit_wrapper.value = element @explicit_wrapper.to_der else element.to_der end end |
#value? ⇒ Boolean
121 122 123 124 125 126 127 |
# File 'lib/rasn1/wrapper.rb', line 121 def value? if explicit? @explicit_wrapper.value? else __getobj__.value? end end |