Class: Decode::Source

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

Overview

Represents a source file in a specific language.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, language) ⇒ Source

Initialize a new source file.



14
15
16
17
18
19
# File 'lib/decode/source.rb', line 14

def initialize(path, language)
  @path = path
  @language = language
  
  @buffer = nil
end

Instance Attribute Details

#A file-system path to the source file.(file-systempathtothesourcefile.) ⇒ Object (readonly)

The path of the source file.



23
# File 'lib/decode/source.rb', line 23

attr :path

#languageObject (readonly)

The language of the source file.



27
28
29
# File 'lib/decode/source.rb', line 27

def language
  @language
end

#pathObject (readonly)

The path of the source file.



23
24
25
# File 'lib/decode/source.rb', line 23

def path
  @path
end

#The language parser for this source.(languageparser) ⇒ Object (readonly)

The language of the source file.



27
# File 'lib/decode/source.rb', line 27

attr :language

Instance Method Details

#code(index = nil, relative_to: nil) ⇒ Object

Generate code representation with optional index for link resolution.



59
60
61
# File 'lib/decode/source.rb', line 59

def code(index = nil, relative_to: nil)
  @language.code_for(self.read, index, relative_to: relative_to)
end

#definitions(&block) ⇒ Object

Open the source file and read all definitions.



39
40
41
42
43
# File 'lib/decode/source.rb', line 39

def definitions(&block)
  return to_enum(:definitions) unless block_given?
  
  @language.definitions_for(self, &block)
end

#readObject

Read the source file into an internal buffer/cache.



31
32
33
# File 'lib/decode/source.rb', line 31

def read
  @buffer ||= File.read(@path).freeze
end

#segments(&block) ⇒ Object

Open the source file and read all segments.



49
50
51
52
53
# File 'lib/decode/source.rb', line 49

def segments(&block)
  return to_enum(:segments) unless block_given?
  
  @language.segments_for(self, &block)
end