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.

Parameters:

  • name (Symbol)

    The name of the definition.

  • parent (Symbol) (defaults to: nil)

    The parent lexical scope.

  • language (Language) (defaults to: parent.language)

    The language in which the symbol is defined in.

  • comments (Array(String)) (defaults to: nil)

    The comments associated with the definition.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/decode/definition.rb', line 29

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

Instance Attribute Details

#commentsObject (readonly)

The comment lines which directly preceeded the definition.



57
58
59
# File 'lib/decode/definition.rb', line 57

def comments
  @comments
end

#languageObject (readonly)

The language the symbol is defined within.



53
54
55
# File 'lib/decode/definition.rb', line 53

def language
  @language
end

#nameObject (readonly)

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



47
48
49
# File 'lib/decode/definition.rb', line 47

def name
  @name
end

#parentObject (readonly)

The parent symbol, defining lexical scope.



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

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

Raises:

  • (ArgumentError)


81
82
83
# File 'lib/decode/definition.rb', line 81

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

#documentationDocumentation | Nil

Structured access to the definitions comments.

Returns:

  • (Documentation | Nil)

    A ‘Documentation` if this definition has comments.



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

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

#long_formString | nil

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

Returns:

  • (String | nil)


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_nameString

The name of this definition plus the nesting prefix.

Returns:

  • (String)


73
74
75
# File 'lib/decode/definition.rb', line 73

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

#pathArray Also known as: lexical_path

The lexical scope which is an array of lexical Key instances as generated by key.

Returns:

  • (Array)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/decode/definition.rb', line 87

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

#qualified_formString | nil

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

Returns:

  • (String | nil)


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

def qualified_form
	self.long_form
end

#qualified_nameString

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

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
# File 'lib/decode/definition.rb', line 61

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

#short_formString | nil

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

Returns:

  • (String | nil)


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

def short_form
end

#start_with?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#textString | nil

The full text of the definition.

Returns:

  • (String | nil)


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

def text
end

#to_sObject



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

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