Sha256: f7ddc6a38c3fe1bbf1c5b3a1ff27b842611bc43d426b9b2d2c4655a2332be7d5

Contents?: true

Size: 1.61 KB

Versions: 101

Compression:

Stored size: 1.61 KB

Contents

#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...
class Knj::Compiler
  def initialize(args = {})
    @args = args
    @mutex = Mutex.new
    
    if @args[:cache_hash]
      @compiled = @args[:cache_hash]
    else
      @compiled = {}
    end
  end
  
  #Compiles file into cache as a method.
  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
  
  #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)
    #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
  
  #This class holds the compiled methods.
  class Knj::Compiler::Container; end
end

Version data entries

101 entries across 101 versions & 1 rubygems

Version Path
knjrbfw-0.0.116 lib/knj/compiler.rb
knjrbfw-0.0.115 lib/knj/compiler.rb
knjrbfw-0.0.114 lib/knj/compiler.rb
knjrbfw-0.0.113 lib/knj/compiler.rb
knjrbfw-0.0.111 lib/knj/compiler.rb
knjrbfw-0.0.110 lib/knj/compiler.rb
knjrbfw-0.0.109 lib/knj/compiler.rb
knjrbfw-0.0.108 lib/knj/compiler.rb
knjrbfw-0.0.107 lib/knj/compiler.rb
knjrbfw-0.0.105 lib/knj/compiler.rb
knjrbfw-0.0.104 lib/knj/compiler.rb
knjrbfw-0.0.103 lib/knj/compiler.rb
knjrbfw-0.0.102 lib/knj/compiler.rb
knjrbfw-0.0.101 lib/knj/compiler.rb
knjrbfw-0.0.100 lib/knj/compiler.rb
knjrbfw-0.0.99 lib/knj/compiler.rb
knjrbfw-0.0.98 lib/knj/compiler.rb
knjrbfw-0.0.97 lib/knj/compiler.rb
knjrbfw-0.0.96 lib/knj/compiler.rb
knjrbfw-0.0.95 lib/knj/compiler.rb