Skip to content

Commit adf2186

Browse files
authored
initialize: add path parameter (#5)
This adds an optional path parameter to the initialize method which can be used to load alternate version of the Handlebars runtime.
1 parent fb66d05 commit adf2186

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

lib/handlebars/engine.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Engine
1616
#
1717
# @param lazy [true, false] immediately loads and initializes the JavaScript
1818
# environment.
19-
def initialize(lazy: false)
19+
# @param path [String, nil] the path to the version of Handlebars to load.
20+
# If `nil`, the contents of `Handlebars::Source.bundled_path` is loaded.
21+
def initialize(lazy: false, path: nil)
22+
@path = path
2023
init! unless lazy
2124
end
2225

@@ -204,7 +207,7 @@ def init!
204207
return if @init
205208

206209
@context = MiniRacer::Context.new
207-
@context.load(::Handlebars::Source.bundled_path)
210+
@context.load(@path || ::Handlebars::Source.bundled_path)
208211
@context.load(File.absolute_path("engine/init.js", __dir__))
209212

210213
@init = true

spec/handlebars/engine_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "tempfile"
4+
35
RSpec.describe Handlebars::Engine do
46
let(:engine) { described_class.new(**engine_options) }
57
let(:engine_context) { engine.instance_variable_get(:@context) }
@@ -41,6 +43,36 @@
4143
expect(engine_context).to be nil
4244
end
4345
end
46+
47+
context "when `path` is defined" do
48+
let(:file) { Tempfile.open }
49+
50+
before do
51+
engine_options[:path] = file.path
52+
file.write <<~HANDLEBARS
53+
var Handlebars = {
54+
compile: () => "compile",
55+
precompile: () => "precompile",
56+
template: () => "template",
57+
registerPartial: () => "registerPartial",
58+
unregisterPartial: () => "unregisterPartial",
59+
registerHelper: () => "registerHelper",
60+
unregisterHelper: () => "unregisterHelper",
61+
partials: {},
62+
VERSION: "VERSION",
63+
};
64+
HANDLEBARS
65+
file.rewind
66+
end
67+
68+
after do
69+
file.close
70+
end
71+
72+
it "loads the file contents" do
73+
expect(engine_context.eval("VERSION")).to eq("VERSION")
74+
end
75+
end
4476
end
4577

4678
###################################

0 commit comments

Comments
 (0)