# Class: YARD::CodeObjects::ClassObject

Inherits:
  NamespaceObject
  - Object
  - Base
  - NamespaceObject
  - YARD::CodeObjects::ClassObject
  show all

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.

## Instance Attribute Summary collapse

- #superclass ⇒ ClassObject

  The ClassObject that this class object inherits from in Ruby source.

## Instance Method Summary collapse

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

- #initialize(namespace, name, *args, &block) ⇒ ClassObject constructor

  Creates a new class object in `namespace` with `name`.

- #is_exception? ⇒ Boolean

  Whether or not the class is a Ruby Exception.

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

  Returns the list of methods matching the options hash.

## Constructor Details

### #initialize(namespace, name, *args, &block) ⇒ `ClassObject`

Creates a new class object in `namespace` with `name`

See Also:

- Base.new

    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30

    # 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

### #superclass ⇒ `ClassObject`

The YARD::CodeObjects::ClassObject that this class object inherits from in Ruby source.

Returns:

- (`ClassObject`) —

  a class object that is the superclass of this one

    10
    11
    12

    # File 'lib/yard/code_objects/class_object.rb', line 10

    def superclass
      @superclass
    end

## Instance Method Details

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

Returns the list of constants matching the options hash.

Parameters:

- opts (`Hash`) (defaults to: `{}`) —

  the options hash to match

Options Hash (`opts`):

- :inherited (`Boolean`) — default: `true` —

  whether inherited constant should be included in the list

- :included (`Boolean`) — default: `true` —

  whether mixed in constant should be included in the list

Returns:

- (`Array<ConstantObject>`) —

  the list of constant that matched

    101
    102
    103
    104

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

Parameters:

- include_mods (`Boolean`) (defaults to: `false`) —

  whether or not to include mixins in the inheritance tree.

Returns:

- (`Array<NamespaceObject>`) —

  the list of code objects that make up the inheritance tree.

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55

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

Returns:

- (`Array<ConstantObject>`) —

  the list of inherited constant objects

    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119

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

Returns:

- (`Array<MethodObject>`) —

  the list of inherited method objects

    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91

    # 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

### #is_exception? ⇒ `Boolean`

Whether or not the class is a Ruby Exception

Returns:

- (`Boolean`) —

  whether the object represents a Ruby exception

    35
    36
    37

    # 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

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

Returns the list of methods matching the options hash. Returns all methods if hash is empty.

Parameters:

- opts (`Hash`) (defaults to: `{}`) —

  the options hash to match

Options Hash (`opts`):

- :inherited (`Boolean`) — default: `true` —

  whether inherited methods should be included in the list

- :included (`Boolean`) — default: `true` —

  whether mixed in methods should be included in the list

Returns:

- (`Array<MethodObject>`) —

  the list of methods that matched

    66
    67
    68
    69
    70
    71
    72
    73
    74

    # 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