CoveredSourceCoveredSource

class Source

Source code metadata for a covered file or generated template.

Definitions

def self.for(path, **options)

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

Signature

parameter path String

The source path.

parameter options Hash

Options forwarded to Covered::Source#initialize.

returns Covered::Source

The source metadata.

Implementation

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 initialize(path, code: nil, line_offset: 1, modified_time: nil)

Initialize source metadata.

Signature

parameter path String

The source path.

parameter code String | Nil

Optional generated source code.

parameter line_offset Integer

The starting line offset.

parameter modified_time Time | Nil

The source modification time.

Implementation

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

attr_accessor :path

Signature

attribute String

The source path.

attr :code

Signature

attribute String | Nil

Optional generated source code.

attr :line_offset

Signature

attribute Integer

The starting line offset for generated source code.

attr :modified_time

Signature

attribute Time | Nil

The recorded source modification time.

def to_s

A human-readable representation of this source.

Signature

returns String

A summary containing the source path.

Implementation

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

def read(&block)

Read the source code from disk.

Signature

yields {|file| ...}

If a block is given, yields an open source file.

parameter file File

The open source file.

returns String | Object

The source contents without a block, or the block result with a block.

Implementation

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

def code!

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.

Signature

returns String

The generated code when present, otherwise the file contents.

Implementation

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

def code?

Whether generated source code is present.

Signature

returns Boolean

Whether this source has generated code.

Implementation

def code?
	!!self.code
end

def serialize(packer)

Serialize this source with the given packer.

Signature

parameter packer Object

The MessagePack-compatible packer.

Implementation

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

def self.deserialize(unpacker)

Deserialize a source from the given unpacker.

Signature

parameter unpacker Object

The MessagePack-compatible unpacker.

returns Covered::Source

The deserialized source metadata.

Implementation

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