Class: YARD::CodeObjects::ClassObject
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
NamespaceObject,
Base
|
|
|
Instance Chain:
self,
NamespaceObject,
Base
|
|
| Inherits: |
YARD::CodeObjects::NamespaceObject
|
| Defined in: | lib/yard/code_objects/class_object.rb |
Overview
A ClassObject represents a Ruby class in source code. It is a ModuleObject
with extra inheritance semantics through the superclass.
Class Method Summary
-
.new(namespace, name, *args, &block) ⇒ ClassObject
constructor
Creates a new class object in
namespacewithname
NamespaceObject - Inherited
| .new | Creates a new namespace object inside |
Base - Inherited
Instance Attribute Summary
-
#is_exception? ⇒ Boolean
readonly
Whether or not the class is a Ruby Exception.
-
#superclass ⇒ ClassObject
rw
The
ClassObjectthat this class object inherits from in Ruby source. -
#superclass=(object) ⇒ void
rw
Sets the superclass of the object.
NamespaceObject - Inherited
| #aliases | A hash containing two keys, |
| #attributes | A hash containing two keys, class and instance, each containing the attribute name with a { |
| #children | The list of objects defined in this namespace. |
| #class_mixins | Class mixins. |
| #groups, | |
| #instance_mixins | Instance mixins. |
Base - Inherited
| #base_docstring | The non-localized documentation string associated with the object. |
| #dynamic | Marks whether or not the method is conditionally defined at runtime. |
| #dynamic? | Is the object defined conditionally at runtime? |
| #files | The files the object was defined in. |
| #group, | |
| #namespace | The namespace the object is defined in. |
| #namespace= | Sets the namespace the object is defined in. |
| #parent | Alias for Base#namespace. |
| #root?, | |
| #signature | The one line signature representing an object. |
| #source | The source code associated with the object. |
| #source= | Attaches source code to a code object with an optional file location. |
| #source_type | Language of the source code associated with the object. |
| #visibility, #visibility= | |
Instance Method Summary
-
#constants(opts = {}) ⇒ Array<ConstantObject>
Returns the list of constants matching the options hash.
-
#inheritance_tree(include_mods = false) ⇒ Array<NamespaceObject>
Returns the inheritance tree of the object including self.
-
#inherited_constants ⇒ Array<ConstantObject>
Returns only the constants that were inherited.
-
#inherited_meths(opts = {}) ⇒ Array<MethodObject>
Returns only the methods that were inherited.
-
#meths(opts = {}) ⇒ Array<MethodObject>
Returns the list of methods matching the options hash.
NamespaceObject - Inherited
| #child | Looks for a child that matches the attributes specified by |
| #class_attributes | Only the class attributes. |
| #constants | Returns all constants in the namespace. |
| #cvars | Returns class variables defined in this namespace. |
| #included_constants | Returns constants included from any mixins. |
| #included_meths | Returns methods included from any mixins that match the attributes specified by |
| #instance_attributes | Only the instance attributes. |
| #meths | Returns all methods that match the attributes specified by |
| #mixins | Returns for specific scopes. |
Base - Inherited
| #== | Alias for Base#equal?. |
| #[] | Accesses a custom attribute on the object. |
| #[]= | Sets a custom attribute on the object. |
| #add_file | Associates a file with a code object, optionally adding the line where it was defined. |
| #add_tag | Add tags to the |
| #copy_to | Copies all data in this object to another code object, except for uniquely identifying information (path, namespace, name, scope). |
| #docstring | The documentation string associated with the object. |
| #docstring= | Attaches a docstring to a code object by parsing the comments attached to the statement and filling the |
| #eql? | Alias for Base#equal?. |
| #equal? | Tests if another object is equal to this, including a proxy. |
| #file | Returns the filename the object was first parsed at, taking definitions with docstrings first. |
| #format | Renders the object using the templating system. |
| #has_tag? | Tests if the |
| #hash, | |
| #initialize | Creates a new code object. |
| #inspect | Inspects the object, returning the type and path. |
| #line | Returns the line the object was first parsed at (or nil). |
| #method_missing, | |
| #name | The name of the object. |
| #path | Represents the unique path of the object. |
| #relative_path, | |
| #sep | Override this method with a custom component separator. |
| #tag | Gets a tag from the |
| #tags | Gets a list of tags from the |
| #title, #to_ary, | |
| #to_s | Alias for Base#path. |
| #type | Default type is the lowercase class name without the "Object" suffix. |
| #format_source | Formats source code by removing leading indentation. |
| #translate_docstring | |
Constructor Details
.new(namespace, name, *args, &block) ⇒ ClassObject
Creates a new class object in namespace with name
# File 'lib/yard/code_objects/class_object.rb', line 15
def initialize(namespace, name, *args, &block) super if is_exception? self.superclass ||= "::Exception" unless P(namespace, name) == P(:Exception) else case P(namespace, name).path when "BasicObject" nil when "Object" self.superclass ||= "::BasicObject" else self.superclass ||= "::Object" end end end
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class YARD::CodeObjects::Base
Instance Attribute Details
#is_exception? ⇒ Boolean (readonly)
Whether or not the class is a Ruby Exception
# File 'lib/yard/code_objects/class_object.rb', line 35
def is_exception? inheritance_tree.reverse.any? {|o| BUILTIN_EXCEPTIONS_HASH.key? o.path } end
#superclass ⇒ ClassObject (rw)
The ClassObject that this class object inherits from in Ruby source.
# File 'lib/yard/code_objects/class_object.rb', line 10
attr_reader :superclass
#superclass=(object) ⇒ void (rw)
This method returns an undefined value.
Sets the superclass of the object
# File 'lib/yard/code_objects/class_object.rb', line 125
def superclass=(object) case object when Base, Proxy, NilClass @superclass = object when String, Symbol @superclass = Proxy.new(namespace, object) else raise ArgumentError, "superclass must be CodeObject, Proxy, String or Symbol" end if name == @superclass.name && namespace != YARD::Registry.root && !object.is_a?(Base) @superclass = Proxy.new(namespace.namespace, object) end if @superclass == self msg = "superclass #{@superclass.inspect} cannot be the same as the declared class #{inspect}" @superclass = P("::Object") raise ArgumentError, msg end end
Instance Method Details
#constants(opts = {}) ⇒ Array<ConstantObject>
Returns the list of constants matching the options hash.
# File 'lib/yard/code_objects/class_object.rb', line 101
def constants(opts = {}) opts = SymbolHash[:inherited => true].update(opts) super(opts) + (opts[:inherited] ? inherited_constants : []) end
#inheritance_tree(include_mods = false) ⇒ Array<NamespaceObject>
Returns the inheritance tree of the object including self.
# File 'lib/yard/code_objects/class_object.rb', line 45
def inheritance_tree(include_mods = false) list = (include_mods ? mixins(:instance, :class) : []) if superclass.is_a?(Proxy) || superclass.respond_to?(:inheritance_tree) list += [superclass] unless superclass == P(:Object) || superclass == P(:BasicObject) end [self] + list.map do |m| next m if m == self next m unless m.respond_to?(:inheritance_tree) m.inheritance_tree(include_mods) end.flatten.uniq end
#inherited_constants ⇒ Array<ConstantObject>
Returns only the constants that were inherited.
# File 'lib/yard/code_objects/class_object.rb', line 109
def inherited_constants inheritance_tree[1..-1].inject([]) do |list, superclass| if superclass.is_a?(Proxy) list else list += superclass.constants.reject do |o| child(:name => o.name) || list.find {|o2| o2.name == o.name } end end end end
#inherited_meths(opts = {}) ⇒ Array<MethodObject>
Returns only the methods that were inherited.
# File 'lib/yard/code_objects/class_object.rb', line 79
def inherited_meths(opts = {}) inheritance_tree[1..-1].inject([]) do |list, superclass| if superclass.is_a?(Proxy) list else list += superclass.meths(opts).reject do |o| next(false) if opts[:all] child(:name => o.name, :scope => o.scope) || list.find {|o2| o2.name == o.name && o2.scope == o.scope } end end end end
#meths(opts = {}) ⇒ Array<MethodObject>
Returns the list of methods matching the options hash. Returns all methods if hash is empty.
# File 'lib/yard/code_objects/class_object.rb', line 66
def meths(opts = {}) opts = SymbolHash[:inherited => true].update(opts) list = super(opts) list += inherited_meths(opts).reject do |o| next(false) if opts[:all] list.find {|o2| o2.name == o.name && o2.scope == o.scope } end if opts[:inherited] list end