lib/rasn1/model.rb in rasn1-0.13.0 vs lib/rasn1/model.rb in rasn1-0.13.1
- old
+ new
@@ -66,11 +66,11 @@
class Model # rubocop:disable Metrics/ClassLength
# @private
Elem = Struct.new(:name, :proc_or_class, :content) do
# @param [String,Symbol] name
# @param [Proc,Class] proc_or_class
- # @param [Hash,nil] content
+ # @param [Array,nil] content
def initialize(name, proc_or_class, content)
if content.is_a?(Array)
duplicate_names = find_all_duplicate_names(content.map(&:name) + [name])
raise ModelValidationError, "Duplicate name #{duplicate_names.first} found" if duplicate_names.any?
end
@@ -276,11 +276,11 @@
# @param [Model, Types::Base] type type for SET OF
# @param [Hash] options
# @return [Elem]
# @see Types::SetOf#initialize
%w[sequence set].each do |type|
- define_type_accel_of(type, Types.const_get("#{type.capitalize}Of"))
+ define_type_accel_of(type, Types.const_get(:"#{type.capitalize}Of"))
end
# @!method boolean(name, options)
# @!scope class
# @param [Symbol,String] name name of object in model
@@ -459,10 +459,11 @@
def value(name=nil, *args)
if name.nil?
root.value
else
elt = by_name(name)
+ return nil if elt.nil?
unless args.empty?
args.each do |arg|
elt = elt.root if elt.is_a?(Model)
elt = elt[arg]
@@ -502,18 +503,19 @@
protected
# Give a (nested) element from its name
# @param [String, Symbol] name
- # @return [Model, Types::Base]
+ # @return [Model, Types::Base, nil]
def by_name(name)
elt = self[name]
return elt unless elt.nil?
@elements.each_key do |subelt_name|
- if self[subelt_name].is_a?(Model)
- elt = self[subelt_name][name]
+ subelt = self[subelt_name]
+ if subelt.is_a?(Model)
+ elt = subelt[name]
return elt unless elt.nil?
end
end
nil
@@ -539,11 +541,16 @@
def generate_root
class_element = self.class.class_eval { @root }
@root_name = class_element.name
@elements = {}
- @elements[@root_name] = get_type(class_element.proc_or_class, self.class.options || {})
+ @elements[@root_name] = case class_element
+ when WrapElem
+ generate_wrapper(class_element)
+ else
+ get_type(class_element.proc_or_class, self.class.options || {})
+ end
class_element
end
def generate_elements(element)
if element.is_a?(WrapElem)
@@ -575,21 +582,22 @@
end
end
def initialize_elements(obj, args)
args.each do |name, value|
- next unless obj[name]
+ subobj = obj[name]
+ next unless subobj
case value
when Hash
- raise ArgumentError, "element #{name}: may only pass a Hash for Model elements" unless obj[name].is_a?(Model)
+ raise ArgumentError, "element #{name}: may only pass a Hash for Model elements" unless subobj.is_a?(Model)
- initialize_elements obj[name], value
+ initialize_elements(subobj, value)
when Array
- initialize_element_from_array(obj[name], value)
+ initialize_element_from_array(subobj, value)
else
- obj[name].value = value
+ subobj.value = value
end
end
end
def initialize_element_from_array(obj, value)
@@ -654,15 +662,16 @@
ary.compact!
ary.to_h
end
def wrapper_to_h(wrap)
- case wrap.element
+ el = wrap.element
+ case el
when Model
- hsh = wrap.element.to_h
+ hsh = el.to_h
hsh[hsh.keys.first]
else
- private_to_h(wrap.element)
+ private_to_h(el)
end
end
end
end