Class: Covered::Source

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

Overview

Source code metadata for a covered file or generated template.

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

Initialize source metadata.



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

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.



39
40
41
# File 'lib/covered/source.rb', line 39

def code
  @code
end

#line_offsetObject (readonly)

Returns the value of attribute line_offset.



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

def line_offset
  @line_offset
end

#modified_timeObject (readonly)

Returns the value of attribute modified_time.



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

def modified_time
  @modified_time
end

#Optional generated source code.(generatedsourcecode.) ⇒ Object (readonly)



39
# File 'lib/covered/source.rb', line 39

attr :code

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#The source path.(sourcepath.) ⇒ Object (readonly)



36
# File 'lib/covered/source.rb', line 36

attr_accessor :path

#The starting line offset for generated source code.(startinglineoffset) ⇒ Object (readonly)



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

attr :line_offset

Class Method Details

.deserialize(unpacker) ⇒ Object

Deserialize a source from the given unpacker.



89
90
91
92
93
94
95
96
# File 'lib/covered/source.rb', line 89

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

Build source metadata for the given path. Records the current file modification time when the path exists.



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

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.



67
68
69
# File 'lib/covered/source.rb', line 67

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

#code?Boolean

Whether generated source code is present.

Returns:

  • (Boolean)


73
74
75
# File 'lib/covered/source.rb', line 73

def code?
	!!self.code
end

#read(&block) ⇒ Object

Read the source code from disk.



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

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

#serialize(packer) ⇒ Object

Serialize this source with the given packer.



79
80
81
82
83
84
# File 'lib/covered/source.rb', line 79

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

#The recorded source modification time.=(recordedsourcemodificationtime. = (value)) ⇒ Object



45
# File 'lib/covered/source.rb', line 45

attr :modified_time

#to_sObject

A human-readable representation of this source.



49
50
51
# File 'lib/covered/source.rb', line 49

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