lib/yard/code_objects/method_object.rb in yard-0.7.5 vs lib/yard/code_objects/method_object.rb in yard-0.8.0
- old
+ new
@@ -21,37 +21,71 @@
attr_accessor :parameters
# Creates a new method object in +namespace+ with +name+ and an instance
# or class +scope+
#
+ # If scope is +:module+, this object is instantiated as a public
+ # method in +:class+ scope, but also creates a new (empty) method
+ # as a private +:instance+ method on the same class or module.
+ #
# @param [NamespaceObject] namespace the namespace
# @param [String, Symbol] name the method name
- # @param [Symbol] scope +:instance+ or +:class+
- def initialize(namespace, name, scope = :instance)
+ # @param [Symbol] scope +:instance+, +:class+, or +:module+
+ def initialize(namespace, name, scope = :instance, &block)
+ @module_function = false
@scope = nil
- self.visibility = :public
+
+ # handle module function
+ if scope == :module
+ other = self.class.new(namespace, name, &block)
+ other.visibility = :private
+ scope = :class
+ @module_function = true
+ end
+
+ @visibility = :public
self.scope = scope
self.parameters = []
super
end
# Changes the scope of an object from :instance or :class
# @param [Symbol] v the new scope
def scope=(v)
reregister = @scope ? true : false
- YARD::Registry.delete(self) if reregister
+
+ # handle module function
+ if v == :module
+ other = self.class.new(namespace, name)
+ other.visibility = :private
+ @visibility = :public
+ @module_function = true
+ @path = nil
+ end
+
+ YARD::Registry.delete(self)
@path = nil
@scope = v.to_sym
+ if @scope == :module
+ @scope = :class
+ end
YARD::Registry.register(self) if reregister
end
# @return whether or not the method is the #initialize constructor method
def constructor?
name == :initialize && scope == :instance && namespace.is_a?(ClassObject)
end
+ # @return [Boolean] whether or not this method was created as a module
+ # function
+ # @since 0.8.0
+ def module_function?
+ @module_function
+ end
+
# Returns the read/writer info for the attribute if it is one
# @return [SymbolHash] if there is information about the attribute
# @return [nil] if the method is not an attribute
# @since 0.5.3
def attr_info
@@ -135,19 +169,23 @@
# @return [Symbol] the name without {#sep} if prefix is set to false
def name(prefix = false)
prefix ? (sep == ISEP ? "#{sep}#{super}" : super.to_s) : super
end
- protected
-
# Override separator to differentiate between class and instance
# methods.
# @return [String] "#" for an instance method, "." for class
def sep
if scope == :class
namespace && namespace != YARD::Registry.root ? CSEP : NSEP
else
ISEP
end
+ end
+
+ protected
+
+ def copyable_attributes
+ super - %w(scope module_function)
end
end
end