Module: RASN1::Model::Accel
- Included in:
- RASN1::Model
- Defined in:
- lib/rasn1/model.rb
Overview
Define helper methods to define models
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
Instance Method Summary collapse
- #any(name, options = {}) ⇒ Elem
-
#define_type_accel(accel_name, klass) ⇒ Object
Define an accelarator to access a type in a model definition.
- #define_type_accel_base(accel_name, klass) ⇒ Object
- #define_type_accel_of(accel_name, klass) ⇒ Object
-
#inherited(klass) ⇒ void
On inheritance, create @root class variable.
-
#model(name, model_klass) ⇒ Elem
Use another model in this model.
- #objectid(name, options = {}) ⇒ Elem
-
#parse(str, ber: false) ⇒ Model
Parse a DER/BER encoded string.
-
#root_options(options) ⇒ void
Update options of root element.
-
#type ⇒ String
Give type name (aka class name).
-
#wrapper(element, options = {}) ⇒ WrapElem
Use a Wrapper around a Types::Base or a RASN1::Model object.
Instance Attribute Details
#options ⇒ Hash (readonly)
113 114 115 |
# File 'lib/rasn1/model.rb', line 113 def @options end |
Instance Method Details
#any(name, options = {}) ⇒ Elem
226 227 228 229 230 |
# File 'lib/rasn1/model.rb', line 226 def any(name, ={}) [:name] = name proc = proc { |opts| Types::Any.new(.merge(opts)) } @root = BaseElem.new(name, proc, nil) end |
#define_type_accel(accel_name, klass) ⇒ Object
Define an accelarator to access a type in a model definition
202 203 204 205 206 207 208 |
# File 'lib/rasn1/model.rb', line 202 def define_type_accel(accel_name, klass) if klass < Types::SequenceOf define_type_accel_of(accel_name, klass) else define_type_accel_base(accel_name, klass) end end |
#define_type_accel_base(accel_name, klass) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rasn1/model.rb', line 168 def define_type_accel_base(accel_name, klass) singleton_class.class_eval <<-EVAL, __FILE__, __LINE__ + 1 def #{accel_name}(name, options={}) # def sequence(name, type, options) options[:name] = name proc = proc do |opts| #{klass}.new(options.merge(opts)) # Sequence.new(options.merge(opts)) end @root = BaseElem.new(name, proc, options[:content]) end EVAL end |
#define_type_accel_of(accel_name, klass) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rasn1/model.rb', line 185 def define_type_accel_of(accel_name, klass) singleton_class.class_eval <<-EVAL, __FILE__, __LINE__ + 1 def #{accel_name}_of(name, type, options={}) # def sequence_of(name, type, options) options[:name] = name proc = proc do |opts| #{klass}.new(type, options.merge(opts)) # SequenceOf.new(type, options.merge(opts)) end @root = BaseElem.new(name, proc, nil) end EVAL end |
#inherited(klass) ⇒ void
This method returns an undefined value.
On inheritance, create @root class variable
157 158 159 160 161 |
# File 'lib/rasn1/model.rb', line 157 def inherited(klass) super root = @root klass.class_eval { @root = root } end |
#model(name, model_klass) ⇒ Elem
Use another model in this model
119 120 121 |
# File 'lib/rasn1/model.rb', line 119 def model(name, model_klass) @root = ModelElem.new(name, model_klass) end |
#objectid(name, options = {}) ⇒ Elem
Note:
This method is named objectid and not object_id to not override Object#object_id.
216 217 218 219 220 |
# File 'lib/rasn1/model.rb', line 216 def objectid(name, ={}) [:name] = name proc = proc { |opts| Types::ObjectId.new(.merge(opts)) } @root = BaseElem.new(name, proc, nil) end |
#parse(str, ber: false) ⇒ Model
Parse a DER/BER encoded string
245 246 247 248 249 |
# File 'lib/rasn1/model.rb', line 245 def parse(str, ber: false) model = new model.parse!(str, ber: ber) model end |
#root_options(options) ⇒ void
146 147 148 149 150 151 152 |
# File 'lib/rasn1/model.rb', line 146 def () @options = return unless .key?(:name) @root = @root.dup @root.name = [:name] end |
#type ⇒ String
Give type name (aka class name)
234 235 236 237 238 |
# File 'lib/rasn1/model.rb', line 234 def type return @type if defined? @type @type = self.to_s.gsub(/.*::/, '') end |
#wrapper(element, options = {}) ⇒ WrapElem
Use a Wrapper around a Types::Base or a RASN1::Model object
128 129 130 |
# File 'lib/rasn1/model.rb', line 128 def wrapper(element, ={}) @root = WrapElem.new(element, ) end |