Class: Vernacular::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/vernacular/source_file.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path, iseq_path) ⇒ SourceFile

Returns a new instance of SourceFile.



9
10
11
12
# File 'lib/vernacular/source_file.rb', line 9

def initialize(source_path, iseq_path)
  @source_path = source_path
  @iseq_path   = iseq_path
end

Instance Attribute Details

#iseq_pathObject (readonly)

Returns the value of attribute iseq_path.



7
8
9
# File 'lib/vernacular/source_file.rb', line 7

def iseq_path
  @iseq_path
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



7
8
9
# File 'lib/vernacular/source_file.rb', line 7

def source_path
  @source_path
end

Class Method Details

.load_iseq(source_path) ⇒ Object



34
35
36
37
38
39
# 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

Instance Method Details

#dumpObject



14
15
16
17
18
19
20
21
22
23
# 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

#loadObject



25
26
27
28
29
30
31
32
# 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