lib/rasn1/model.rb in rasn1-0.2.0 vs lib/rasn1/model.rb in rasn1-0.3.0
- old
+ new
@@ -47,10 +47,17 @@
# record2[:rented] = true
# record2[:a_record][:id] = 65537
# record2[:a_record][:room] = 43
# or like this:
# record2 = Record2.new(rented: true, a_record: { id: 65537, room: 43 })
+ #
+ # == Delegation
+ # {Model} may delegate some methods to its root element. Thus, if root element
+ # is, for example, a {Types::Choice}, model may delegate +#chosen+ and +#chosen_value+.
+ #
+ # All methods defined by root may be delegated by model, unless model also defines
+ # this method.
# @author Sylvain Daubert
class Model
class << self
@@ -126,10 +133,17 @@
def any(name, options={})
proc = Proc.new { Types::Any.new(name, options) }
@root = [name, proc]
end
+ # Give type name (aka class name)
+ # @return [String]
+ def type
+ return @type if @type
+ @type = self.to_s.gsub(/.*::/, '')
+ end
+
# Parse a DER/BER encoded string
# @param [String] str
# @param [Boolean] ber accept BER encoding or not
# @return [Model]
def parse(str, ber: false)
@@ -152,10 +166,19 @@
# @return [Types::Base]
def [](name)
@elements[name]
end
+ # Set value of element +name+. Element should be a {Base}.
+ # @param [String,Symbol] name
+ # @param [Object] value
+ # @return [Object] value
+ def []=(name, value)
+ raise Error, "cannot set value for a Model" if @elements[name].is_a? Model
+ @elements[name].value = value
+ end
+
# Get name frm root type
# @return [String,Symbol]
def name
@elements[@root].name
end
@@ -181,15 +204,31 @@
# @return [Types::Base,Model]
def root
@elements[@root]
end
+ # Give type name (aka class name)
+ # @return [String]
+ def type
+ self.class.type
+ end
+
# Parse a DER/BER encoded string, and modify object in-place.
# @param [String] str
# @param [Boolean] ber accept BER encoding or not
# @return [Integer] number of parsed bytes
def parse!(str, ber: false)
@elements[@root].parse!(str.dup.force_encoding('BINARY'), ber: ber)
+ end
+
+ # Delegate some methods to root element
+ # @param [Symbol] meth
+ def method_missing(meth, *args)
+ if @elements[@root].respond_to? meth
+ @elements[@root].send meth, *args
+ else
+ super
+ end
end
private
def is_composed?(el)