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

#initialize(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



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

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

#explicitBoolean

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.



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

def explicit
  @explicit
end

#parametersArray<Array(String, String)>

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

Returns:



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

def parameters
  @parameters
end

#scopeSymbol

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

Returns:

  • (Symbol)

    the scope



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

def scope
  @scope
end

Instance Method Details

#aliasesArray<Symbol>

Returns all alias names of the object

Returns:

  • (Array<Symbol>)

    the alias names



143
144
145
146
147
148
149
150
# File 'lib/yard/code_objects/method_object.rb', line 143

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



94
95
96
97
# File 'lib/yard/code_objects/method_object.rb', line 94

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

#constructor?Boolean

Returns whether or not the method is the #initialize constructor method.

Returns:

  • (Boolean)

    whether or not the method is the #initialize constructor method



79
80
81
# File 'lib/yard/code_objects/method_object.rb', line 79

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

#copyable_attributesObject (protected)



190
191
192
# File 'lib/yard/code_objects/method_object.rb', line 190

def copyable_attributes
  super - %w(scope module_function)
end

#is_alias?Boolean

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

Returns:

  • (Boolean)

    whether the object is an alias



120
121
122
123
# File 'lib/yard/code_objects/method_object.rb', line 120

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

#is_attribute?Boolean

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

Returns:

  • (Boolean)

    whether the object is an attribute



113
114
115
116
# File 'lib/yard/code_objects/method_object.rb', line 113

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

#is_explicit?Boolean

Tests boolean #explicit value.

Returns:

  • (Boolean)

    whether the method is explicitly defined in source



128
129
130
# File 'lib/yard/code_objects/method_object.rb', line 128

def is_explicit?
  explicit ? true : false
end

#module_function?Boolean

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

Returns:

  • (Boolean)

    whether or not this method was created as a module function

Since:

  • 0.8.0



86
87
88
# File 'lib/yard/code_objects/method_object.rb', line 86

def module_function?
  @module_function
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



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

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



135
136
137
138
139
# File 'lib/yard/code_objects/method_object.rb', line 135

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



155
156
157
158
159
160
161
# File 'lib/yard/code_objects/method_object.rb', line 155

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

#reader?Boolean

Returns whether the method is a reader attribute.

Returns:

  • (Boolean)

    whether the method is a reader attribute

Since:

  • 0.5.3



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

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

#sepString

Override separator to differentiate between class and instance methods.

Returns:

  • (String)

    “#” for an instance method, “.” for class



180
181
182
183
184
185
186
# File 'lib/yard/code_objects/method_object.rb', line 180

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

#writer?Boolean

Returns whether the method is a writer attribute.

Returns:

  • (Boolean)

    whether the method is a writer attribute

Since:

  • 0.5.3



101
102
103
# File 'lib/yard/code_objects/method_object.rb', line 101

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