123456789_123456789_123456789_123456789_123456789_

Class: YARD::CodeObjects::NamespaceObject

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base
Instance Chain:
self, Base
Inherits: YARD::CodeObjects::Base
Defined in: lib/yard/code_objects/namespace_object.rb

Overview

A "namespace" is any object that can store other objects within itself. The two main Ruby objects that can act as namespaces are modules (ModuleObject) and classes (ClassObject).

Class Method Summary

Base - Inherited

.===

Compares the class with subclasses.

.new

Allocates a new code object.

Instance Attribute Summary

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

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 #docstring

#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 #tags and #docstring methods with the parsed information.

#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 #docstring has a tag.

#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 #docstring

#tags

Gets a list of tags from the #docstring

#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) ⇒ NamespaceObject

Creates a new namespace object inside namespace with name.

See Also:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 56

def initialize(namespace, name, *args, &block)
  @children = CodeObjectList.new(self)
  @class_mixins = CodeObjectList.new(self)
  @instance_mixins = CodeObjectList.new(self)
  @attributes = SymbolHash[:class => SymbolHash.new, :instance => SymbolHash.new]
  @aliases = {}
  @groups = []
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class YARD::CodeObjects::Base

Instance Attribute Details

#aliasesHash (readonly)

A hash containing two keys, :class and :instance, each containing a hash of objects and their alias names.

Returns:

  • (Hash)

    a list of methods

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 44

attr_reader :aliases

#attributesHash (readonly)

A hash containing two keys, class and instance, each containing the attribute name with a { :read, :write } hash for the read and write objects respectively.

Examples:

The attributes of an object

>> Registry.at('YARD::Docstring').attributes
=> {
      :class => { },
      :instance => {
        :ref_tags => {
          :read => #<yardoc method YARD::Docstring#ref_tags>,
          :write => nil
        },
        :object => {
          :read => #<yardoc method YARD::Docstring#object>,
          :write => #<yardoc method YARD::Docstring#object=>
         },
         ...
      }
    }

Returns:

  • (Hash)

    a list of methods

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 39

attr_reader :attributes

#childrenArray<Base> (readonly)

The list of objects defined in this namespace

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 16

attr_reader :children

#class_mixinsArray<ModuleObject> (readonly)

Class mixins

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 48

attr_reader :class_mixins

#groupsArray<String> (rw)

Returns:

  • (Array<String>)

    a list of ordered group names inside the namespace

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 12

attr_accessor :groups

#instance_mixinsArray<ModuleObject> (readonly)

Instance mixins

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 52

attr_reader :instance_mixins

Instance Method Details

#child(opts = {}) ⇒ Base?

Looks for a child that matches the attributes specified by opts.

Examples:

Finds a child by name and scope

namespace.child(:name => :to_s, :scope => :instance)
# => #<yardoc method MyClass#to_s>

Returns:

  • (Base, nil)

    the first matched child object, or nil

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 86

def child(opts = {})
  if !opts.is_a?(Hash)
    children.find {|o| o.name == opts.to_sym }
  else
    opts = SymbolHash[opts]
    children.find do |obj|
      opts.each do |meth, value|
        break false unless value.is_a?(Array) ? value.include?(obj[meth]) : obj[meth] == value
      end
    end
  end
end

#class_attributesHash

Only the class attributes

Returns:

  • (Hash)

    a list of method names and their read/write objects

See Also:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 69

def class_attributes
  attributes[:class]
end

#constants(opts = {}) ⇒ Array<ConstantObject>

Returns all constants in the namespace

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :included (Boolean) — default: true

    whether or not to include mixed in constants in list

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 164

def constants(opts = {})
  opts = SymbolHash[:included => true].update(opts)
  consts = children.select {|o| o.is_a? ConstantObject }
  consts + (opts[:included] ? included_constants : [])
end

#cvarsArray<ClassVariableObject>

Returns class variables defined in this namespace.

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 186

def cvars
  children.select {|o| o.is_a? ClassVariableObject }
end

#included_constantsArray<ConstantObject>

Returns constants included from any mixins

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 172

def included_constants
  instance_mixins.inject([]) do |list, mixin|
    if mixin.respond_to? :constants
      list += mixin.constants.reject do |o|
        child(:name => o.name) || list.find {|o2| o2.name == o.name }
      end
    else
      list
    end
  end
end

#included_meths(opts = {})

Returns methods included from any mixins that match the attributes specified by opts. If no options are specified, returns all included methods.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :visibility (Array<Symbol>, Symbol) — default: [:public, :private, :protected]

    the visibility of the methods to list. Can be an array or single value.

  • :scope (Array<Symbol>, Symbol) — default: [:class, :instance]

    the scope of the methods to list. Can be an array or single value.

  • :included (Boolean) — default: true

    whether to include mixed in methods in the list.

See Also:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 144

def included_meths(opts = {})
  opts = SymbolHash[:scope => [:instance, :class]].update(opts)
  [opts[:scope]].flatten.map do |scope|
    mixins(scope).inject([]) do |list, mixin|
      next list if mixin.is_a?(Proxy)
      arr = mixin.meths(opts.merge(:scope => :instance)).reject do |o|
        next false if opts[:all]
        child(:name => o.name, :scope => scope) || list.find {|o2| o2.name == o.name }
      end
      arr.map! {|o| ExtendedMethodObject.new(o) } if scope == :class
      list + arr
    end
  end.flatten
end

#instance_attributesHash

Only the instance attributes

Returns:

  • (Hash)

    a list of method names and their read/write objects

See Also:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 76

def instance_attributes
  attributes[:instance]
end

#meths(opts = {}) ⇒ Array<MethodObject>

Returns all methods that match the attributes specified by opts. If no options are provided, returns all methods.

Examples:

Finds all private and protected class methods

namespace.meths(:visibility => [:private, :protected], :scope => :class)
# => [#<yardoc method MyClass.privmeth>, #<yardoc method MyClass.protmeth>]

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :visibility (Array<Symbol>, Symbol) — default: [:public, :private, :protected]

    the visibility of the methods to list. Can be an array or single value.

  • :scope (Array<Symbol>, Symbol) — default: [:class, :instance]

    the scope of the methods to list. Can be an array or single value.

  • :included (Boolean) — default: true

    whether to include mixed in methods in the list.

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 113

def meths(opts = {})
  opts = SymbolHash[
    :visibility => [:public, :private, :protected],
    :scope => [:class, :instance],
    :included => true
  ].update(opts)

  opts[:visibility] = [opts[:visibility]].flatten
  opts[:scope] = [opts[:scope]].flatten

  ourmeths = children.select do |o|
    o.is_a?(MethodObject) &&
      opts[:visibility].include?(o.visibility) &&
      opts[:scope].include?(o.scope)
  end

  ourmeths + (opts[:included] ? included_meths(opts) : [])
end

#mixins(*scopes) ⇒ Array<ModuleObject>

Returns for specific scopes. If no scopes are provided, returns all mixins.

Parameters:

  • scopes (Array<Symbol>)

    a list of scopes (:class, :instance) to return mixins for. If this is empty, all scopes will be returned.

Returns:

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_object.rb', line 194

def mixins(*scopes)
  return class_mixins if scopes == [:class]
  return instance_mixins if scopes == [:instance]
  class_mixins | instance_mixins
end