Class: Jadeite::Environment
- Inherits:
-
Object
- Object
- Jadeite::Environment
- Defined in:
- lib/jadeite/environment.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ cache: true, cache_dir: ".jadeite-cache", compile_options: { client: false, compileDebug: true } }.freeze
Instance Method Summary collapse
- #cache? ⇒ Boolean
- #cache_dir ⇒ Object
- #compile(template_str, opts = {}) ⇒ Object
- #compile_file(file, opts = {}) ⇒ Object
- #compile_options ⇒ Object
-
#initialize(options = {}) ⇒ Environment
constructor
A new instance of Environment.
- #render(template_str, data = {}, opts = {}) ⇒ Object
- #render_file(file, data = {}, opts = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Environment
Returns a new instance of Environment.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jadeite/environment.rb', line 18 def initialize(={}) @options = DEFAULT_OPTIONS.merge() FileUtils.mkdir_p(cache_dir) if cache? and !File.directory?(cache_dir) # Setup V8 context @context = V8::Context.new # Load jade-runtime node_env = NodeJS::Environment.new(@context, File.('../../', __FILE__)) @context['jade'] = node_env.require('jade-runtime').runtime @jade = node_env.require('jade') # Create a new object in V8 that will keep a cached copy of compiled templates @cache = @context['Object'].new end |
Instance Method Details
#cache? ⇒ Boolean
61 62 63 |
# File 'lib/jadeite/environment.rb', line 61 def cache? !!@options[:cache] end |
#cache_dir ⇒ Object
65 66 67 |
# File 'lib/jadeite/environment.rb', line 65 def cache_dir @options[:cache_dir] end |
#compile(template_str, opts = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/jadeite/environment.rb', line 35 def compile(template_str, opts={}) opts = .merge(opts) compiled = if cache? cached_read(template_str, opts) { @jade.compile(template_str, opts) } else @jade.compile(template_str.strip, .merge(opts)) end Template.new(compiled) end |
#compile_file(file, opts = {}) ⇒ Object
49 50 51 |
# File 'lib/jadeite/environment.rb', line 49 def compile_file(file, opts = {}) compile(File.read(file), opts.merge(filename: File.(file))) end |
#compile_options ⇒ Object
57 58 59 |
# File 'lib/jadeite/environment.rb', line 57 def @options[:compile_options] end |
#render(template_str, data = {}, opts = {}) ⇒ Object
45 46 47 |
# File 'lib/jadeite/environment.rb', line 45 def render(template_str, data={}, opts={}) compile(template_str, .merge(opts)).render(data) end |
#render_file(file, data = {}, opts = {}) ⇒ Object
53 54 55 |
# File 'lib/jadeite/environment.rb', line 53 def render_file(file, data={}, opts = {}) compile_file(file, opts).render(data) end |