123456789_123456789_123456789_123456789_123456789_

Class: YARD::CodeObjects::MethodObject

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base
Instance Chain:
self, Base
Inherits: YARD::CodeObjects::Base
Defined in: lib/yard/code_objects/method_object.rb

Overview

Represents a Ruby method in source

Class Method Summary

Base - Inherited

.===

Compares the class with subclasses.

.new

Allocates a new code object.

Instance Attribute Summary

Base - Inherited

#base_docstring

The non-localized documentation string associated with the object.

#dynamic

Marks whether or not the method is conditionally defined at runtime.

#dynamic?

Is the object defined conditionally at runtime?

#files

The files the object was defined in.

#group,
#namespace

The namespace the object is defined in.

#namespace=

Sets the namespace the object is defined in.

#parent

Alias for Base#namespace.

#root?,
#signature

The one line signature representing an object.

#source

The source code associated with the object.

#source=

Attaches source code to a code object with an optional file location.

#source_type

Language of the source code associated with the object.

#visibility, #visibility=

Instance Method Summary

Base - Inherited

#==

Alias for Base#equal?.

#[]

Accesses a custom attribute on the object.

#[]=

Sets a custom attribute on the object.

#add_file

Associates a file with a code object, optionally adding the line where it was defined.

#add_tag

Add tags to the #docstring

#copy_to

Copies all data in this object to another code object, except for uniquely identifying information (path, namespace, name, scope).

#docstring

The documentation string associated with the object.

#docstring=

Attaches a docstring to a code object by parsing the comments attached to the statement and filling the #tags and #docstring methods with the parsed information.

#eql?

Alias for Base#equal?.

#equal?

Tests if another object is equal to this, including a proxy.

#file

Returns the filename the object was first parsed at, taking definitions with docstrings first.

#format

Renders the object using the templating system.

#has_tag?

Tests if the #docstring has a tag.

#hash,
#initialize

Creates a new code object.

#inspect

Inspects the object, returning the type and path.

#line

Returns the line the object was first parsed at (or nil).

#method_missing,
#name

The name of the object.

#path

Represents the unique path of the object.

#relative_path,
#sep

Override this method with a custom component separator.

#tag

Gets a tag from the #docstring

#tags

Gets a list of tags from the #docstring

#title, #to_ary,
#to_s

Alias for Base#path.

#type

Default type is the lowercase class name without the "Object" suffix.

#format_source

Formats source code by removing leading indentation.

#translate_docstring

Constructor Details

.new(namespace, name, scope = :instance, &block) ⇒ MethodObject

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.

Parameters:

  • namespace (NamespaceObject)

    the namespace

  • name (String, Symbol)

    the method name

  • scope (Symbol) (defaults to: :instance)

    :instance, :class, or :module

[ GitHub ]

  
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class YARD::CodeObjects::Base

Instance Attribute Details

#constructor?Boolean (readonly)

Returns:

  • (Boolean)

    whether or not the method is the #initialize constructor method

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 78

def constructor?
  name == :initialize && scope == :instance && namespace.is_a?(ClassObject)
end

#explicitBoolean (rw)

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.

Returns:

  • (Boolean)

    whether the object is explicitly defined in source.

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 18

attr_accessor :explicit

#is_alias?Boolean (readonly)

Tests if the object is defined as an alias of another method

Returns:

  • (Boolean)

    whether the object is an alias

[ GitHub ]

  
# 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

#is_attribute?Boolean (readonly)

Tests if the object is defined as an attribute in the namespace

Returns:

  • (Boolean)

    whether the object is an attribute

[ GitHub ]

  
# 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

#is_explicit?Boolean (readonly)

Tests boolean #explicit value.

Returns:

  • (Boolean)

    whether the method is explicitly defined in source

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 134

def is_explicit?
  explicit ? true : false
end

#module_function?Boolean (readonly)

Returns:

  • (Boolean)

    whether or not this method was created as a module function

Since:

  • 0.8.0

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 85

def module_function?
  @module_function
end

#parametersArray<Array(String, String)> (rw)

Returns the list of parameters parsed out of the method signature with their default values.

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 25

attr_accessor :parameters

#reader?Boolean (readonly)

Returns:

  • (Boolean)

    whether the method is a reader attribute

Since:

  • 0.5.3

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 107

def reader?
  info = attr_info
  info && info[:read] == self ? true : false
end

#scopeSymbol (rw)

The scope of the method (+:class+ or :instance)

Returns:

  • (Symbol)

    the scope

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 11

attr_reader :scope

#scope=(v) (rw)

Changes the scope of an object from :instance or :class

Parameters:

  • v (Symbol)

    the new scope

[ GitHub ]

  
# 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

#writer?Boolean (readonly)

Returns:

  • (Boolean)

    whether the method is a writer attribute

Since:

  • 0.5.3

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 100

def writer?
  info = attr_info
  info && info[:write] == self ? true : false
end

Instance Method Details

#aliasesArray<MethodObject>

Returns all alias names of the object

Returns:

  • (Array<MethodObject>)

    the alias names

[ GitHub ]

  
# 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

#attr_infoSymbolHash?

Returns the read/writer info for the attribute if it is one

Returns:

  • (SymbolHash)

    if there is information about the attribute

  • (nil)

    if the method is not an attribute

Since:

  • 0.5.3

[ GitHub ]

  
# 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

#name(prefix = false) ⇒ String, Symbol

Returns the name of the object.

Examples:

The name of an instance method (with prefix)

an_instance_method.name(true) # => "#mymethod"

The name of a class method (with prefix)

a_class_method.name(true) # => "mymethod"

Parameters:

  • prefix (Boolean) (defaults to: false)

    whether or not to show the prefix

Returns:

  • (String)

    returns #sep + name for an instance method if prefix is true

  • (Symbol)

    the name without #sep if prefix is set to false

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 175

def name(prefix = false)
  prefix ? (sep == ISEP ? "#{sep}#{super}" : super.to_s) : super
end

#overridden_methodMethodObject?

Returns:

  • (MethodObject)

    the object that this method overrides

  • (nil)

    if it does not override a method

Since:

  • 0.6.0

[ GitHub ]

  
# 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

#pathString

Override path handling for instance methods in the root namespace (they should still have a separator as a prefix).

Returns:

  • (String)

    the path of a method

[ GitHub ]

  
# File 'lib/yard/code_objects/method_object.rb', line 161

def path
  @path ||= !namespace || namespace.path == "" ? sep + super : super
end

#sepString

Override separator to differentiate between class and instance methods.

Returns:

  • (String)

    "#" for an instance method, "." for class

[ GitHub ]

  
# 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