Class: Embarista::Precompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/embarista/precompiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Precompiler

Returns a new instance of Precompiler.



27
28
29
30
31
32
# File 'lib/embarista/precompiler.rb', line 27

def initialize(opts)
  throw PrecompilerConfigurationError.new('Must specify handlebars source path') unless opts[:handlebars]
  throw PrecompilerConfigurationError.new('Must specify ember_template_compiler source path') unless opts[:ember_template_compiler]
  @handlebars = File.new(opts[:handlebars])
  @ember_template_precompiler = File.new(opts[:ember_template_compiler])
end

Instance Attribute Details

#ember_template_precompilerObject (readonly)

Returns the value of attribute ember_template_precompiler.



47
48
49
# File 'lib/embarista/precompiler.rb', line 47

def ember_template_precompiler
  @ember_template_precompiler
end

#handlebarsObject (readonly)

Returns the value of attribute handlebars.



47
48
49
# File 'lib/embarista/precompiler.rb', line 47

def handlebars
  @handlebars
end

Instance Method Details

#compile(template) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/embarista/precompiler.rb', line 34

def compile(template)
  context.call(
    "EmberHandlebarsCompiler.precompile",
    sanitize(template)
  )
rescue ExecJS::ProgramError => ex
  raise Embarista::PrecompilerError.new(template, ex)
end

#precompilerObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/embarista/precompiler.rb', line 49

def precompiler
  @precompiler ||= StringIO.new(<<-JS)
    var exports = this.exports || {};
    function require() {
      // ember-template-compiler only requires('handlebars')
      return Handlebars;
    }
    // Precompiler
    var EmberHandlebarsCompiler = {
      precompile: function(string) {
        return exports.precompile(string).toString();
      }
    };
  JS
end

#sourcesObject



43
44
45
# File 'lib/embarista/precompiler.rb', line 43

def sources
  [precompiler, handlebars, ember_template_precompiler]
end