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

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


107
108
109
# File 'lib/rasn1/model.rb', line 107

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


220
221
222
223
224
# File 'lib/rasn1/model.rb', line 220

def any(name, options={})
  options[:name] = name
  proc = proc { |opts| Types::Any.new(options.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

Parameters:

  • accel_name (String)
  • klass (Class)

    class to instanciate

Since:

  • 0.11.0

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



196
197
198
199
200
201
202
# File 'lib/rasn1/model.rb', line 196

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)



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rasn1/model.rb', line 162

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

Parameters:

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

Since:

  • 0.11.0

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



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rasn1/model.rb', line 179

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

Parameters:

  • klass (Class)


151
152
153
154
155
# File 'lib/rasn1/model.rb', line 151

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)


113
114
115
# File 'lib/rasn1/model.rb', line 113

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.

Parameters:

  • name (Symbol, String)

    name of object in model

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

Returns:

  • (Elem)

See Also:

  • Types::ObjectId#initialize


210
211
212
213
214
# File 'lib/rasn1/model.rb', line 210

def objectid(name, options={})
  options[:name] = name
  proc = proc { |opts| Types::ObjectId.new(options.merge(opts)) }
  @root = 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:



239
240
241
242
243
# File 'lib/rasn1/model.rb', line 239

def parse(str, ber: false)
  model = new
  model.parse!(str, ber: ber)
  model
end

#root_options(options) ⇒ void

This method returns an undefined value.

Update options of root element. May be used when subclassing.

class Model1 < RASN1::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



140
141
142
143
144
145
146
# File 'lib/rasn1/model.rb', line 140

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)


228
229
230
231
232
# File 'lib/rasn1/model.rb', line 228

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

Parameters:

Returns:

Since:

  • 0.12



122
123
124
# File 'lib/rasn1/model.rb', line 122

def wrapper(element, options={})
  @root = WrapElem.new(element, options)
end