Class: Decode::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/definition.rb

Overview

A symbol with attached documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent: nil, language: parent.language, comments: nil) ⇒ Definition

Initialize the symbol.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/decode/definition.rb', line 16

def initialize(name, parent: nil, language: parent.language, comments: nil)
	@name = name
	
	@parent = parent
	@language = language
	
	@comments = comments
	
	@path = nil
	@qualified_name = nil
end

Instance Attribute Details

#commentsObject (readonly)

The comment lines which directly preceeded the definition.



49
50
51
# File 'lib/decode/definition.rb', line 49

def comments
  @comments
end

#languageObject (readonly)

The language the symbol is defined within.



45
46
47
# File 'lib/decode/definition.rb', line 45

def language
  @language
end

#nameObject

The symbol name. e.g. ‘:Decode`.



37
38
39
# File 'lib/decode/definition.rb', line 37

def name
  @name
end

#parentObject

The parent definition, defining lexical scope.



41
42
43
# File 'lib/decode/definition.rb', line 41

def parent
  @parent
end

Instance Method Details

#container?Boolean

Whether this definition can contain nested definitions.

Returns:

  • (Boolean)


141
142
143
# File 'lib/decode/definition.rb', line 141

def container?
	false
end

#convert(kind) ⇒ Object

Convert this definition into another kind of definition.

Raises:

  • (ArgumentError)


76
77
78
# File 'lib/decode/definition.rb', line 76

def convert(kind)
	raise ArgumentError, "Unable to convert #{self} into #{kind}!"
end

#documentationObject

Structured access to the definitions comments.



155
156
157
158
159
# File 'lib/decode/definition.rb', line 155

def documentation
	if @comments&.any?
		@documentation ||= Documentation.new(@comments, @language)
	end
end

#inspectObject Also known as: to_s



28
29
30
# File 'lib/decode/definition.rb', line 28

def inspect
	"\#<#{self.class} #{qualified_name}>"
end

#locationObject

The location of the definition.



164
165
166
# File 'lib/decode/definition.rb', line 164

def location
	nil
end

#long_formObject

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



113
114
115
# File 'lib/decode/definition.rb', line 113

def long_form
	self.short_form
end

#multiline?Boolean

Whether the definition spans multiple lines.

Returns:

  • (Boolean)


128
129
130
# File 'lib/decode/definition.rb', line 128

def multiline?
	false
end

#nested?Boolean

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

Returns:

  • (Boolean)


148
149
150
# File 'lib/decode/definition.rb', line 148

def nested?
	container?
end

#nested_nameObject

The name of this definition plus the nesting prefix.



65
66
67
# File 'lib/decode/definition.rb', line 65

def nested_name
	"::#{@name}"
end

#pathObject Also known as: lexical_path

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



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/decode/definition.rb', line 83

def path
	if @path
		# Cached version:
		@path
	elsif @parent
		# Merge with parent:
		@path = [*@parent.path, *path_name].freeze
	else
		# At top:
		@path = path_name.freeze
	end
end

#path_nameObject



96
97
98
# File 'lib/decode/definition.rb', line 96

def path_name
	[@name]
end

#qualified_formObject

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



121
122
123
# File 'lib/decode/definition.rb', line 121

def qualified_form
	self.long_form
end

#qualified_nameObject

The qualified name is an absolute name which includes any and all namespacing.



53
54
55
56
57
58
59
60
61
# File 'lib/decode/definition.rb', line 53

def qualified_name
	@qualified_name ||= begin
		if @parent
			@parent.qualified_name + self.nested_name
		else
			@name.to_s
		end
	end
end

#short_formObject

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



106
107
# File 'lib/decode/definition.rb', line 106

def short_form
end

#start_with?(prefix) ⇒ Boolean

Does the definition name match the specified prefix?

Returns:

  • (Boolean)


71
72
73
# File 'lib/decode/definition.rb', line 71

def start_with?(prefix)
	self.nested_name.start_with?(prefix)
end

#textObject

The full text of the definition.



135
136
# File 'lib/decode/definition.rb', line 135

def text
end