Sha256: 1e61af979dfa16b751a25a456b2be6b2d62b1b504cace22552ee0f1a7e3ccf11
Contents?: true
Size: 1.16 KB
Versions: 2
Compression:
Stored size: 1.16 KB
Contents
# frozen_string_literal: true module Marameters module Sources # Reads object source code from memory or file (assumes implementation is a one-liner). class Reader def initialize offset: 1, parser: RubyVM::InstructionSequence, io: File @offset = offset @parser = parser @io = io freeze end def call object instructions = parser.of object fail StandardError, "Unable to load source for: #{object.inspect}." unless instructions process object, instructions end private attr_reader :offset, :parser, :io def process object, instructions lines = instructions.script_lines return lines.first if lines return extract(*object.source_location) if io.readable? instructions.absolute_path fail StandardError, "Unable to load source for: #{object.inspect}." end def extract(path, line_number) = io.open(path) { |body| pluck body, line_number } def pluck body, line_number body.each_line .with_index .find { |_line, index| index + offset == line_number } .first end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
marameters-4.0.1 | lib/marameters/sources/reader.rb |
marameters-4.0.0 | lib/marameters/sources/reader.rb |