lib/rasn1/model.rb in rasn1-0.3.0 vs lib/rasn1/model.rb in rasn1-0.3.1

- old
+ new

@@ -66,20 +66,46 @@ # @param [Class] model_klass def model(name, model_klass) @root = [name, model_klass] end + # 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 + # @param [Hash] options + # @return [void] + def root_options(options) + @options = options + end + + # On inheritance, create +@root+ class variable + # @param [Class] klass + # @return [void] + def inherited(klass) + super + root = @root + klass.class_eval { @root = root } + end + # @method sequence(name, options) # @see Types::Sequence#initialize # @method set(name, options) # @see Types::Set#initialize # @method choice(name, options) # @see Types::Choice#initialize %w(sequence set choice).each do |type| class_eval "def #{type}(name, options={})\n" \ - " proc = Proc.new do\n" \ - " Types::#{type.capitalize}.new(name, options)\n" \ + " proc = Proc.new do |opts|\n" \ + " Types::#{type.capitalize}.new(name, options.merge(opts))\n" \ " end\n" \ " @root = [name, proc]\n" \ " @root << options[:content] unless options[:content].nil?\n" \ " @root\n" \ "end" @@ -90,12 +116,12 @@ # @method set_of(name, type, options) # @see Types::SetOf#initialize %w(sequence set).each do |type| klass_name = "Types::#{type.capitalize}Of" class_eval "def #{type}_of(name, type, options={})\n" \ - " proc = Proc.new do\n" \ - " #{klass_name}.new(name, type, options)\n" \ + " proc = Proc.new do |opts|\n" \ + " #{klass_name}.new(name, type, options.merge(opts))\n" \ " end\n" \ " @root = [name, proc]\n" \ "end" end @@ -114,26 +140,28 @@ # @method utf8_string(name, options) # @see Types::Utf8String#initialize Types.primitives.each do |prim| next if prim == Types::ObjectId class_eval "def #{prim.type.downcase.gsub(/\s+/, '_')}(name, options={})\n" \ - " proc = Proc.new { #{prim.to_s}.new(name, options) }\n" \ + " proc = Proc.new do |opts|\n" \ + " #{prim.to_s}.new(name, options.merge(opts))\n" \ + " end\n" \ " @root = [name, proc]\n" \ "end" end # @note This method is named +objectid+ and not +object_id+ to not override # +Object#object_id+. # @see Types::ObjectId#initialize def objectid(name, options={}) - proc = Proc.new { Types::ObjectId.new(name, options) } + proc = Proc.new { |opts| Types::ObjectId.new(name, options.merge(opts)) } @root = [name, proc] end # @see Types::Any#initialize def any(name, options={}) - proc = Proc.new { Types::Any.new(name, options) } + proc = Proc.new { |opts| Types::Any.new(name, options.merge(opts)) } @root = [name, proc] end # Give type name (aka class name) # @return [String] @@ -238,30 +266,30 @@ def is_of?(el) [Types::SequenceOf, Types::SetOf].include? el.class end - def get_type(proc_or_class, name=nil) + def get_type(proc_or_class, options={}) case proc_or_class when Proc - proc_or_class.call + proc_or_class.call(options) when Class proc_or_class.new end end def generate_root root = self.class.class_eval { @root } @root = root[0] @elements = {} - @elements[@root] = get_type(root[1]) + @elements[@root] = get_type(root[1], self.class.class_eval { @options } || {}) root end def set_elements(name, el, content=nil) if content.is_a? Array @elements[name].value = content.map do |name2, proc_or_class, content2| - subel = get_type(proc_or_class, name2) + subel = get_type(proc_or_class) @elements[name2] = subel if is_composed?(subel) and content2.is_a? Array set_elements(name2, proc_or_class, content2) end subel