Class: SleepingKingStudios::Docs::Data::NamespaceObject

Inherits:
Base
  • Object
show all
Defined in:
lib/sleeping_king_studios/docs/data/namespace_object.rb

Overview

Object representing a Ruby namespace.

Each namespace can define the following elements:

  • Definitions (classes and modules).

  • Constants.

  • Class attributes.

  • Class methods.

  • Instance attributes.

  • Instance methods.

In most cases, there should be one documented namespace, reflecting the top-level namespace, and in most cases should only include defined classes and/or modules. Top-level constants may be required on a case-by-case basis.

All other namespace properties (attributes and methods) should be reserved for classes and modules, whose representation inherits from NamespaceObject.

Direct Known Subclasses

ModuleObject, RootObject

Instance Method Summary collapse

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

Returns a Hash with the following keys:

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

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

Additionally, the returned Hash will conditionally include the following keys, if the namespace 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.

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

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

Returns:

  • (Hash{String => Object})

    the representation of the namespace.



61
62
63
64
65
66
67
68
69
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 61

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

    next memo if empty?(value)

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

#class_attributesArray<Hash>

Finds the class attributes defined for the namespace.

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

  • ‘name’: The name of the attribute.

  • ‘read’: True if the attribute defines a reader method.

  • ‘write’: True if the attribute defines a writer method.

  • ‘path’: The path to the reader method data file, or the writer method data file if the attribute does not define a reader method.

Returns:

  • (Array<Hash>)

    the class attributes.



82
83
84
85
86
87
88
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 82

def class_attributes
  @class_attributes ||=
    find_class_attributes(native)
    .map { |name, options| format_attribute(name, options) }
    .compact
    .sort_by { |hsh| hsh['name'] }
end

#class_methodsArray<Hash{String => String}>

Finds the class methods defined for the namespace.

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

  • ‘name’: The name of the method, including trailing characters such as ‘=’ or ‘?’.

  • ‘path’: The path to the method data file.

Returns:

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

    the documented class methods.



99
100
101
102
103
104
105
106
107
108
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 99

def class_methods # rubocop:disable Metrics/CyclomaticComplexity
  @class_methods ||=
    native
    .meths
    .select { |obj| obj.scope == :class && !obj.is_attribute? }
    .reject { |obj| private_method?(obj) }
    .reject(&:is_alias?)
    .map { |obj| format_method(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#constantsArray<String>

Finds the names of the constants defined under this namespace.

Returns:

  • (Array<String>)

    the names of the constants.



113
114
115
116
117
118
119
120
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 113

def constants
  @constants ||=
    native
    .constants
    .reject { |obj| private_constant?(obj) }
    .map { |obj| format_constant(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#defined_classesArray<Hash>

Finds the Classes defined under this namespace, if any.

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

  • ‘name’: The name of the defined Class.

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

Examples:

# Given a class LaunchWindow in the namespace Space::Operations:
namespace.name
#=> 'Space::Operations'
namespace.defined_classes
#=> [{ 'name' => 'LaunchWindow', 'slug' => 'launch-window' }]

Returns:

  • (Array<Hash>)

    the defined classes.



137
138
139
140
141
142
143
144
145
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 137

def defined_classes
  @defined_classes ||=
    native
    .children
    .select { |obj| obj.type == :class }
    .reject { |obj| private_definition?(obj) }
    .map { |obj| format_definition(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#defined_modulesArray<Hash>

Finds the Modules defined under this namespace, if any.

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

  • ‘name’: The name of the defined Module.

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

Examples:

# Given a class FuelConsumer in the namespace Space::Engineering:
namespace.name
#=> 'Space::Engineering'
namespace.defined_classes
#=> [{ 'name' => 'FuelConsumer', 'slug' => 'fuel-consumer' }]

Returns:

  • (Array<Hash>)

    the defined modules.



162
163
164
165
166
167
168
169
170
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 162

def defined_modules
  @defined_modules ||=
    native
    .children
    .select { |obj| obj.type == :module }
    .reject { |obj| private_definition?(obj) }
    .map { |obj| format_definition(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#instance_attributesArray<Hash>

Finds the instance attributes defined for the namespace.

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

  • ‘name’: The name of the attribute.

  • ‘read’: True if the attribute defines a reader method.

  • ‘write’: True if the attribute defines a writer method.

  • ‘path’: The path to the reader method data file, or the writer method data file if the attribute does not define a reader method.

Returns:

  • (Array<Hash>)

    the instance attributes.



183
184
185
186
187
188
189
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 183

def instance_attributes
  @instance_attributes ||=
    find_instance_attributes(native)
    .map { |name, options| format_attribute(name, options) }
    .compact
    .sort_by { |hsh| hsh['name'] }
end

#instance_methodsArray<Hash{String => String}>

Finds the instance methods defined for the namespace.

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

  • ‘name’: The name of the method, including trailing characters such as ‘=’ or ‘?’.

  • ‘path’: The path to the method data file.

Returns:

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

    the documented instance methods.



200
201
202
203
204
205
206
207
208
209
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 200

def instance_methods # rubocop:disable Metrics/CyclomaticComplexity
  @instance_methods ||=
    native
    .meths
    .select { |obj| obj.scope == :instance && !obj.is_attribute? }
    .reject { |obj| private_method?(obj) }
    .reject(&:is_alias?)
    .map { |obj| format_method(obj) }
    .sort_by { |hsh| hsh['name'] }
end

#nameString

The full, qualified name of the namespace.

For the root namespace, should return an empty string. For a Class or a Module, should return the full name, e.g. “MyGem::MyModule::MyClass”.

Returns:

  • (String)

    the qualified name.



217
218
219
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 217

def name
  @name ||= native.path
end

#slugString

The name of the namespace in url-safe format.

Returns:

  • (String)

    the namespace name.



224
225
226
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 224

def slug
  @slug ||= slugify(name.split('::').last || '')
end

#typeString

Returns the type of the namespace.

Returns:

  • (String)

    the type of the namespace.



229
230
231
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 229

def type
  'namespace'
end