lib/rasn1/wrapper.rb in rasn1-0.12.0 vs lib/rasn1/wrapper.rb in rasn1-0.12.1

- old
+ new

@@ -12,12 +12,12 @@ # * explicitly wrap an object (i.e wrap the object in another explicit ASN.1 tag) # # @example # # object to wrap # int = RASN1::Types::Integer.new(implicit: 1) # its tag is 0x81 - # # simple wraper, change an option - # wrapper = RASN1::Wrapper.new(int, optional: true, default: 1) + # # simple wrapper, change an option + # wrapper = RASN1::Wrapper.new(int, default: 1) # # implicit wrapper # wrapper = RASN1::Wrapper.new(int, implicit: 3) # wrapped int tag is now 0x83 # # explicit wrapper # wrapper = RASN1::Wrapper.new(int, explicit: 4) # int tag is always 0x81, but it is wrapped in a 0x84 tag # @since 0.12.0 @@ -68,30 +68,10 @@ raise RASN1::Error, 'Cannot be implicit and explicit' if explicit? && implicit? super(element) end - def explicit_implicit(options) - opts = options.dup - @explicit = opts.delete(:explicit) - @implicit = opts.delete(:implicit) - opts - end - - def generate_explicit_wrapper(options) - # ExplicitWrapper is a hand-made explicit tag, but we have to use its implicit option - # to force its tag value. - @explicit_wrapper = ExplicitWrapper.new(options.merge(implicit: @explicit)) - end - - def generate_explicit_wrapper_options(options) - new_opts = {} - new_opts[:default] = options[:default] if options.key?(:default) - new_opts[:optional] = options[:optional] if options.key?(:optional) - new_opts - end - # Say if wrapper is an explicit one (i.e. add tag and length to its element) # @return [Boolean] def explicit? !!@explicit end @@ -134,10 +114,12 @@ else element.parse!(der, ber: ber) end end + # @return [Boolean] + # @see Types::Base#value? def value? if explicit? @explicit_wrapper.value? else __getobj__.value? @@ -149,10 +131,11 @@ def element __getobj__ end # @return [::Integer] + # @see Types::Base#id def id if implicit? @implicit elsif explicit? @explicit @@ -160,34 +143,59 @@ element.id end end # @return [Symbol] + # @see Types::Base#asn1_class def asn1_class return element.asn1_class unless @options.key?(:class) @options[:class] end # @return [Boolean] + # @see Types::Base#constructed def constructed? return element.constructed? unless @options.key?(:constructed) @options[:constructed] end # @return [Boolean] + # @see Types::Base#primitive def primitive? !constructed? end + # @param [::Integer] level + # @return [String] def inspect(level=0) return super(level) unless explicit? @explicit_wrapper.inspect(level) << ' ' << super(level) end private + + def explicit_implicit(options) + opts = options.dup + @explicit = opts.delete(:explicit) + @implicit = opts.delete(:implicit) + opts + end + + def generate_explicit_wrapper(options) + # ExplicitWrapper is a hand-made explicit tag, but we have to use its implicit option + # to force its tag value. + @explicit_wrapper = ExplicitWrapper.new(options.merge(implicit: @explicit)) + end + + def generate_explicit_wrapper_options(options) + new_opts = {} + new_opts[:default] = options[:default] if options.key?(:default) + new_opts[:optional] = options[:optional] if options.key?(:optional) + new_opts + end def generate_implicit_element el = element.dup if el.explicit? el.options = el.options.merge(explicit: @implicit)