class Vernacular::SourceFile

Represents a file that contains Ruby source code that can be read from and compiled down to instruction sequences.

Attributes

iseq_path[R]
source_path[R]

Public Class Methods

load_iseq(source_path) click to toggle source
# File lib/vernacular/source_file.rb, line 34
def self.load_iseq(source_path)
  new(
    source_path,
    File.join(Vernacular.iseq_dir, Vernacular.iseq_path_for(source_path))
  ).load
end
new(source_path, iseq_path) click to toggle source
# File lib/vernacular/source_file.rb, line 9
def initialize(source_path, iseq_path)
  @source_path = source_path
  @iseq_path   = iseq_path
end

Public Instance Methods

dump() click to toggle source
# File lib/vernacular/source_file.rb, line 14
def dump
  source = Vernacular.modify(File.read(source_path))
  iseq = RubyVM::InstructionSequence.compile(source, source_path,
                                             source_path)
  digest = ::Digest::MD5.file(source_path).digest
  File.binwrite(iseq_path, iseq.to_binary("MD5:#{digest}"))
  iseq
rescue SyntaxError, RuntimeError
  nil
end
load() click to toggle source
# File lib/vernacular/source_file.rb, line 25
def load
  if File.exist?(iseq_path) &&
     (File.mtime(source_path) <= File.mtime(iseq_path))
    RubyVM::InstructionSequence.load_from_binary(File.binread(iseq_path))
  else
    dump
  end
end