Skip to content

Commit 4e9a586

Browse files
committed
Add support for strict mode feature
1 parent fa4bbcc commit 4e9a586

4 files changed

Lines changed: 21 additions & 8 deletions

File tree

lib/ruby-handlebars.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def initialize()
1717
set_escaper
1818
end
1919

20-
def compile(template)
21-
Template.new(self, template_to_ast(template))
20+
def compile(template, **options)
21+
Template.new(self, template_to_ast(template), **options)
2222
end
2323

2424
def register_helper(name, &fn)

lib/ruby-handlebars/context.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
module Handlebars
22
class Context
3-
def initialize(hbs, data)
3+
class AttributeNotFoundError < StandardError
4+
end
5+
6+
def initialize(hbs, data, **options)
47
@hbs = hbs
58
@data = data
9+
@options = options || {}
610
end
711

812
def get(path)
@@ -75,6 +79,8 @@ def get_attribute(item, attribute)
7579
if item.respond_to?(sym_attr)
7680
return item.send(sym_attr)
7781
end
82+
83+
raise AttributeNotFoundError if @options[:strict]
7884
end
7985
end
8086
end

lib/ruby-handlebars/template.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
module Handlebars
44
class Template
5-
def initialize(hbs, ast)
5+
def initialize(hbs, ast, **options)
66
@hbs = hbs
77
@ast = ast
8+
@options = options || {}
89
end
910

1011
def call(args = nil)
11-
ctx = Context.new(@hbs, args)
12+
ctx = Context.new(@hbs, args, **@options)
1213

1314
@ast.eval(ctx)
1415
end

spec/handlebars_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
describe Handlebars::Handlebars do
77
let(:hbs) {Handlebars::Handlebars.new}
88

9-
def evaluate(template, args = {})
10-
hbs.compile(template).call(args)
9+
def evaluate(template, args = {}, **compiling_options)
10+
hbs.compile(template, **compiling_options).call(args)
1111
end
1212

1313
context 'evaluating' do
@@ -51,6 +51,12 @@ def evaluate(template, args = {})
5151
expect(evaluate('Hello {{first-name}}', double("first-name": 'world'))).to eq('Hello world')
5252
end
5353

54+
context 'strict mode' do
55+
it 'raises an error' do
56+
expect{evaluate('Hello {{you}}', {}, strict: true)}.to raise_error(Handlebars::Context::AttributeNotFoundError)
57+
end
58+
end
59+
5460
context 'partials' do
5561
it 'simple' do
5662
hbs.register_partial('plic', "Plic")
@@ -71,7 +77,7 @@ def evaluate(template, args = {})
7177
hbs.register_partial('brackets', "[{{name}}]")
7278
expect(evaluate("Hello {{> brackets}}", {name: 'world'})).to eq("Hello [world]")
7379
end
74-
80+
7581
it 'with a string argument' do
7682
hbs.register_partial('with_args', "[{{name}}]")
7783
expect(evaluate("Hello {{> with_args name='jon'}}")).to eq("Hello [jon]")

0 commit comments

Comments
 (0)