Class: SleepingKingStudios::Docs::Data::NamespaceObject
- 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
Instance Method Summary collapse
-
#as_json ⇒ Hash{String => Object}
Generates a JSON-compatible representation of the namespace.
-
#class_attributes ⇒ Array<Hash>
Finds the class attributes defined for the namespace.
-
#class_methods ⇒ Array<Hash{String => String}>
Finds the class methods defined for the namespace.
-
#constants ⇒ Array<String>
Finds the names of the constants defined under this namespace.
-
#defined_classes ⇒ Array<Hash>
Finds the Classes defined under this namespace, if any.
-
#defined_modules ⇒ Array<Hash>
Finds the Modules defined under this namespace, if any.
-
#instance_attributes ⇒ Array<Hash>
Finds the instance attributes defined for the namespace.
-
#instance_methods ⇒ Array<Hash{String => String}>
Finds the instance methods defined for the namespace.
-
#name ⇒ String
The full, qualified name of the namespace.
-
#slug ⇒ String
The name of the namespace in url-safe format.
-
#type ⇒ String
The type of the namespace.
Methods inherited from Base
Constructor Details
This class inherits a constructor from SleepingKingStudios::Docs::Data::Base
Instance Method Details
#as_json ⇒ Hash{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.
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_attributes ⇒ Array<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.
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, | format_attribute(name, ) } .compact .sort_by { |hsh| hsh['name'] } end |
#class_methods ⇒ Array<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.
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 |
#constants ⇒ Array<String>
Finds the names of the constants defined under this namespace.
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_classes ⇒ Array<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.
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_modules ⇒ Array<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.
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_attributes ⇒ Array<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.
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, | format_attribute(name, ) } .compact .sort_by { |hsh| hsh['name'] } end |
#instance_methods ⇒ Array<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.
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 |
#name ⇒ String
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”.
217 218 219 |
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 217 def name @name ||= native.path end |
#slug ⇒ String
The name of the namespace in url-safe format.
224 225 226 |
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 224 def slug @slug ||= slugify(name.split('::').last || '') end |
#type ⇒ String
Returns the type of the namespace.
229 230 231 |
# File 'lib/sleeping_king_studios/docs/data/namespace_object.rb', line 229 def type 'namespace' end |