class Covered::Source
Source code metadata for a covered file or generated template.
def self.deserialize(unpacker)
@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)
@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!
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?
Whether generated source code is present.
def code? !!self.code end
def initialize(path, code: nil, line_offset: 1, modified_time: nil)
@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)
@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)
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
A human-readable representation of this source.
def to_s "\#<#{self.class} path=#{path}>" end