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

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


137
138
139
# File 'lib/rasn2/model.rb', line 137

def options
  @options
end

Instance Method Details

#any(name, options = {}) ⇒ Elem

Parameters:

  • name (Symbol, String)

    name of object in model

  • options (Hash) (defaults to: {})

Returns:

  • (Elem)

See Also:

  • Types::Any#initialize


292
293
294
295
296
# File 'lib/rasn2/model.rb', line 292

def any(name, options={})
  options[:name] = name
  proc = proc { |opts| Types::Any.new(options.merge(opts)) }
  push_element(BaseElem.new(name, proc, nil))
end

#capture_content(&block) ⇒ Array<BaseElem,ModelElem,WrapElem>

Returns:

Since:

  • 0.17.0



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

Parameters:

  • accel_name (String)
  • klass (Class)

    class to instanciate

Since:

  • 0.11.0

  • 0.12.0 track source location on error (adfoster-r7)



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

Parameters:

  • accel_name (String, Symbol)
  • klass (Class)

Since:

  • 0.11.0

  • 0.12.0 track source location on error (adfoster-r7)

  • 0.17.0 accept a block to define content



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

Parameters:

  • accel_name (String, Symbol)
  • klass (Class)

Since:

  • 0.11.0

  • 0.12.0 track source location on error (adfoster-r7)



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

Parameters:

  • klass (Class)


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

Parameters:

  • name (String, Symbol)
  • model_klass (Class)

Returns:

  • (Elem)


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

Note:

This method is named objectid and not object_id to not override Object#object_id.

Parameters:

  • name (Symbol, String)

    name of object in model

  • options (Hash) (defaults to: {})

Returns:

  • (Elem)

See Also:

  • Types::ObjectId#initialize


282
283
284
285
286
# File 'lib/rasn2/model.rb', line 282

def objectid(name, options={})
  options[:name] = name
  proc = proc { |opts| Types::ObjectId.new(options.merge(opts)) }
  push_element(BaseElem.new(name, proc, nil))
end

#parse(str, ber: false) ⇒ Model

Parse a DER/BER encoded string

Parameters:

  • str (String)
  • ber (Boolean) (defaults to: false)

    accept BER encoding or not

Returns:

Raises:



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.

Parameters:

Returns:

Since:

  • 0.17.0



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

Parameters:

  • options (Hash)

Since:

  • 0.12.0 may change name through :name



210
211
212
213
214
215
216
# File 'lib/rasn2/model.rb', line 210

def root_options(options)
  @options = options
  return unless options.key?(:name)

  @root = @root.dup
  @root.name = options[:name]
end

#typeString

Give type name (aka class name)

Returns:

  • (String)


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

Overloads:

  • #wrapper(element, options = {}) ⇒ WrapElem

    Parameters:

  • #wrapper(options = {}, &block) ⇒ WrapElem

    Define the wrapped element through a block. The block must define exactly one element.

    Parameters:

    • options (Hash) (defaults to: {})

    Yield Returns:

    • (void)

Returns:

Since:

  • 0.12

  • 0.17.0 block form



158
159
160
161
162
163
164
# File 'lib/rasn2/model.rb', line 158

def wrapper(element=nil, options={}, &block)
  if block
    options = element if element.is_a?(Hash)
    element = single_element_from_block(:wrapper, &block)
  end
  push_element(WrapElem.new(element, options))
end