class Covered::Source

Source code metadata for a covered file or generated template.

def self.deserialize(unpacker)

@returns [Covered::Source] The deserialized source metadata.
@parameter unpacker [Object] The MessagePack-compatible unpacker.
Deserialize a source from the given unpacker.
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

def self.for(path, **options)

@returns [Covered::Source] The source metadata.
@parameter options [Hash] Options forwarded to {initialize}.
@parameter path [String] The source path.
Records the current file modification time when the path exists.
Build source metadata for the given path.
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

def code!

@returns [String] The generated code when present, otherwise the file contents.
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.
def code!
	self.code || self.read
end

def code?

@returns [Boolean] Whether this source has generated code.
Whether generated source code is present.
def code?
	!!self.code
end

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

@parameter modified_time [Time | Nil] The source modification time.
@parameter line_offset [Integer] The starting line offset.
@parameter code [String | Nil] Optional generated source code.
@parameter path [String] The source path.
Initialize source metadata.
def initialize(path, code: nil, line_offset: 1, modified_time: nil)
	@path = path
	@code = code
	@line_offset = line_offset
	@modified_time = modified_time
end

def read(&block)

@returns [String | Object] The source contents without a block, or the block result with a block.
@parameter file [File] The open source file.
@yields {|file| ...} If a block is given, yields an open source file.
Read the source code from disk.
def read(&block)
	if block_given?
		File.open(self.path, "r", &block)
	else
		File.read(self.path)
	end
end

def serialize(packer)

@parameter packer [Object] The MessagePack-compatible packer.
Serialize this source with the given packer.
def serialize(packer)
	packer.write(self.path)
	packer.write(self.code)
	packer.write(self.line_offset)
	packer.write(self.modified_time)
end

def to_s

@returns [String] A summary containing the source path.
A human-readable representation of this source.
def to_s
	"\#<#{self.class} path=#{path}>"
end