class Decode::Source

Represents a source file in a specific language.

Attributes

language[R]

The language of the source file. @attribute [Language::Generic]

path[R]

The path of the source file. @attribute [String] A file-system path.

Public Class Methods

new(path, language) click to toggle source
# File lib/decode/source.rb, line 26
def initialize(path, language)
        @path = path
        @buffer = nil
        @language = language
end

Public Instance Methods

code(index = nil, relative_to: nil) click to toggle source
# File lib/decode/source.rb, line 66
def code(index = nil, relative_to: nil)
        @language.code_for(self.read, index, relative_to: relative_to)
end
definitions(&block) click to toggle source

Open the source file and read all definitions. @yields {|definition| …} All definitions from the source file.

@parameter definition [Definition]

@returns [Enumerator(Definition)] If no block given.

# File lib/decode/source.rb, line 50
def definitions(&block)
        return to_enum(:definitions) unless block_given?
        
        @language.definitions_for(self.read, &block)
end
read() click to toggle source

Read the source file into an internal buffer/cache. @returns [String]

# File lib/decode/source.rb, line 42
def read
        @buffer ||= File.read(@path).freeze
end
segments(&block) click to toggle source

Open the source file and read all segments. @yields {|segment| …} All segments from the source file.

@parameter segment [Segment]

@returns [Enumerator(Segment)] If no block given.

# File lib/decode/source.rb, line 60
def segments(&block)
        return to_enum(:segments) unless block_given?
        
        @language.segments_for(self.read, &block)
end