Class: Embarista::Filters::PrecompileHandlebarsFilter

Inherits:
Rake::Pipeline::Filter
  • Object
show all
Defined in:
lib/embarista/filters/precompile_handlebars_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ PrecompileHandlebarsFilter

Returns a new instance of PrecompileHandlebarsFilter.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/embarista/filters/precompile_handlebars_filter.rb', line 4

def initialize(options={}, &block)
  @template_dir = options[:template_dir] || 'templates'
  @templates_global = options[:templates_global] || 'Ember.TEMPLATES'

  options[:handlebars] ||= default_handlebars_src
  options[:ember_template_compiler] ||= default_ember_template_compiler_src

  throw Embarista::PrecompilerConfigurationError.new('Must specify handlebars source path') unless options[:handlebars]
  throw Embarista::PrecompilerConfigurationError.new('Must specify ember_template_compiler source path') unless options[:ember_template_compiler]
  @handlebars_src = options[:handlebars]
  @ember_template_compiler_src = options[:ember_template_compiler]

  super(&block)
  unless block_given?
    @output_name_generator = proc { |input| input.sub(/\.handlebars$/, '.js') }
  end
end

Instance Method Details

#default_ember_template_compiler_srcObject



41
42
43
# File 'lib/embarista/filters/precompile_handlebars_filter.rb', line 41

def default_ember_template_compiler_src
  Dir['app/vendor/ember-template-compiler-*.js'].last
end

#default_handlebars_srcObject



37
38
39
# File 'lib/embarista/filters/precompile_handlebars_filter.rb', line 37

def default_handlebars_src
  Dir['app/vendor/handlebars-*'].last
end

#generate_output(inputs, output) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/embarista/filters/precompile_handlebars_filter.rb', line 23

def generate_output(inputs, output)
  inputs.each do |input|
    name = File.basename(input.path, '.handlebars')
    dirname = File.dirname(input.path)

    dirname.gsub!(/^\/?#{@template_dir}\/?/,'')

    full_name = [dirname, name].compact.reject(&:empty?).join('/')
    compiled = precompile(input.read, @handlebars_src, @ember_template_compiler_src)

    output.write "\n#{@templates_global}['#{full_name}'] = #{compiled};\n"
  end
end