Module: RASN2::Model::Accel
- Included in:
- RASN2::Model
- Defined in:
- lib/rasn2/model.rb
Overview
Define helper methods to define models
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
Instance Method Summary collapse
- #any(name, options = {}) ⇒ Elem
- #capture_content(&block) ⇒ Array<BaseElem,ModelElem,WrapElem>
-
#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
@rootclass 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.
-
#push_element(elem) ⇒ BaseElem, ...
Elem.
-
#root_options(options) ⇒ void
Update options of root element.
-
#type ⇒ String
Give type name (aka class name).
-
#wrapper(element = nil, options = {}, &block) ⇒ WrapElem
Use a Wrapper around a Types::Base or a RASN2::Model object.
Instance Attribute Details
#options ⇒ Hash (readonly)
137 138 139 |
# File 'lib/rasn2/model.rb', line 137 def @options end |
Instance Method Details
#any(name, options = {}) ⇒ Elem
292 293 294 295 296 |
# File 'lib/rasn2/model.rb', line 292 def any(name, ={}) [:name] = name proc = proc { |opts| Types::Any.new(.merge(opts)) } push_element(BaseElem.new(name, proc, nil)) end |
#capture_content(&block) ⇒ Array<BaseElem,ModelElem,WrapElem>
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rasn2/model.rb', line 185 def capture_content(&block) @content_builders ||= [] @content_builders << [] begin instance_eval(&block) ensure content = @content_builders.pop end content end |
#define_type_accel(accel_name, klass) ⇒ Object
Define an accelarator to access a type in a model definition
268 269 270 271 272 273 274 |
# File 'lib/rasn2/model.rb', line 268 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
233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rasn2/model.rb', line 233 def define_type_accel_base(accel_name, klass) singleton_class.class_eval <<-EVAL, __FILE__, __LINE__ + 1 def #{accel_name}(name, options={}, &block) # def sequence(name, type, options, &block) options[:name] = name options[:content] = merge_content(options[:content], capture_content(&block)) if block proc = proc do |opts| #{klass}.new(options.merge(opts)) # Sequence.new(options.merge(opts)) end push_element(BaseElem.new(name, proc, options[:content])) end EVAL end |
#define_type_accel_of(accel_name, klass) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/rasn2/model.rb', line 251 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 push_element(BaseElem.new(name, proc, nil)) end EVAL end |
#inherited(klass) ⇒ void
This method returns an undefined value.
On inheritance, create @root class variable
221 222 223 224 225 |
# File 'lib/rasn2/model.rb', line 221 def inherited(klass) super root = @root klass.class_eval { @root = root } end |
#model(name, model_klass) ⇒ Elem
Use another model in this model
143 144 145 |
# File 'lib/rasn2/model.rb', line 143 def model(name, model_klass) push_element(ModelElem.new(name, model_klass)) end |
#objectid(name, options = {}) ⇒ Elem
This method is named objectid and not object_id to not override
Object#object_id.
282 283 284 285 286 |
# File 'lib/rasn2/model.rb', line 282 def objectid(name, ={}) [:name] = name proc = proc { |opts| Types::ObjectId.new(.merge(opts)) } push_element(BaseElem.new(name, proc, nil)) end |
#parse(str, ber: false) ⇒ Model
Parse a DER/BER encoded string
311 312 313 314 315 |
# File 'lib/rasn2/model.rb', line 311 def parse(str, ber: false) model = new model.parse!(str, ber: ber) model end |
#push_element(elem) ⇒ BaseElem, ...
Returns elem.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/rasn2/model.rb', line 171 def push_element(elem) builder = @content_builders&.last if builder.nil? @root = elem else remove_consumed_elements(builder, elem) builder << elem end elem end |
#root_options(options) ⇒ void
This method returns an undefined value.
Update options of root element. May be used when subclassing. class Model1 < RASN2::Model sequence :seq, implicit: 0, content: [bool(:bool), integer(:int)] end
same as Model1 but with implicit tag set to 1
class Model2 < Model1 root_options implicit: 1 end
210 211 212 213 214 215 216 |
# File 'lib/rasn2/model.rb', line 210 def () @options = return unless .key?(:name) @root = @root.dup @root.name = [:name] end |
#type ⇒ String
Give type name (aka class name)
300 301 302 303 304 |
# File 'lib/rasn2/model.rb', line 300 def type return @type if defined? @type @type = self.to_s.gsub(/.*::/, '') end |
#wrapper(element, options = {}) ⇒ WrapElem #wrapper(options = {}, &block) ⇒ WrapElem
Use a Wrapper around a Types::Base or a RASN2::Model object
158 159 160 161 162 163 164 |
# File 'lib/rasn2/model.rb', line 158 def wrapper(element=nil, ={}, &block) if block = element if element.is_a?(Hash) element = single_element_from_block(:wrapper, &block) end push_element(WrapElem.new(element, )) end |