lib/knj/compiler.rb in knjrbfw-0.0.4 vs lib/knj/compiler.rb in knjrbfw-0.0.7

- old
+ new

@@ -12,40 +12,43 @@ end #Compiles file into cache as a method. def compile_file(args) raise "File does not exist." if !File.exist?(args[:filepath]) - filepath = Knj::Php.realpath(args[:filepath]) - defname = def_name_for_file_path(filepath) + defname = def_name_for_file_path(args[:filepath]) evalcont = "class Knj::Compiler::Container; def self.#{defname};" - evalcont += File.read(filepath) + evalcont += File.read(args[:filepath]) evalcont += ";end;end" eval(evalcont, nil, args[:fileident]) - @compiled[filepath] = Time.new + @compiled[args[:filepath]] = Time.new end #Returns the method name for a filepath. def def_name_for_file_path(filepath) return filepath.gsub("/", "_").gsub(".", "_") end #Compile and evaluate a file - it will be cached. def eval_file(args) - filepath = Knj::Php.realpath(args[:filepath]) - #Compile if it hasnt been compiled yet. - @mutex.synchronize do - compile_file(args) if !@compiled.has_key?(filepath) - - #Compile if modified time has been changed. - mtime = File.mtime(filepath) - compile_file(args) if @compiled[filepath] < mtime + if !@compiled.has_key?(args[:filepath]) + @mutex.synchronize do + compile_file(args) if !@compiled.has_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(filepath) + defname = def_name_for_file_path(args[:filepath]) Knj::Compiler::Container.send(defname) end #This class holds the compiled methods. class Knj::Compiler::Container; end \ No newline at end of file