Class: SleepingKingStudios::Docs::Data::ConstantObject

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

Overview

Object representing a Ruby constant.

Each constant has a name and a value. In addition, a constant may have a short description, a full description, and metadata.

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

Returns a Hash with the following keys:

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

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

  • ‘value’: A String representation of the value of the constant.

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

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

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

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

Returns:

  • (Hash{String => Object})

    the representation of the constant.



39
40
41
42
43
44
45
46
47
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 39

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

#data_pathString

The path to the data file.

Returns:

  • (String)

    the file path.



52
53
54
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 52

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

#descriptionString

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

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

Returns:

  • (String)

    the remaining description.

See Also:



64
65
66
67
68
69
70
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 64

def description
  return @description if @description

  @short_description, @description = split_docstring

  @description
end

#metadataObject

Additional metadata tags from the documentation.

See Also:



75
76
77
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 75

def 
   ||= 
end

#nameString

The full, qualified name of the constant.

Returns:

  • (String)

    the qualified name.



82
83
84
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 82

def name
  @name ||= native.path
end

#parent_pathString

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

Returns:

  • (String)

    the file path.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 89

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

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

Returns:

  • (String)

    the short description.

See Also:



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

def short_description
  return @short_description if @short_description

  @short_description, @description = split_docstring

  @short_description
end

#slugString

The name of the constant in url-safe format.

Returns:

  • (String)

    the constant name.



124
125
126
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 124

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

#valueString

A String representation of the value of the constant.

Returns:

  • (String)

    the constant value.



131
132
133
# File 'lib/sleeping_king_studios/docs/data/constant_object.rb', line 131

def value
  native.value
end