Class: YARD::CodeObjects::NamespaceObject

Inherits:
Base
  • Object
show all
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).

Direct Known Subclasses

ClassObject, ModuleObject

Instance Attribute Summary (collapse)

Attributes inherited from Base

#docstring, #dynamic, #files, #group, #namespace, #signature, #source, #source_type, #visibility

Instance Method Summary (collapse)

Methods inherited from Base

===, #[], #[]=, #add_file, #dynamic?, #equal?, #file, #format, #format_source, #has_tag?, #hash, #inspect, #line, #method_missing, #name, new, #path, #relative_path, #root?, #sep, #tag, #tags, #to_ary, #type

Constructor Details

- (NamespaceObject) initialize(namespace, name, *args, &block)

Creates a new namespace object inside namespace with name.

See Also:



56
57
58
59
60
61
62
63
64
# 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

- (Hash) aliases (readonly)

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



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

def aliases
  @aliases
end

- (Hash) attributes (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=>
         },
         ...
      }
    }


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

def attributes
  @attributes
end

- (Base?) child(opts = {})

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>


86
87
88
89
90
91
92
93
94
95
96
97
# 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 if !(value.is_a?(Array) ? value.include?(obj[meth]) : obj[meth] == value)
      end
    end
  end
end

- (Array<Base>) children (readonly)

The list of objects defined in this namespace



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

def children
  @children
end

- (Hash) class_attributes

Only the class attributes

See Also:



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

def class_attributes
  attributes[:class]
end

- (Array<ModuleObject>) class_mixins (readonly)

Class mixins



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

def class_mixins
  @class_mixins
end

- (Array<ConstantObject>) constants(opts = {})

Returns all constants in the namespace

Options Hash (opts):

  • :included (Boolean) — default: true

    whether or not to include mixed in constants in list



164
165
166
167
168
# 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

- (Array<ClassVariableObject>) cvars

Returns class variables defined in this namespace.



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

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

- (Array<String>) groups

Returns a list of ordered group names inside the namespace

Since:

  • 0.6.0



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

def groups
  @groups
end

- (Array<ConstantObject>) included_constants

Returns constants included from any mixins



172
173
174
175
176
177
178
179
180
181
182
# 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

- (Object) 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.

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:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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

- (Hash) instance_attributes

Only the instance attributes

See Also:



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

def instance_attributes
  attributes[:instance]
end

- (Array<ModuleObject>) instance_mixins (readonly)

Instance mixins



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

def instance_mixins
  @instance_mixins
end

- (Array<MethodObject>) meths(opts = {})

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>]

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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

- (Array<ModuleObject>) mixins(*scopes)

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



194
195
196
197
198
# 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