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 4 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),
-
wrap a choice model to reuse it in its own definition.
Defined Under Namespace
Classes: ExplicitWrapper
Instance Method Summary collapse
- #asn1_class ⇒ Symbol
- #constructed? ⇒ Boolean
- #do_parse(der, ber: false) ⇒ Object
-
#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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rasn1/wrapper.rb', line 68 def initialize(element, ={}) @lazy = true opts = explicit_implicit() if explicit? generate_explicit_wrapper(opts) @element_options_to_merge = (opts) @options = opts else opts[:value] = element.value if element.respond_to?(:value) @element_options_to_merge = opts @options = {} end raise RASN1::Error, 'Cannot be implicit and explicit' if explicit? && implicit? super(element) end |
Instance Method Details
#asn1_class ⇒ Symbol
184 185 186 187 188 |
# File 'lib/rasn1/wrapper.rb', line 184 def asn1_class return lazy_generation(register: false).asn1_class unless @options.key?(:class) @options[:class] end |
#constructed? ⇒ Boolean
192 193 194 195 196 |
# File 'lib/rasn1/wrapper.rb', line 192 def constructed? return lazy_generation(register: false).constructed? unless @options.key?(:constructed) @options[:constructed] end |
#do_parse(der, ber: false) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/rasn1/wrapper.rb', line 140 def do_parse(der, ber: false) if implicit? generate_implicit_element(Types::Base.new(constructed: element.constructed?)).do_parse(der, ber: ber) elsif explicit? @explicit_wrapper.do_parse(der, ber: ber) else element.do_parse(der, ber: ber) end end |
#element ⇒ Types::Base, Model
Return Wrapped element
166 167 168 |
# File 'lib/rasn1/wrapper.rb', line 166 def element lazy_generation end |
#explicit? ⇒ Boolean
Say if wrapper is an explicit one (i.e. add tag and length to its element)
88 89 90 |
# File 'lib/rasn1/wrapper.rb', line 88 def explicit? !!@explicit end |
#id ⇒ ::Integer
172 173 174 175 176 177 178 179 180 |
# File 'lib/rasn1/wrapper.rb', line 172 def id if implicit? @implicit elsif explicit? @explicit else lazy_generation(register: false).id end end |
#implicit? ⇒ Boolean
Say if wrapper is an implicit one (i.e. change tag of its element)
94 95 96 |
# File 'lib/rasn1/wrapper.rb', line 94 def implicit? !!@implicit end |
#inspect(level = 0) ⇒ String
206 207 208 209 210 |
# File 'lib/rasn1/wrapper.rb', line 206 def inspect(level=0) return super() unless explicit? @explicit_wrapper.inspect(level) << ' ' << super() end |
#parse!(der, ber: false) ⇒ Integer
Parse a DER string. This method updates object.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rasn1/wrapper.rb', line 122 def parse!(der, ber: false) lazy_generation 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
200 201 202 |
# File 'lib/rasn1/wrapper.rb', line 200 def primitive? !constructed? end |
#to_der ⇒ String
Convert wrapper and its element to a DER string
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rasn1/wrapper.rb', line 102 def to_der lazy_generation 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
152 153 154 155 156 157 158 159 160 |
# File 'lib/rasn1/wrapper.rb', line 152 def value? if explicit? @explicit_wrapper.value? elsif __getobj__.is_a?(Class) false else __getobj__.value? end end |