Class: Covered::Source

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, code: nil, line_offset: 1, modified_time: nil) ⇒ Source

Returns a new instance of Source.



17
18
19
20
21
22
# File 'lib/covered/source.rb', line 17

def initialize(path, code: nil, line_offset: 1, modified_time: nil)
	@path = path
	@code = code
	@line_offset = line_offset
	@modified_time = modified_time
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



25
26
27
# File 'lib/covered/source.rb', line 25

def code
  @code
end

#line_offsetObject (readonly)

Returns the value of attribute line_offset.



26
27
28
# File 'lib/covered/source.rb', line 26

def line_offset
  @line_offset
end

#modified_timeObject (readonly)

Returns the value of attribute modified_time.



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

def modified_time
  @modified_time
end

#pathObject

Returns the value of attribute path.



24
25
26
# File 'lib/covered/source.rb', line 24

def path
  @path
end

Class Method Details

.deserialize(unpacker) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/covered/source.rb', line 57

def self.deserialize(unpacker)
	path = unpacker.read
	code = unpacker.read
	line_offset = unpacker.read
	modified_time = unpacker.read
	
	self.new(path, code: code, line_offset: line_offset, modified_time: modified_time)
end

.for(path, **options) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/covered/source.rb', line 8

def self.for(path, **options)
	if File.exist?(path)
		# options[:code] ||= File.read(path)
		options[:modified_time] ||= File.mtime(path)
	end
	
	self.new(path, **options)
end

Instance Method Details

#code!Object

The actual code which is being covered. If a template generates the source, this is the generated code, while the path refers to the template itself.



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

def code!
	self.code || self.read
end

#code?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/covered/source.rb', line 46

def code?
	!!self.code
end

#read(&block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/covered/source.rb', line 33

def read(&block)
	if block_given?
		File.open(self.path, "r", &block)
	else
		File.read(self.path)
	end
end

#serialize(packer) ⇒ Object



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

def serialize(packer)
	packer.write(self.path)
	packer.write(self.code)
	packer.write(self.line_offset)
	packer.write(self.modified_time)
end

#to_sObject



29
30
31
# File 'lib/covered/source.rb', line 29

def to_s
	"\#<#{self.class} path=#{path}>"
end