Class: Jadeite::Environment

Inherits:
Object
  • Object
show all
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

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={})
  @options = DEFAULT_OPTIONS.merge(options)

  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.expand_path('../../', __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

Returns:

  • (Boolean)


61
62
63
# File 'lib/jadeite/environment.rb', line 61

def cache?
  !!@options[:cache]
end

#cache_dirObject



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 = compile_options.merge(opts)
  compiled = if cache?
               cached_read(template_str, opts) { @jade.compile(template_str, opts) }
             else
               @jade.compile(template_str.strip, compile_options.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.expand_path(file)))
end

#compile_optionsObject



57
58
59
# File 'lib/jadeite/environment.rb', line 57

def compile_options
  @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, compile_options.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