Class: SleepingKingStudios::Docs::Data::ClassObject

Inherits:
ModuleObject show all
Defined in:
lib/sleeping_king_studios/docs/data/class_object.rb

Overview

Object representing a Ruby class.

Each class can define the following elements:

  • Defined In files.

  • inherited classes.

  • extend-ed modules.

  • include-ed modules.

  • Definitions (classes and modules).

  • Constants.

  • Class attributes.

  • Class methods.

  • A constructor method.

  • Instance attributes.

  • Instance methods.

  • Known subclasses.

Additionally, a class can have a description and metadata tags.

Instance Method Summary collapse

Methods inherited from ModuleObject

#data_path, #description, #extended_modules, #files, #included_modules, #metadata, #parent_path, #short_description

Methods inherited from NamespaceObject

#class_attributes, #class_methods, #constants, #defined_classes, #defined_modules, #instance_attributes, #instance_methods, #name, #slug

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SleepingKingStudios::Docs::Data::Base

Instance Method Details

#as_jsonHash{String => Object}

Generates a JSON-compatible representation of the module.

Returns a Hash with the following keys:

  • ‘name’: The full, qualified name of the module.

  • ‘slug’: The name of the module in url-safe format.

  • ‘files’: A list of the files where the module is defined.

  • ‘short_description’: A short description of the module.

  • ‘constructor’: True if the class defines a constructor, otherwise false.

Additionally, the returned Hash will conditionally include the following keys, if the module defines at least one of the corresponding code objects.

  • ‘class_attributes’: The class attributes, if any.

  • ‘class_methods’: The class methods, if any.

  • ‘constants’: The constants, if any.

  • ‘defined_classes’: The defined Classes, if any.

  • ‘defined_modules’: The defined Modules, if any.

  • ‘description’: The full description of the module, minus the first clause.

  • ‘extended_modules’: A list of the modules that extend the module.

  • ‘included_modules’: A list of the modules that are included in the module.

  • ‘instance_attributes’: The instance attributes, if any.

  • ‘instance_methods’: The instance methods, if any.

  • ‘metadata’: Additional metadata tags from the documentation.

Returns:

  • (Hash{String => Object})

    the representation of the module.



63
64
65
66
67
68
69
70
71
72
# File 'lib/sleeping_king_studios/docs/data/class_object.rb', line 63

def as_json
  JSON_PROPERTIES.reduce(super.merge('constructor' => constructor?)) \
  do |memo, property_name|
    value = send(property_name)

    next memo if empty?(value)

    memo.update(property_name.to_s => value)
  end
end

#constructor?Boolean

Returns true if the class defines a constructor, otherwise false.

Returns:

  • (Boolean)

    true if the class defines a constructor, otherwise false.



76
77
78
# File 'lib/sleeping_king_studios/docs/data/class_object.rb', line 76

def constructor?
  native.meths.any? { |meth| meth.name == :initialize }
end

#direct_subclassesArray<Hash{String => String}>

A list of the direct subclasses of the class.

For each subclass, it returns a Hash with the following keys:

  • ‘name’: The name of the inherited class.

  • ‘slug’: A url-safe, hyphen-separated representation of the name.

  • ‘path’: The path to the data file for the class.

Returns:

  • (Array<Hash{String => String}>)

    the direct subclasses.



89
90
91
92
93
94
95
96
# File 'lib/sleeping_king_studios/docs/data/class_object.rb', line 89

def direct_subclasses
  registry
    .select do |obj|
      obj.type == :class && obj.inheritance_tree[1] == native
    end
    .map { |obj| format_inclusion(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#inherited_classesArray<Hash{String => String}>

A list of the classes that are inherited by the class.

For each inherited Class, it returns a Hash with the following keys:

  • ‘name’: The name of the inherited class.

  • ‘slug’: A url-safe, hyphen-separated representation of the name.

  • ‘path’: The path to the data file for the class.

Returns:

  • (Array<Hash{String => String}>)

    the inherited classes.



107
108
109
110
111
112
# File 'lib/sleeping_king_studios/docs/data/class_object.rb', line 107

def inherited_classes
  @inherited_classes ||=
    native
    .inheritance_tree[1..]
    .map { |obj| format_inclusion(obj) }
end

#typeString

Returns the type of the namespace.

Returns:

  • (String)

    the type of the namespace.



115
116
117
# File 'lib/sleeping_king_studios/docs/data/class_object.rb', line 115

def type
  'class'
end