lib/yard/code_objects/base.rb in yard-0.7.5 vs lib/yard/code_objects/base.rb in yard-0.8.0

- old
+ new

@@ -222,10 +222,25 @@ @namespace = nil self.namespace = namespace yield(self) if block_given? end + # Copies all data in this object to another code object, except for + # uniquely identifying information (path, namespace, name, scope). + # + # @param [Base] other the object to copy data to + # @return [Base] the other object + # @since 0.8.0 + def copy_to(other) + copyable_attributes.each do |ivar| + ivar = "@#{ivar}" + other.instance_variable_set(ivar, instance_variable_get(ivar)) + end + other.docstring = docstring.to_raw + other + end + # The name of the object # @param [Boolean] prefix whether to show a prefix. Implement # this in a subclass to define how the prefix is showed. # @return [Symbol] if prefix is false, the symbolized name # @return [String] if prefix is true, prefix + the name as a String. @@ -284,11 +299,11 @@ alias == equal? alias eql? equal? # @return [Integer] the object's hash value (for equality checking) def hash; path.hash end - + # @return [nil] this object does not turn into an array def to_ary; nil end # Accesses a custom attribute on the object # @param [#to_s] key the name of the custom attribute @@ -346,10 +361,11 @@ else @source = format_source(statement.to_s) end end + undef docstring def docstring return @docstring if !@docstring_extra case @docstring when Proxy return @docstring_extra @@ -443,11 +459,11 @@ # @option options [Symbol] :markup (nil) the markup type (:rdoc, :markdown, :textile) # @option options [Serializers::Base] :serializer (nil) see Serializers # @return [String] the rendered template # @see Templates::Engine#render def format(options = {}) - options.merge!(:object => self) + options = options.merge(:object => self) Templates::Engine.render(options) end # Inspects the object, returning the type and path # @return [String] a string describing the object @@ -492,19 +508,34 @@ def has_tag?(name); docstring.has_tag?(name) end # @return whether or not this object is a RootObject def root?; false end - protected - # Override this method with a custom component separator. For instance, # {MethodObject} implements sep as '#' or '.' (depending on if the # method is instance or class respectively). {#path} depends on this # value to generate the full path in the form: namespace.path + sep + name # # @return [String] the component that separates the namespace path # and the name (default is {NSEP}) def sep; NSEP end + + protected + + # Override this method if your code object subclass does not allow + # copying of certain attributes. + # + # @return [Array<String>] the list of instance variable names (without + # "@" prefix) that should be copied when {#copy_to} is called + # @see #copy_to + # @since 0.8.0 + def copyable_attributes + vars = instance_variables.map {|ivar| ivar.to_s[1..-1] } + vars -= %w(docstring namespace name path) + vars + end + + private # Formats source code by removing leading indentation # # @param [String] source the source code to format # @return [String] formatted source