Class: Handlebars::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebars/loader.rb

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



8
9
10
11
12
# File 'lib/handlebars/loader.rb', line 8

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

Instance Method Details

#require(modname) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/handlebars/loader.rb', line 14

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