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

Returns a new instance of Source.



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

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

Instance Attribute Details

#A file-system path.(file-systempath.) ⇒ Object (readonly)

The path of the source file.



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

attr :path

#languageObject (readonly)

The language of the source file.



38
39
40
# File 'lib/decode/source.rb', line 38

def language
  @language
end

#pathObject (readonly)

The path of the source file.



34
35
36
# File 'lib/decode/source.rb', line 34

def path
  @path
end

Instance Method Details

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



66
67
68
# 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) ⇒ Object

Open the source file and read all definitions.



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

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

#readObject

Read the source file into an internal buffer/cache.



42
43
44
# File 'lib/decode/source.rb', line 42

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

#segments(&block) ⇒ Object

Open the source file and read all segments.



60
61
62
63
64
# File 'lib/decode/source.rb', line 60

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