Class: Knj::Compiler
Overview
This class can compile Ruby-files into Ruby-methods on a special class and then call these methods to run the code from the file. The theory is that this can be faster…
Defined Under Namespace
Classes: Container
Instance Method Summary collapse
-
#compile_file(args) ⇒ Object
Compiles file into cache as a method.
-
#def_name_for_file_path(filepath) ⇒ Object
Returns the method name for a filepath.
-
#eval_file(args) ⇒ Object
Compile and evaluate a file - it will be cached.
-
#initialize(args = {}) ⇒ Compiler
constructor
A new instance of Compiler.
Constructor Details
Instance Method Details
#compile_file(args) ⇒ Object
Compiles file into cache as a method.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/knj/compiler.rb', line 15 def compile_file(args) raise "File does not exist." if !File.exist?(args[:filepath]) defname = def_name_for_file_path(args[:filepath]) evalcont = "class Knj::Compiler::Container; def self.#{defname};" evalcont << File.read(args[:filepath]) evalcont << ";end;end" eval(evalcont, nil, args[:fileident]) @compiled[args[:filepath]] = Time.new end |
#def_name_for_file_path(filepath) ⇒ Object
Returns the method name for a filepath.
28 29 30 |
# File 'lib/knj/compiler.rb', line 28 def def_name_for_file_path(filepath) return filepath.gsub("/", "_").gsub(".", "_") end |
#eval_file(args) ⇒ Object
Compile and evaluate a file - it will be cached.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/knj/compiler.rb', line 33 def eval_file(args) #Compile if it hasnt been compiled yet. if !@compiled.key?(args[:filepath]) @mutex.synchronize do compile_file(args) if !@compiled.key?(args[:filepath]) end end #Compile if modified time has been changed. mtime = File.mtime(args[:filepath]) if @compiled[args[:filepath]] < mtime @mutex.synchronize do compile_file(args) end end #Call the compiled function. defname = def_name_for_file_path(args[:filepath]) Knj::Compiler::Container.send(defname) end |