Sha256: 8d5632b25dc8c02d8e1154c1f2179e7856ebb6fef0c76be42eb805691fc27aad

Contents?: true

Size: 799 Bytes

Versions: 4

Compression:

Stored size: 799 Bytes

Contents

require 'v8'
require 'pathname'

module Handlebars
  class Loader

    def initialize
      @cxt = V8::Context.new
      @path = Pathname(__FILE__).dirname.join('..','..','js','lib')
      @modules = {}
    end

    def require(modname)
      unless mod = @modules[modname]
        filename = modname =~ /\.js$/ ? modname : "#{modname}.js"
        filepath = @path.join(filename)
        fail LoadError, "no such file: #{filename}" unless filepath.exist?
        load = @cxt.eval("(function(require, module, exports) {#{File.read(filepath)}})", filepath.expand_path)
        object = @cxt['Object']
        mod = object.new
        mod['exports'] = object.new
        @modules[modname] = mod
        load.call(method(:require), mod, mod.exports)
      end
      return mod.exports
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
hbs_plus-0.1.3 lib/handlebars/loader.rb
hbs-0.1.2 lib/handlebars/loader.rb
hbs-0.1.1 lib/handlebars/loader.rb
hbs-0.1.0 lib/handlebars/loader.rb