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.



11
12
13
14
15
# File 'lib/decode/source.rb', line 11

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.



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

attr :path

#languageObject (readonly)

The language of the source file.



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

def language
  @language
end

#pathObject (readonly)

The path of the source file.



19
20
21
# File 'lib/decode/source.rb', line 19

def path
  @path
end

Instance Method Details

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



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

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.



44
45
46
47
48
# File 'lib/decode/source.rb', line 44

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.



36
37
38
# File 'lib/decode/source.rb', line 36

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

#relative_pathObject

The relative path of the source, if it is known.



22
23
24
25
26
27
28
# File 'lib/decode/source.rb', line 22

def relative_path
	if @path.respond_to?(:relative_path)
		@path.relative_path
	else
		@path
	end
end

#segments(&block) ⇒ Object

Open the source file and read all segments.



54
55
56
57
58
# File 'lib/decode/source.rb', line 54

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