Class: YARD::CodeObjects::MethodObject

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/code_objects/method_object.rb

Overview

Represents a Ruby method in source

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (MethodObject) initialize(namespace, name, scope = :instance, &block)

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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yard/code_objects/method_object.rb', line 33

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

- (Docstring) base_docstring (readonly) Originally defined in class Base

The non-localized documentation string associated with the object

Since:

  • 0.8.4

- (Boolean) dynamic Originally defined in class Base

Marks whether or not the method is conditionally defined at runtime

- (Boolean) explicit

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.



14
15
16
# File 'lib/yard/code_objects/method_object.rb', line 14

def explicit
  @explicit
end

- (Array<String>) files (readonly) Originally defined in class Base

The files the object was defined in. To add a file, use #add_file.

See Also:

- (String) group Originally defined in class Base

Returns the group this object is associated with

Since:

  • 0.6.0

- (NamespaceObject) namespace Also known as: parent Originally defined in class Base

The namespace the object is defined in. If the object is in the top level namespace, this is Registry.root

- (Array<Array(String, String)>) parameters

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



21
22
23
# File 'lib/yard/code_objects/method_object.rb', line 21

def parameters
  @parameters
end

- (Symbol) scope

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



7
8
9
# File 'lib/yard/code_objects/method_object.rb', line 7

def scope
  @scope
end

- (String) signature Originally defined in class Base

The one line signature representing an object. For a method, this will be of the form “def meth(arguments…)”. This is usually the first source line.

- (String?) source Originally defined in class Base

The source code associated with the object

- (Symbol) source_type Originally defined in class Base

Language of the source code associated with the object. Defaults to :ruby.

- (Symbol) visibility Originally defined in class Base

Instance Method Details

- (Array<Symbol>) aliases

Returns all alias names of the object



140
141
142
143
144
145
146
147
# File 'lib/yard/code_objects/method_object.rb', line 140

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

- (SymbolHash?) attr_info

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

Since:

  • 0.5.3



91
92
93
94
# File 'lib/yard/code_objects/method_object.rb', line 91

def attr_info
  return nil unless namespace.is_a?(NamespaceObject)
  namespace.attributes[scope][name.to_s.gsub(/=$/, '')]
end

- (Boolean) constructor?



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

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

- (Object) copyable_attributes (protected)



187
188
189
# File 'lib/yard/code_objects/method_object.rb', line 187

def copyable_attributes
  super - %w(scope module_function)
end

- (Boolean) is_alias?

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



117
118
119
120
# File 'lib/yard/code_objects/method_object.rb', line 117

def is_alias?
  return false unless namespace.is_a?(NamespaceObject)
  namespace.aliases.has_key? self
end

- (Boolean) is_attribute?

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



110
111
112
113
# File 'lib/yard/code_objects/method_object.rb', line 110

def is_attribute?
  return false unless info = attr_info
  info[name.to_s =~ /=$/ ? :write : :read] ? true : false
end

- (Boolean) is_explicit?

Tests boolean #explicit value.



125
126
127
# File 'lib/yard/code_objects/method_object.rb', line 125

def is_explicit?
  explicit ? true : false
end

- (Boolean) module_function?

Returns whether or not this method was created as a module function

Since:

  • 0.8.0



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

def module_function?
  @module_function
end

- (String, Symbol) name(prefix = false)

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"


170
171
172
# File 'lib/yard/code_objects/method_object.rb', line 170

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

- (MethodObject?) overridden_method

Since:

  • 0.6.0



132
133
134
135
136
# File 'lib/yard/code_objects/method_object.rb', line 132

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

- (String) path

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



152
153
154
155
156
157
158
# File 'lib/yard/code_objects/method_object.rb', line 152

def path
  @path ||= if !namespace || namespace.path == ""
    sep + super
  else
    super
  end
end

- (Boolean) reader?

Returns whether the method is a reader attribute

Since:

  • 0.5.3



104
105
106
# File 'lib/yard/code_objects/method_object.rb', line 104

def reader?
  !!((info = attr_info) && info[:read] == self)
end

- (String) sep

Override separator to differentiate between class and instance methods.



177
178
179
180
181
182
183
# File 'lib/yard/code_objects/method_object.rb', line 177

def sep
  if scope == :class
    namespace && namespace != YARD::Registry.root ? CSEP : NSEP
  else
    ISEP
  end
end

- (Boolean) writer?

Returns whether the method is a writer attribute

Since:

  • 0.5.3



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

def writer?
  !!((info = attr_info) && info[:write] == self)
end