Class: Handlebars::Loader
- Inherits:
-
Object
- Object
- Handlebars::Loader
- Defined in:
- lib/handlebars/loader.rb
Instance Method Summary collapse
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
- #require(modname) ⇒ Object
Constructor Details
#initialize ⇒ Loader
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.) 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 |