class Source
Represents a source file in a specific language.
Definitions
def initialize(path, language)
Initialize a new source file.
Signature
-
parameter
path
String
The file-system path to the source file.
-
parameter
language
Language::Generic
The language parser to use.
Implementation
def initialize(path, language)
@path = path
@buffer = nil
@language = language
end
attr :path
The path of the source file.
Signature
-
attribute
String
A file-system path to the source file.
def relative_path
The relative path of the source, if it is known.
Signature
-
returns
String
The relative path or the full path if relative path is unknown.
Implementation
def relative_path
if @path.respond_to?(:relative_path)
@path.relative_path
else
@path
end
end
attr :language
The language of the source file.
Signature
-
attribute
Language::Generic
The language parser for this source.
def read
Read the source file into an internal buffer/cache.
Signature
-
returns
String
The contents of the source file.
Implementation
def read
@buffer ||= File.read(@path).freeze
end
def definitions(&block)
Open the source file and read all definitions.
Signature
-
yields
{|definition| ...}
All definitions from the source file.
-
parameter
definition
Definition
-
parameter
-
returns
Enumerator(Definition)
If no block given.
Implementation
def definitions(&block)
return to_enum(:definitions) unless block_given?
@language.definitions_for(self, &block)
end
def segments(&block)
Open the source file and read all segments.
Signature
-
yields
{|segment| ...}
All segments from the source file.
-
parameter
segment
Segment
-
parameter
-
returns
Enumerator(Segment)
If no block given.
Implementation
def segments(&block)
return to_enum(:segments) unless block_given?
@language.segments_for(self, &block)
end
def code(index = nil, relative_to: nil)
Generate code representation with optional index for link resolution.
Signature
-
parameter
index
Index
Optional index for resolving links.
-
parameter
relative_to
Definition
Optional definition to resolve relative references.
-
returns
String
The formatted code representation.
Implementation
def code(index = nil, relative_to: nil)
@language.code_for(self.read, index, relative_to: relative_to)
end