Class: SleepingKingStudios::Docs::Data::ModuleObject

Inherits:
NamespaceObject show all
Defined in:
lib/sleeping_king_studios/docs/data/module_object.rb

Overview

Object representing a Ruby module.

Each module can define the following elements:

  • Defined In files.

  • extend-ed modules.

  • include-ed modules.

  • Definitions (classes and modules).

  • Constants.

  • Class attributes.

  • Class methods.

  • Instance attributes.

  • Instance methods.

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

Direct Known Subclasses

ClassObject

Instance Method Summary collapse

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.

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.



66
67
68
69
70
71
72
73
74
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 66

def as_json
  JSON_PROPERTIES.reduce(super) do |memo, property_name|
    value = send(property_name)

    next memo if empty?(value)

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

#data_pathString

The path to the data file.

Returns:

  • (String)

    the file path.



79
80
81
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 79

def data_path
  @data_path ||= name.split('::').map { |str| slugify(str) }.join('/')
end

#descriptionString

The full description of the module, minus the first clause.

The remainder of the module description, if any, after subtracting the short description (separated by the first paragraph break).

Returns:

  • (String)

    the remaining description.

See Also:



91
92
93
94
95
96
97
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 91

def description
  return @description if @description

  @short_description, @description = split_docstring

  @description
end

#extended_modulesArray<Hash{String => String}>

A list of the modules that extend the original module.

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

  • ‘name’: The name of the extending module.

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

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

Returns:

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

    the extended modules.



108
109
110
111
112
113
114
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 108

def extended_modules
  @extended_modules ||=
    native
    .class_mixins
    .map { |obj| format_inclusion(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#filesArray<String>

A list of the files where the module is defined.

Returns:

  • (Array<String>)

    the list of files.



119
120
121
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 119

def files
  @files ||= native.files.map(&:first)
end

#included_modulesArray<Hash{String => String}>

A list of the modules that are included in the original module.

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

  • ‘name’: The name of the included module.

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

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

Returns:

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

    the included modules.



132
133
134
135
136
137
138
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 132

def included_modules
  @included_modules ||=
    native
    .instance_mixins
    .map { |obj| format_inclusion(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#metadataObject

Additional metadata tags from the documentation.



143
144
145
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 143

def 
  @metadata ||= 
end

#parent_pathString

The path to the defining class or module’s data file.

Returns:

  • (String)

    the file path.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 150

def parent_path
  return @parent_path if @parent_path

  return @parent_path = '' if native.parent.root?

  parent_class  =
    if native.parent.is_a?(YARD::CodeObjects::ClassObject)
      SleepingKingStudios::Docs::Data::ClassObject
    else
      SleepingKingStudios::Docs::Data::ModuleObject
    end
  parent_object = parent_class.new(native: native.parent)

  @parent_path = parent_object.data_path
end

#short_descriptionString

A short description of the module.

The first part of the module description, separated by the first paragraph break. Typically should fit on a single line of text.

Returns:

  • (String)

    the short description.

See Also:



174
175
176
177
178
179
180
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 174

def short_description
  return @short_description if @short_description

  @short_description, @description = split_docstring

  @short_description
end

#typeString

Returns the type of the namespace.

Returns:

  • (String)

    the type of the namespace.



183
184
185
# File 'lib/sleeping_king_studios/docs/data/module_object.rb', line 183

def type
  'module'
end