Module: RASN1::Model::Accel

Included in:
RASN1::Model
Defined in:
lib/rasn1/model/accel.rb

Overview

Define helper methods to define models

Author:

  • Sylvain Daubert

Since:

  • 0.17 block notation

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)

Since:

  • 0.17 block notation



15
16
17
# File 'lib/rasn1/model/accel.rb', line 15

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

Since:

  • 0.17 block notation



203
204
205
206
207
# File 'lib/rasn1/model/accel.rb', line 203

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



177
178
179
180
181
182
183
184
185
# File 'lib/rasn1/model/accel.rb', line 177

def define_type_accel(accel_name, klass)
  if klass <= Types::SequenceOf
    define_type_accel_of(accel_name, klass)
  elsif (klass <= Types::Sequence) || (klass <= Types::Choice)
    define_type_accel_container(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)



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rasn1/model/accel.rb', line 112

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
      push_element(BaseElem.new(name, proc, nil))
    end
  EVAL
end

#define_type_accel_container(accel_name, klass) ⇒ Object

Parameters:

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

Since:

  • 0.17.0



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rasn1/model/accel.rb', line 128

def define_type_accel_container(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
      proc = proc do |opts|
        #{klass}.new(options.merge(opts)) # Sequence.new(options.merge(opts))
      end
      options[:content] ||= []
      options[:content] += generate_content(&block)
      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)



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

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

#elementsObject

Since:

  • 0.17.0



27
28
29
30
31
# File 'lib/rasn1/model/accel.rb', line 27

def elements
  return @elements unless @elements.nil?

  @elements = []
end

#generate_contentObject

Since:

  • 0.17.0



144
145
146
147
148
149
150
151
152
153
# File 'lib/rasn1/model/accel.rb', line 144

def generate_content
  return [] unless block_given?

  previous_elements = @elements
  @elements = []
  yield
  content = @elements
  @elements = previous_elements
  content
end

#inherited(klass) ⇒ void

This method returns an undefined value.

On inheritance, create @elements class variable

Parameters:

  • klass (Class)

Since:

  • 0.17 block notation



20
21
22
23
24
# File 'lib/rasn1/model/accel.rb', line 20

def inherited(klass)
  super
  current_elements = @elements
  klass.class_eval { @elements = current_elements.dup }
end

#model(name, model_klass) ⇒ Elem

Use another model in this model

Parameters:

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

Returns:

  • (Elem)

Since:

  • 0.17 block notation



37
38
39
# File 'lib/rasn1/model/accel.rb', line 37

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

Since:

  • 0.17 block notation



193
194
195
196
197
# File 'lib/rasn1/model/accel.rb', line 193

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:

Since:

  • 0.17 block notation



222
223
224
225
226
# File 'lib/rasn1/model/accel.rb', line 222

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



44
45
46
47
# File 'lib/rasn1/model/accel.rb', line 44

def push_element(elem)
  elements << elem
  elem
end

#rootObject

Since:

  • 0.17.0



50
51
52
# File 'lib/rasn1/model/accel.rb', line 50

def root
  elements.last
end

#root=(elem) ⇒ Object

Since:

  • 0.17.0



55
56
57
58
59
60
61
# File 'lib/rasn1/model/accel.rb', line 55

def root=(elem)
  if elements.empty?
    elements << elem
  else
    elements[-1] = elem
  end
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



99
100
101
102
103
104
105
# File 'lib/rasn1/model/accel.rb', line 99

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

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

#typeString

Give type name (aka class name)

Returns:

  • (String)

Since:

  • 0.17 block notation



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

def type
  return @type if defined? @type

  @type = self.to_s.gsub(/.*::/, '')
end

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

@overload(options={}) @param options [Hash] @return [WrapElem] @yieldreturn [BaseElem, ModelElem] element to wrapp @since 0.17.0

Use a Wrapper around a Types::Base or a RASN1::Model object

Parameters:

Returns:

Since:

  • 0.12

Since:

  • 0.17 block notation



74
75
76
77
78
79
80
81
82
83
# File 'lib/rasn1/model/accel.rb', line 74

def wrapper(element_or_options={}, options={}, &)
  case element_or_options
  when Hash
    element = generate_content(&).last
    options = element_or_options
  else
    element = element_or_options
  end
  push_element(WrapElem.new(element, options))
end