Class: YARD::CodeObjects::Base Abstract
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/yard/code_objects/base.rb |
Overview
Base
is the superclass of all code objects recognized by ::YARD
. A code
object is any entity in the Ruby language (class, method, module). A
DSL might subclass Base
to create a new custom object representing
a new entity type.
Registry Integration
Any created object associated with a namespace is immediately registered
with the registry. This allows the ::YARD::Registry
to act as an identity map
to ensure that no object is represented by more than one Ruby object
in memory. A unique #path is essential for this identity map to work
correctly.
Custom Attributes
Code objects allow arbitrary custom attributes to be set using the #[]= assignment method.
Namespaces
There is a special type of object called a "namespace". These are subclasses
of the NamespaceObject
and represent Ruby entities that can have
objects defined within them. Classically these are modules and classes,
though a DSL might create a custom NamespaceObject
to describe a
specific set of objects.
Separators
Custom classes with different separator tokens should define their own separators using the NamespaceMapper#register_separator method. The standard Ruby separators have already been defined ('::', '#', '.', etc).
Class Method Summary
-
.===(other) ⇒ Boolean
Compares the class with subclasses.
-
.new(namespace, name, *args) {|obj| ... } ⇒ Base
constructor
Allocates a new code object.
Instance Attribute Summary
-
#base_docstring ⇒ Docstring
readonly
The non-localized documentation string associated with the object.
-
#dynamic ⇒ Boolean
rw
Marks whether or not the method is conditionally defined at runtime.
-
#dynamic? ⇒ Boolean
rw
Is the object defined conditionally at runtime?
-
#files ⇒ Array<Array(String, Integer)>
readonly
The files the object was defined in.
- #group ⇒ String rw
-
#namespace ⇒ NamespaceObject
(also: #parent)
rw
The namespace the object is defined in.
-
#namespace=(obj)
(also: #parent=)
rw
Sets the namespace the object is defined in.
-
#parent
rw
Alias for #namespace.
- #root? ⇒ Boolean readonly
-
#signature ⇒ String
rw
The one line signature representing an object.
-
#source ⇒ String?
rw
The source code associated with the object.
-
#source=(statement)
rw
Attaches source code to a code object with an optional file location.
-
#source_type ⇒ Symbol
rw
Language of the source code associated with the object.
- #visibility ⇒ Symbol rw
- #visibility=(v) ⇒ Symbol rw
Instance Method Summary
-
#==(other)
Alias for #equal?.
-
#[](key) ⇒ Object?
Accesses a custom attribute on the object.
-
#[]=(key, value) ⇒ void
Sets a custom attribute on the object.
-
#add_file(file, line = nil, has_comments = false)
Associates a file with a code object, optionally adding the line where it was defined.
-
#add_tag(*tags)
Add tags to the #docstring
-
#copy_to(other) ⇒ Base
Copies all data in this object to another code object, except for uniquely identifying information (path, namespace, name, scope).
-
#docstring(locale = I18n::Locale.default) ⇒ Docstring
The documentation string associated with the object.
-
#docstring=(comments)
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?(other)
Alias for #equal?.
-
#equal?(other) ⇒ Boolean
(also: #==, #eql?)
Tests if another object is equal to this, including a proxy.
-
#file ⇒ String?
Returns the filename the object was first parsed at, taking definitions with docstrings first.
-
#format(options = {}) ⇒ String
Renders the object using the templating system.
-
#has_tag?(name) ⇒ Boolean
Tests if the #docstring has a tag.
- #hash ⇒ Integer
-
#initialize(namespace, name) {|self| ... } ⇒ Base
constructor
Creates a new code object.
-
#inspect ⇒ String
Inspects the object, returning the type and path.
-
#line ⇒ Fixnum?
Returns the line the object was first parsed at (or nil).
- #dynamic_attr_name ⇒ Object
-
#name(prefix = false) ⇒ Symbol, String
The name of the object.
-
#path ⇒ String
(also: #to_s)
Represents the unique path of the object.
- #relative_path(other) ⇒ String
-
#sep ⇒ String
Override this method with a custom component separator.
-
#tag(name)
Gets a tag from the #docstring
-
#tags(name = nil)
Gets a list of tags from the #docstring
- #title ⇒ String
- #to_ary ⇒ nil
-
#to_s
Alias for #path.
-
#type ⇒ Symbol
Default type is the lowercase class name without the "Object" suffix.
-
#format_source(source) ⇒ String
private
Formats source code by removing leading indentation.
- #translate_docstring(locale) private
Constructor Details
.new(namespace, name, *args) {|obj| ... } ⇒ Base
Allocates a new code object
# File 'lib/yard/code_objects/base.rb', line 189
def new(namespace, name, *args, &block) raise ArgumentError, "invalid empty object name" if name.to_s.empty? if namespace.is_a?(ConstantObject) unless namespace.value =~ /\A#{NAMESPACEMATCH}\Z/ raise Parser::UndocumentableError, "constant mapping" end namespace = Proxy.new(namespace.namespace, namespace.value) end if name.to_s[0, 2] == NSEP name = name.to_s[2..-1] namespace = Registry.root end if name =~ /(?:#{NSEPQ})([^:]+)$/ return new(Proxy.new(namespace, $`), $1, *args, &block) end obj = super(namespace, name, *args) existing_obj = Registry.at(obj.path) obj = existing_obj if existing_obj && existing_obj.class == self yield(obj) if block_given? obj end
#initialize(namespace, name) {|self| ... } ⇒ Base
Creates a new code object
# File 'lib/yard/code_objects/base.rb', line 238
def initialize(namespace, name, *) if namespace && namespace != :root && !namespace.is_a?(NamespaceObject) && !namespace.is_a?(Proxy) raise ArgumentError, "Invalid namespace object: #{namespace}" end @files = [] @current_file_has_comments = false @name = name.to_sym @source_type = :ruby @visibility = :public @tags = [] @docstrings = {} @docstring = Docstring.new!('', [], self) @namespace = nil self.namespace = namespace yield(self) if block_given? end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#dynamic_attr_name ⇒ Object
#dynamic_attr_name=(value) ⇒ value
Object
#dynamic_attr_name=(value) ⇒ value
Class Method Details
.===(other) ⇒ Boolean
Compares the class with subclasses
# File 'lib/yard/code_objects/base.rb', line 219
def ===(other) other.is_a?(self) end
Instance Attribute Details
#base_docstring ⇒ Docstring (readonly)
The non-localized documentation string associated with the object
# File 'lib/yard/code_objects/base.rb', line 166
attr_reader :base_docstring
#dynamic ⇒ Boolean
(rw)
Marks whether or not the method is conditionally defined at runtime
# File 'lib/yard/code_objects/base.rb', line 170
attr_accessor :dynamic
#dynamic? ⇒ Boolean
(rw)
Is the object defined conditionally at runtime?
# File 'lib/yard/code_objects/base.rb', line 178
def dynamic?; @dynamic end
#files ⇒ Array<Array(String, Integer
)> (readonly)
The files the object was defined in. To add a file, use #add_file.
# File 'lib/yard/code_objects/base.rb', line 137
attr_reader :files
#group ⇒ String (rw)
# File 'lib/yard/code_objects/base.rb', line 174
attr_accessor :group
#namespace ⇒ NamespaceObject (rw) Also known as: #parent
The namespace the object is defined in. If the object is in the top level namespace, this is Registry.root
# File 'lib/yard/code_objects/base.rb', line 142
attr_reader :namespace
#namespace=(obj) (rw) Also known as: #parent=
Sets the namespace the object is defined in.
# File 'lib/yard/code_objects/base.rb', line 522
def namespace=(obj) if @namespace @namespace.children.delete(self) Registry.delete(self) end @namespace = (obj == :root ? Registry.root : obj) if @namespace reg_obj = Registry.at(path) return if reg_obj && reg_obj.class == self.class unless @namespace.is_a?(Proxy) # remove prior objects from obj's children that match this one @namespace.children.delete_if {|o| o.path == path } @namespace.children << self end Registry.register(self) end end
#parent (rw)
Alias for #namespace.
# File 'lib/yard/code_objects/base.rb', line 543
alias parent namespace
#root? ⇒ Boolean
(readonly)
# File 'lib/yard/code_objects/base.rb', line 567
def root?; false end
#signature ⇒ String (rw)
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.
# File 'lib/yard/code_objects/base.rb', line 159
attr_accessor :signature
#source ⇒ String? (rw)
The source code associated with the object
# File 'lib/yard/code_objects/base.rb', line 146
attr_reader :source
#source=(statement) (rw)
Attaches source code to a code object with an optional file location
# File 'lib/yard/code_objects/base.rb', line 388
def source=(statement) if statement.respond_to?(:source) @source = format_source(statement.source.strip) else @source = format_source(statement.to_s) end if statement.respond_to?(:signature) self.signature = statement.signature end end
#source_type ⇒ Symbol
(rw)
Language of the source code associated with the object. Defaults to
:ruby
.
# File 'lib/yard/code_objects/base.rb', line 152
attr_accessor :source_type
#visibility ⇒ Symbol
(rw)
# File 'lib/yard/code_objects/base.rb', line 181
attr_accessor :visibility
#visibility=(v) ⇒ Symbol
(rw)
# File 'lib/yard/code_objects/base.rb', line 183
attr_accessor :visibility
Instance Method Details
#==(other)
Alias for #equal?.
# File 'lib/yard/code_objects/base.rb', line 330
alias == equal?
#[](key) ⇒ Object
?
Accesses a custom attribute on the object
# File 'lib/yard/code_objects/base.rb', line 343
def [](key) if respond_to?(key) send(key) elsif instance_variable_defined?("@#{key}") instance_variable_get("@#{key}") end end
#[]=(key, value) ⇒ void
This method returns an undefined value.
Sets a custom attribute on the object
# File 'lib/yard/code_objects/base.rb', line 356
def []=(key, value) if respond_to?("#{key}=") send("#{key}=", value) else instance_variable_set("@#{key}", value) end end
#add_file(file, line = nil, has_comments = false)
Associates a file with a code object, optionally adding the line where it was defined.
By convention, '
# File 'lib/yard/code_objects/base.rb', line 290
def add_file(file, line = nil, has_comments = false) raise(ArgumentError, "file cannot be nil or empty") if file.nil? || file == '' obj = [file.to_s, line] return if files.include?(obj) if has_comments && !@current_file_has_comments @current_file_has_comments = true @files.unshift(obj) else @files << obj # back of the line end end
#add_tag(*tags)
Add tags to the #docstring
# File 'lib/yard/code_objects/base.rb', line 561
def add_tag(* ) @docstrings.clear @docstring.add_tag(* ) end
#copy_to(other) ⇒ Base
Copies all data in this object to another code object, except for uniquely identifying information (path, namespace, name, scope).
# File 'lib/yard/code_objects/base.rb', line 263
def copy_to(other) copyable_attributes.each do |ivar| ivar = "@#{ivar}" other.instance_variable_set(ivar, instance_variable_get(ivar)) end other.docstring = @docstring.to_raw other end
#docstring(locale = I18n::Locale.default) ⇒ Docstring
The documentation string associated with the object
# File 'lib/yard/code_objects/base.rb', line 405
def docstring(locale = I18n::Locale.default) if locale.nil? @docstring.resolve_reference return @docstring end if locale.is_a?(String) locale_name = locale locale = nil else locale_name = locale.name end @docstrings[locale_name] ||= translate_docstring(locale || Registry.locale(locale_name)) end
#docstring=(comments)
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?(other)
Alias for #equal?.
# File 'lib/yard/code_objects/base.rb', line 331
alias eql? equal?
#equal?(other) ⇒ Boolean
Also known as: #==, #eql?
Tests if another object is equal to this, including a proxy
#file ⇒ String?
Returns the filename the object was first parsed at, taking definitions with docstrings first.
# File 'lib/yard/code_objects/base.rb', line 307
def file @files.first ? @files.first[0] : nil end
#format(options = {}) ⇒ String
Renders the object using the templating system.
#format_source(source) ⇒ String (private)
Formats source code by removing leading indentation
#has_tag?(name) ⇒ Boolean
Tests if the #docstring has a tag
#hash ⇒ Integer
# File 'lib/yard/code_objects/base.rb', line 334
def hash; path.hash end
#inspect ⇒ String
Inspects the object, returning the type and path
#line ⇒ Fixnum
?
Returns the line the object was first parsed at (or nil)
# File 'lib/yard/code_objects/base.rb', line 315
def line @files.first ? @files.first[1] : nil end
#name(prefix = false) ⇒ Symbol
, String
The name of the object
# File 'lib/yard/code_objects/base.rb', line 278
def name(prefix = false) prefix ? @name.to_s : (defined?(@name) && @name) end
#path ⇒ String Also known as: #to_s
Represents the unique path of the object. The default implementation joins the path of #namespace with #name via the value of #sep. Custom code objects should ensure that the path is unique to the code object by either overriding #sep or this method.
#relative_path(other) ⇒ String
# File 'lib/yard/code_objects/base.rb', line 475
def relative_path(other) other = Registry.at(other) if String === other && Registry.at(other) same_parent = false if other.respond_to?(:path) same_parent = other.parent == parent other = other.path end return other unless namespace common = [path, other].join(" ").match(/^(\S*)\S*(?: \1\S*)*$/)[1] common = path unless common =~ /(\.|::|#)$/ common = common.sub(/(\.|::|#)[^:#\.]*?$/, '') if same_parent suffix = %w(. :).include?(common[-1, 1]) || other[common.size, 1] == '#' ? '' : '(::|\.)' result = other.sub(/^#{Regexp.quote common}#{suffix}/, '') result.empty? ? other : result end
#sep ⇒ String
Override this method with a custom component separator. For instance,
MethodObject
implements sep as '#' or '.' (depending on if the
method is instance or class respectively). #path depends on this
value to generate the full path in the form: namespace.path + sep + name
# File 'lib/yard/code_objects/base.rb', line 576
def sep; NSEP end
#tag(name)
Gets a tag from the #docstring
#tags(name = nil)
Gets a list of tags from the #docstring
#title ⇒ String
Override this method if your object has a special title that does not match the #path attribute value. This title will be used when linking or displaying the object.
# File 'lib/yard/code_objects/base.rb', line 468
def title path end
#to_ary ⇒ nil
# File 'lib/yard/code_objects/base.rb', line 337
def to_ary; nil end
#to_s
Alias for #path.
# File 'lib/yard/code_objects/base.rb', line 460
alias to_s path
#translate_docstring(locale) (private)
[ GitHub ]# File 'lib/yard/code_objects/base.rb', line 606
def translate_docstring(locale) @docstring.resolve_reference return @docstring if locale.nil? text = I18n::Text.new(@docstring) localized_text = text.translate(locale) docstring = Docstring.new(localized_text, self) @docstring. .each do |tag| if tag.is_a?(Tags::Tag) localized_tag = tag.clone localized_tag.text = I18n::Text.new(tag.text).translate(locale) docstring.add_tag(localized_tag) else docstring.add_tag(tag) end end docstring end
#type ⇒ Symbol
Default type is the lowercase class name without the "Object" suffix. Override this method to provide a custom object type
# File 'lib/yard/code_objects/base.rb', line 437
def type obj_name = self.class.name.split('::').last obj_name.gsub!(/Object$/, '') obj_name.downcase! obj_name.to_sym end