class Decode::Definition

A symbol with attached documentation.

Attributes

comments[R]

The comment lines which directly preceeded the definition. @attribute [Array(String)]

language[R]

The language the symbol is defined within. @attribute [Language::Generic]

name[R]

The symbol name. e.g. `:Decode`. @attribute [Symbol]

parent[R]

The parent definition, defining lexical scope. @attribute [Definition | Nil]

Public Class Methods

new(name, parent: nil, language: parent.language, comments: nil) click to toggle source

Initialize the symbol. @parameter name [Symbol] The name of the definition. @parameter parent [Symbol] The parent lexical scope. @parameter language [Language] The language in which the symbol is defined in. @parameter comments [Array(String)] The comments associated with the definition.

# File lib/decode/definition.rb, line 29
def initialize(name, parent: nil, language: parent.language, comments: nil)
        @name = name
        
        @parent = parent
        @language = language
        
        @comments = comments
        
        @path = nil
        @qualified_name = nil
end

Public Instance Methods

container?() click to toggle source

Whether this definition can contain nested definitions.

@returns [Boolean]

# File lib/decode/definition.rb, line 148
def container?
        false
end
convert(kind) click to toggle source

Convert this definition into another kind of definition.

# File lib/decode/definition.rb, line 87
def convert(kind)
        raise ArgumentError, "Unable to convert #{self} into #{kind}!"
end
documentation() click to toggle source

Structured access to the definitions comments.

@returns [Documentation | Nil] A {Documentation} instance if this definition has comments.

# File lib/decode/definition.rb, line 162
def documentation
        if @comments&.any?
                @documentation ||= Documentation.new(@comments, @language)
        end
end
lexical_path()
Alias for: path
long_form() click to toggle source

A long form of the definition. e.g. `def initialize(kind, name, comments, **options)`.

@returns [String | nil]

# File lib/decode/definition.rb, line 120
def long_form
        self.short_form
end
multiline?() click to toggle source

Whether the definition spans multiple lines.

@returns [Boolean]

# File lib/decode/definition.rb, line 135
def multiline?
        false
end
nested?() click to toggle source

Whether this represents a single entity to be documented (along with it's contents).

@returns [Boolean]

# File lib/decode/definition.rb, line 155
def nested?
        container?
end
nested_name() click to toggle source

The name of this definition plus the nesting prefix. @returns [String]

# File lib/decode/definition.rb, line 76
def nested_name
        "::#{@name}"
end
path() click to toggle source

The lexical scope as an array of names. e.g. `[:Decode, :Definition]` @returns [Array]

# File lib/decode/definition.rb, line 94
def path
        if @path
                # Cached version:
                @path
        elsif @parent
                # Merge with parent:
                @path = [*@parent.path, @name].freeze
        else
                # At top:
                @path = [@name].freeze
        end
end
Also aliased as: lexical_path
qualified_form() click to toggle source

A long form which uses the qualified name if possible. Defaults to {long_form}.

@returns [String | nil]

# File lib/decode/definition.rb, line 128
def qualified_form
        self.long_form
end
qualified_name() click to toggle source

The qualified name is an absolute name which includes any and all namespacing. @returns [String]

# File lib/decode/definition.rb, line 64
def qualified_name
        @qualified_name ||= begin
                if @parent
                        @parent.qualified_name + self.nested_name
                else
                        @name.to_s
                end
        end
end
short_form() click to toggle source

A short form of the definition. e.g. `def short_form`.

@returns [String | nil]

# File lib/decode/definition.rb, line 113
def short_form
end
start_with?(prefix) click to toggle source

Does the definition name match the specified prefix? @returns [Boolean]

# File lib/decode/definition.rb, line 82
def start_with?(prefix)
        self.nested_name.start_with?(prefix)
end
text() click to toggle source

The full text of the definition.

@returns [String | nil]

# File lib/decode/definition.rb, line 142
def text
end
to_s() click to toggle source
# File lib/decode/definition.rb, line 41
def to_s
        "\#<#{self.class} #{qualified_name}>"
end