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

Instance Method Summary

Methods inherited from Base

#==, ===, #[], #[]=, #add_file, #dynamic?, #file, #format, #format_source, #has_tag?, #inspect, #line, #method_missing, new, #parent, #parent=, #relative_path, #root?, #tag, #tags, #to_s, #type

Constructor Details

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

Creates a new method object in namespace with name and an instance or class scope

Parameters:

  • (NamespaceObject) namespace — the namespace
  • (String, Symbol) name — the method name
  • (Symbol) scope (defaults to: :instance):instance or :class


34
35
36
37
38
39
40
# File 'lib/yard/code_objects/method_object.rb', line 34

def initialize(namespace, name, scope = :instance) 
  self.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

- (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.

Returns:

  • (Boolean) — whether the object is explicitly defined in source.


19
20
21
# File 'lib/yard/code_objects/method_object.rb', line 19

def explicit
  @explicit
end

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

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

Returns:



26
27
28
# File 'lib/yard/code_objects/method_object.rb', line 26

def parameters
  @parameters
end

- (Symbol) scope

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

Returns:

  • (Symbol) — the scope


12
13
14
# File 'lib/yard/code_objects/method_object.rb', line 12

def scope
  @scope
end

- (Symbol) visibility

The visibility of the method (:public:, :protected, :private)

Returns:

  • (Symbol) — the method visibility


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

def visibility
  @visibility
end

Instance Method Details

- (Array<Symbol>) aliases

Returns all alias names of the object

Returns:

  • (Array<Symbol>) — the alias names


101
102
103
104
105
106
107
108
# File 'lib/yard/code_objects/method_object.rb', line 101

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

Returns:

  • (SymbolHash) — if there is information about the attribute
  • (nil) — if the method is not an attribute


63
64
65
66
# File 'lib/yard/code_objects/method_object.rb', line 63

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

- (Boolean) constructor?

Whether or not the method is the #initialize constructor method

Returns:

  • (Boolean) — whether or not the method is the #initialize constructor method


56
57
58
# File 'lib/yard/code_objects/method_object.rb', line 56

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

- (Boolean) is_alias?

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

Returns:

  • (Boolean) — whether the object is an alias


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

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

Returns:

  • (Boolean) — whether the object is an attribute


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

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.

Returns:

  • (Boolean) — whether the method is explicitly defined in source


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

def is_explicit?
  explicit ? true : false
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"

Parameters:

  • (Boolean) prefix (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


131
132
133
# File 'lib/yard/code_objects/method_object.rb', line 131

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

- (String) path

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


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

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

- (Boolean) reader?

Whether the method is a reader attribute

Returns:

  • (Boolean) — whether the method is a reader attribute


74
75
76
# File 'lib/yard/code_objects/method_object.rb', line 74

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

- (String) sep (protected)

Override separator to differentiate between class and instance methods.

Returns:

  • (String) — “#” for an instance method, “.” for class


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

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

- (Boolean) writer?

Whether the method is a writer attribute

Returns:

  • (Boolean) — whether the method is a writer attribute


69
70
71
# File 'lib/yard/code_objects/method_object.rb', line 69

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