Class: YARD::CodeObjects::Base Abstract
- Inherits:
-
Object
- Object
- YARD::CodeObjects::Base
- 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 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.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Docstring) docstring
The documentation string associated with the object.
-
- (Boolean) dynamic
Marks whether or not the method is conditionally defined at runtime.
-
- (Array<String>) files
readonly
The files the object was defined in.
-
- (String) group
The group this object is associated with.
-
- (NamespaceObject) namespace
(also: #parent)
The namespace the object is defined in.
-
- (String) signature
The one line signature representing an object.
-
- (String?) source
The source code associated with the object.
-
- (Symbol) source_type
Language of the source code associated with the object.
Class Method Summary (collapse)
-
+ (Boolean) ===(other)
Compares the class with subclasses.
-
+ (Base) new(namespace, name, *args) {|obj| ... }
Allocates a new code object.
Instance Method Summary (collapse)
-
- (Object?) [](key)
Accesses a custom attribute on the object.
-
- (void) []=(key, value)
Sets a custom attribute on the object.
-
- (Object) add_file(file, line = nil, has_comments = false)
Associates a file with a code object, optionally adding the line where it was defined.
-
- (Boolean) dynamic?
Is the object defined conditionally at runtime?.
-
- (Boolean) equal?(other)
(also: #==, #eql?)
Tests if another object is equal to this, including a proxy.
-
- (String) file
Returns the filename the object was first parsed at, taking definitions with docstrings first.
-
- (String) format(options = {})
Renders the object using the templating system.
-
- (String) format_source(source)
protected
Formats source code by removing leading indentation.
-
- (Boolean) has_tag?(name)
Tests if the #docstring has a tag.
-
- (Integer) hash
The object's hash value (for equality checking).
-
- (Base) initialize(namespace, name, *args) {|self| ... }
constructor
Creates a new code object.
-
- (String) inspect
Inspects the object, returning the type and path.
-
- (Fixnum?) line
Returns the line the object was first parsed at (or nil).
- - (Object) method_missing(meth, *args, &block)
-
- (Symbol, String) name(prefix = false)
The name of the object.
-
- (String) path
(also: #to_s)
Represents the unique path of the object.
-
- (String) relative_path(other)
The shortest relative path from this object to other.
-
- (Boolean) root?
Whether or not this object is a RootObject.
-
- (String) sep
protected
Override this method with a custom component separator.
-
- (Object) tag(name)
Gets a tag from the #docstring.
-
- (Object) tags(name = nil)
Gets a list of tags from the #docstring.
-
- (Symbol) type
Default type is the lowercase class name without the "Object" suffix.
-
- (Symbol) visibility
This attribute exists in order to maintain a consistent interface with the MethodObject class, so that a Verifier expression need not check the object type before accessing visibility.
Constructor Details
- (Base) initialize(namespace, name, *args) {|self| ... }
Creates a new code object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/yard/code_objects/base.rb', line 205 def initialize(namespace, name, *args, &block) 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 @tags = [] @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
- (Object) dynamic_attr_name - (Object) dynamic_attr_name=(value)
319 320 321 322 323 324 325 326 327 |
# File 'lib/yard/code_objects/base.rb', line 319 def method_missing(meth, *args, &block) if meth.to_s =~ /=$/ self[meth.to_s[0..-2]] = args.first elsif instance_variable_get("@#{meth}") self[meth] else super end end |
Instance Attribute Details
- (Docstring) docstring
The documentation string associated with the object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/yard/code_objects/base.rb', line 141 def docstring value = case @docstring when Proxy Docstring.new("", self) when Base @docstring.docstring else @docstring end @docstring_extra ? value + @docstring_extra : value end |
- (Boolean) dynamic
Marks whether or not the method is conditionally defined at runtime
145 146 147 |
# File 'lib/yard/code_objects/base.rb', line 145 def dynamic @dynamic end |
- (Array<String>) files (readonly)
The files the object was defined in. To add a file, use #add_file.
115 116 117 |
# File 'lib/yard/code_objects/base.rb', line 115 def files @files end |
- (String) group
The group this object is associated with
149 150 151 |
# File 'lib/yard/code_objects/base.rb', line 149 def group @group end |
- (NamespaceObject) namespace Also known as: parent
The namespace the object is defined in. If the object is in the top level namespace, this is Registry.root
120 121 122 |
# File 'lib/yard/code_objects/base.rb', line 120 def namespace @namespace end |
- (String) signature
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.
137 138 139 |
# File 'lib/yard/code_objects/base.rb', line 137 def signature @signature end |
- (String?) source
The source code associated with the object
124 125 126 |
# File 'lib/yard/code_objects/base.rb', line 124 def source @source end |
- (Symbol) source_type
Language of the source code associated with the object. Defaults to :ruby.
130 131 132 |
# File 'lib/yard/code_objects/base.rb', line 130 def source_type @source_type end |
Class Method Details
+ (Boolean) ===(other)
Compares the class with subclasses
186 187 188 |
# File 'lib/yard/code_objects/base.rb', line 186 def ===(other) other.is_a?(self) end |
+ (Base) new(namespace, name, *args) {|obj| ... }
Allocates a new code object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/yard/code_objects/base.rb', line 166 def new(namespace, name, *args, &block) raise ArgumentError, "invalid empty object name" if name.to_s.empty? if name.to_s[0,2] == NSEP name = name.to_s[2..-1] namespace = Registry.root elsif name =~ /(?:#{NSEPQ}|#{ISEPQ}|#{CSEPQ})([^#{NSEPQ}#{ISEPQ}#{CSEPQ}]+)$/ 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 |
Instance Method Details
- (Object?) [](key)
Accesses a custom attribute on the object
289 290 291 292 293 294 295 |
# File 'lib/yard/code_objects/base.rb', line 289 def [](key) if respond_to?(key) send(key) elsif instance_variable_defined?("@#{key}") instance_variable_get("@#{key}") end end |
- (void) []=(key, value)
This method returns an undefined value.
Sets a custom attribute on the object
302 303 304 305 306 307 308 |
# File 'lib/yard/code_objects/base.rb', line 302 def []=(key, value) if respond_to?("#{key}=") send("#{key}=", value) else instance_variable_set("@#{key}", value) end end |
- (Object) 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, '<stdin>' should be used to associate code that comes form standard input.
240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/yard/code_objects/base.rb', line 240 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 |
- (Boolean) dynamic?
Is the object defined conditionally at runtime?
153 |
# File 'lib/yard/code_objects/base.rb', line 153 def dynamic?; @dynamic end |
- (Boolean) equal?(other) Also known as: ==, eql?
Tests if another object is equal to this, including a proxy
272 273 274 275 276 277 278 |
# File 'lib/yard/code_objects/base.rb', line 272 def equal?(other) if other.is_a?(Base) || other.is_a?(Proxy) path == other.path else super end end |
- (String) file
Returns the filename the object was first parsed at, taking definitions with docstrings first.
256 257 258 |
# File 'lib/yard/code_objects/base.rb', line 256 def file @files.first ? @files.first[0] : nil end |
- (String) format(options = {})
Renders the object using the templating system.
421 422 423 424 |
# File 'lib/yard/code_objects/base.rb', line 421 def format( = {}) .merge!(:object => self) Templates::Engine.render() end |
- (String) format_source(source) (protected)
Formats source code by removing leading indentation
486 487 488 489 490 491 |
# File 'lib/yard/code_objects/base.rb', line 486 def format_source(source) source.chomp! last = source.split(/\r?\n/).last indent = last ? last[/^([ \t]*)/, 1].length : 0 source.gsub(/^[ \t]{#{indent}}/, '') end |
- (Boolean) has_tag?(name)
Tests if the #docstring has a tag
466 |
# File 'lib/yard/code_objects/base.rb', line 466 def has_tag?(name); docstring.has_tag?(name) end |
- (Integer) hash
The object's hash value (for equality checking)
283 |
# File 'lib/yard/code_objects/base.rb', line 283 def hash; path.hash end |
- (String) inspect
Inspects the object, returning the type and path
428 429 430 |
# File 'lib/yard/code_objects/base.rb', line 428 def inspect "#<yardoc #{type} #{path}>" end |
- (Fixnum?) line
Returns the line the object was first parsed at (or nil)
264 265 266 |
# File 'lib/yard/code_objects/base.rb', line 264 def line @files.first ? @files.first[1] : nil end |
- (Symbol, String) name(prefix = false)
The name of the object
228 229 230 |
# File 'lib/yard/code_objects/base.rb', line 228 def name(prefix = false) prefix ? @name.to_s : @name end |
- (String) path 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.
390 391 392 393 394 395 396 |
# File 'lib/yard/code_objects/base.rb', line 390 def path if parent && !parent.root? [parent.path, name.to_s].join(sep) else name.to_s end end |
- (String) relative_path(other)
The shortest relative path from this object to other
402 403 404 405 406 |
# File 'lib/yard/code_objects/base.rb', line 402 def relative_path(other) other = other.path if other.respond_to?(:path) return other unless namespace other.gsub(/^#{Regexp.quote namespace.path}(::|\.)?/, '') end |
- (Boolean) root?
Whether or not this object is a RootObject
469 |
# File 'lib/yard/code_objects/base.rb', line 469 def root?; false end |
- (String) sep (protected)
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
480 |
# File 'lib/yard/code_objects/base.rb', line 480 def sep; NSEP end |
- (Object) tag(name)
Gets a tag from the #docstring
458 |
# File 'lib/yard/code_objects/base.rb', line 458 def tag(name); docstring.tag(name) end |
- (Object) tags(name = nil)
Gets a list of tags from the #docstring
462 |
# File 'lib/yard/code_objects/base.rb', line 462 def (name = nil); docstring.(name) end |
- (Symbol) type
Default type is the lowercase class name without the "Object" suffix. Override this method to provide a custom object type
377 378 379 |
# File 'lib/yard/code_objects/base.rb', line 377 def type self.class.name.split(/#{NSEPQ}/).last.gsub(/Object$/, '').downcase.to_sym end |
- (Symbol) visibility
This attribute exists in order to maintain a consistent interface with the MethodObject class, so that a Verifier expression need not check the object type before accessing visibility.
160 |
# File 'lib/yard/code_objects/base.rb', line 160 def visibility; :public end |