lib/rasn1/model.rb in rasn1-0.7.1 vs lib/rasn1/model.rb in rasn1-0.8.0

- old
+ new

@@ -202,20 +202,20 @@ # @param [Hash] options # @note This method is named +objectid+ and not +object_id+ to not override # +Object#object_id+. # @see Types::ObjectId#initialize def objectid(name, options={}) - options.merge!(name: name) + options[:name] = name proc = proc { |opts| Types::ObjectId.new(options.merge(opts)) } @root = [name, proc] end # @param [Symbol,String] name name of object in model # @param [Hash] options # @see Types::Any#initialize def any(name, options={}) - options.merge!(name: name) + options[:name] = name proc = proc { |opts| Types::Any.new(options.merge(opts)) } @root = [name, proc] end # Give type name (aka class name) @@ -367,11 +367,11 @@ # @return [Model, Types::Base] def by_name(name) elt = self[name] return elt unless elt.nil? - keys.each do |subelt_name| + @elements.each_key do |subelt_name| if self[subelt_name].is_a?(Model) elt = self[subelt_name][name] return elt unless elt.nil? end end @@ -406,43 +406,40 @@ return unless content.is_a? Array @elements[name].value = content.map do |name2, proc_or_class, content2| subel = get_type(proc_or_class) @elements[name2] = subel - if composed?(subel) && content2.is_a?(Array) - set_elements(name2, proc_or_class, content2) - end + set_elements(name2, proc_or_class, content2) if composed?(subel) && content2.is_a?(Array) subel end end def initialize_elements(obj, args) args.each do |name, value| - if obj[name] - if value.is_a? Hash - if obj[name].is_a? Model - initialize_elements obj[name], value - else - raise ArgumentError, "element #{name}: may only pass a Hash for Model elements" + next unless obj[name] + + case value + when Hash + raise ArgumentError, "element #{name}: may only pass a Hash for Model elements" unless obj[name].is_a? Model + + initialize_elements obj[name], value + when Array + composed = if obj[name].is_a? Model + obj[name].root + else + obj[name] + end + if composed.of_type.is_a? Model + value.each do |el| + composed << initialize_elements(composed.of_type.class.new, el) end - elsif value.is_a? Array - composed = if obj[name].is_a? Model - obj[name].root - else - obj[name] - end - if composed.of_type.is_a? Model - value.each do |el| - composed << initialize_elements(composed.of_type.class.new, el) - end - else - value.each do |el| - obj[name] << el - end - end else - obj[name].value = value + value.each do |el| + obj[name] << el + end end + else + obj[name].value = value end end end def private_to_h(element=nil)