-
Notifications
You must be signed in to change notification settings - Fork 68
/
context.rb
68 lines (52 loc) · 1.27 KB
/
context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'handlebars/source'
require 'v8'
module Handlebars
class Context
def initialize
@js = V8::Context.new
@js['global'] = {} # there may be a more appropriate object to be used here @MHW
@js.load(Handlebars::Source.bundled_path)
@partials = handlebars.partials = Handlebars::Partials.new
end
def compile(*args)
::Handlebars::Template.new(self, handlebars.compile(*args))
end
def load_helpers(helpers_pattern)
Dir[helpers_pattern].each{ |path| load_helper(path) }
end
def load_helper(path)
@js.load(path)
end
def precompile(*args)
handlebars.precompile(*args)
end
def register_helper(name, &fn)
handlebars.registerHelper(name, fn)
end
def register_partial(name, content)
handlebars.registerPartial(name, content)
end
def create_frame(data)
handlebars.createFrame(data)
end
def partial_missing(&fn)
@partials.partial_missing = fn
end
def handlebars
@js.eval('Handlebars')
end
def []=(key, value)
data[key] = value
end
def [](key)
data[key]
end
class << self
attr_accessor :current
end
private
def data
handlebars[:_rubydata] ||= handlebars.create()
end
end
end