Class: YARD::CodeObjects::MethodObject
- Defined in:
- lib/yard/code_objects/method_object.rb
Overview
Represents a Ruby method in source
Instance Attribute Summary collapse
-
#explicit ⇒ Boolean
Whether the object is explicitly defined in source or whether it was inferred by a handler.
-
#parameters ⇒ Array<Array(String, String)>
Returns the list of parameters parsed out of the method signature with their default values.
-
#scope ⇒ Symbol
The scope of the method (
:class
or:instance
).
Attributes inherited from Base
#base_docstring, #dynamic, #files, #group, #namespace, #signature, #source, #source_type, #visibility
Instance Method Summary collapse
-
#aliases ⇒ Array<MethodObject>
Returns all alias names of the object.
-
#attr_info ⇒ SymbolHash?
Returns the read/writer info for the attribute if it is one.
-
#constructor? ⇒ Boolean
Whether or not the method is the #initialize constructor method.
-
#initialize(namespace, name, scope = :instance, &block) ⇒ MethodObject
constructor
Creates a new method object in
namespace
withname
and an instance or classscope
. -
#is_alias? ⇒ Boolean
Tests if the object is defined as an alias of another method.
-
#is_attribute? ⇒ Boolean
Tests if the object is defined as an attribute in the namespace.
-
#is_explicit? ⇒ Boolean
Tests boolean #explicit value.
-
#module_function? ⇒ Boolean
Whether or not this method was created as a module function.
-
#name(prefix = false) ⇒ String, Symbol
Returns the name of the object.
- #overridden_method ⇒ MethodObject?
-
#path ⇒ String
Override path handling for instance methods in the root namespace (they should still have a separator as a prefix).
-
#reader? ⇒ Boolean
Whether the method is a reader attribute.
-
#sep ⇒ String
Override separator to differentiate between class and instance methods.
-
#writer? ⇒ Boolean
Whether the method is a writer attribute.
Methods inherited from Base
===, #[], #[]=, #add_file, #add_tag, #copy_to, #docstring, #docstring=, #dynamic?, #equal?, #file, #format, #has_tag?, #hash, #inspect, #line, #method_missing, new, #relative_path, #root?, #tag, #tags, #title, #to_ary, #type
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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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
#explicit ⇒ Boolean
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.
18 19 20 |
# File 'lib/yard/code_objects/method_object.rb', line 18 def explicit @explicit end |
#parameters ⇒ Array<Array(String, String)>
Returns the list of parameters parsed out of the method signature with their default values.
25 26 27 |
# File 'lib/yard/code_objects/method_object.rb', line 25 def parameters @parameters end |
#scope ⇒ Symbol
The scope of the method (:class
or :instance
)
11 12 13 |
# File 'lib/yard/code_objects/method_object.rb', line 11 def scope @scope end |
Instance Method Details
#aliases ⇒ Array<MethodObject>
Returns all alias names of the object
149 150 151 152 153 154 155 156 |
# 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_info ⇒ SymbolHash?
Returns the read/writer info for the attribute if it is one
93 94 95 96 |
# 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 |
#constructor? ⇒ Boolean
Returns whether or not the method is the #initialize constructor method.
78 79 80 |
# File 'lib/yard/code_objects/method_object.rb', line 78 def constructor? name == :initialize && scope == :instance && namespace.is_a?(ClassObject) end |
#is_alias? ⇒ Boolean
Tests if the object is defined as an alias of another method
126 127 128 129 |
# 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
Tests if the object is defined as an attribute in the namespace
114 115 116 117 118 119 120 121 122 |
# 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
Tests boolean #explicit value.
134 135 136 |
# File 'lib/yard/code_objects/method_object.rb', line 134 def is_explicit? explicit ? true : false end |
#module_function? ⇒ Boolean
Returns whether or not this method was created as a module function.
85 86 87 |
# File 'lib/yard/code_objects/method_object.rb', line 85 def module_function? @module_function end |
#name(prefix = false) ⇒ String, Symbol
Returns the name of the object.
175 176 177 |
# 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_method ⇒ MethodObject?
141 142 143 144 145 |
# 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 |
#path ⇒ String
Override path handling for instance methods in the root namespace (they should still have a separator as a prefix).
161 162 163 |
# File 'lib/yard/code_objects/method_object.rb', line 161 def path @path ||= !namespace || namespace.path == "" ? sep + super : super end |
#reader? ⇒ Boolean
Returns whether the method is a reader attribute.
107 108 109 110 |
# File 'lib/yard/code_objects/method_object.rb', line 107 def reader? info = attr_info info && info[:read] == self ? true : false end |
#sep ⇒ String
Override separator to differentiate between class and instance methods.
182 183 184 185 186 187 188 |
# 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 |
#writer? ⇒ Boolean
Returns whether the method is a writer attribute.
100 101 102 103 |
# File 'lib/yard/code_objects/method_object.rb', line 100 def writer? info = attr_info info && info[:write] == self ? true : false end |