class YARD::CodeObjects::MethodObject
Represents a Ruby method in source
Attributes
Whether the object is explicitly defined in source or whether it was inferred by a handler. For instance, attribute methods are generally inferred and therefore not explicitly defined in source.
@return [Boolean] whether the object is explicitly defined in source.
Returns the list of parameters parsed out of the method signature with their default values.
@return [Array<Array(String, String
)>] a list of parameter names followed
by their default values (or nil)
The scope of the method (:class
or :instance
)
@return [Symbol] the scope
Public Class Methods
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
, :class
, or :module
YARD::CodeObjects::Base::new
# File lib/yard/code_objects/method_object.rb, line 37 def initialize(namespace, name, scope = :instance, &block) @module_function = false @scope = nil # 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
Public Instance Methods
Returns all alias names of the object @return [Array<Symbol>] the alias names
# File lib/yard/code_objects/method_object.rb, line 149 def aliases list = [] return list unless namespace.is_a?(NamespaceObject) namespace.aliases.each do |o, aname| list << o if aname == name && o.scope == scope end list 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
# File lib/yard/code_objects/method_object.rb, line 93 def attr_info return nil unless namespace.is_a?(NamespaceObject) namespace.attributes[scope][name.to_s.gsub(/=$/, '')] end
@return whether or not the method is the initialize constructor method
# File lib/yard/code_objects/method_object.rb, line 78 def constructor? name == :initialize && scope == :instance && namespace.is_a?(ClassObject) end
Tests if the object is defined as an alias of another method @return [Boolean] whether the object is an alias
# File lib/yard/code_objects/method_object.rb, line 126 def is_alias? return false unless namespace.is_a?(NamespaceObject) namespace.aliases.key? self end
Tests if the object is defined as an attribute in the namespace @return [Boolean] whether the object is an attribute
# File lib/yard/code_objects/method_object.rb, line 114 def is_attribute? info = attr_info if info read_or_write = name.to_s =~ /=$/ ? :write : :read info[read_or_write] ? true : false else false end end
Tests boolean {#explicit} value.
@return [Boolean] whether the method is explicitly defined in source
# File lib/yard/code_objects/method_object.rb, line 134 def is_explicit? explicit ? true : false end
@return [Boolean] whether or not this method was created as a module
function
@since 0.8.0
# File lib/yard/code_objects/method_object.rb, line 85 def module_function? @module_function end
Returns the name of the object.
@example The name of an instance method (with prefix)
an_instance_method.name(true) # => "#mymethod"
@example The name of a class method (with prefix)
a_class_method.name(true) # => "mymethod"
@param [Boolean] prefix whether or not to show the prefix @return [String] returns {#sep} + name
for an instance method if
prefix is true
@return [Symbol] the name without {#sep} if prefix is set to false
YARD::CodeObjects::Base#name
# File lib/yard/code_objects/method_object.rb, line 175 def name(prefix = false) prefix ? (sep == ISEP ? "#{sep}#{super}" : super.to_s) : super end
@return [MethodObject] the object that this method overrides @return [nil] if it does not override a method @since 0.6.0
# File lib/yard/code_objects/method_object.rb, line 141 def overridden_method return nil if namespace.is_a?(Proxy) meths = namespace.meths(:all => true) meths.find {|m| m.path != path && m.name == name && m.scope == scope } end
Override path handling for instance methods in the root namespace (they should still have a separator as a prefix). @return [String] the path of a method
YARD::CodeObjects::Base#path
# File lib/yard/code_objects/method_object.rb, line 161 def path @path ||= !namespace || namespace.path == "" ? sep + super : super end
@return [Boolean] whether the method is a reader attribute @since 0.5.3
# File lib/yard/code_objects/method_object.rb, line 107 def reader? info = attr_info info && info[:read] == self ? true : false end
Changes the scope of an object from :instance or :class @param [Symbol] v the new scope
# File lib/yard/code_objects/method_object.rb, line 58 def scope=(v) reregister = @scope ? true : false # 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 @scope = :class if @scope == :module YARD::Registry.register(self) if reregister end
Override separator to differentiate between class and instance methods. @return [String] “#” for an instance method, “.” for class
# File lib/yard/code_objects/method_object.rb, line 182 def sep if scope == :class namespace && namespace != YARD::Registry.root ? CSEP : NSEP else ISEP end end
@return [Boolean] whether the method is a writer attribute @since 0.5.3
# File lib/yard/code_objects/method_object.rb, line 100 def writer? info = attr_info info && info[:write] == self ? true : false end
Protected Instance Methods
YARD::CodeObjects::Base#copyable_attributes
# File lib/yard/code_objects/method_object.rb, line 192 def copyable_attributes super - %w(scope module_function) end