Class: Barber::Precompiler

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

Direct Known Subclasses

Ember::Precompiler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.compile(template, options = nil) ⇒ Object


7
8
9
# File 'lib/barber/precompiler.rb', line 7

def compile(template, options = nil)
  instance.compile(template, options)
end

.compiler_versionObject


11
12
13
# File 'lib/barber/precompiler.rb', line 11

def compiler_version
  instance.compiler_version
end

.handlebars_available?Boolean

Returns:

  • (Boolean)

15
16
17
18
19
20
21
# File 'lib/barber/precompiler.rb', line 15

def handlebars_available?
  require 'handlebars/source' # handlebars precompilation is optional feature.
rescue LoadError => e
  raise e unless ['cannot load such file -- handlebars/source', 'no such file to load -- handlebars/source'].include?(e.message)
ensure
  return !!defined?(Handlebars::Source)
end

Instance Method Details

#compile(template, options = nil) ⇒ Object


30
31
32
33
34
# File 'lib/barber/precompiler.rb', line 30

def compile(template, options = nil)
  context.call precompile_function, sanitize(template), options
rescue ExecJS::ProgramError => ex
  raise Barber::PrecompilerError.new(template, ex)
end

#compiler_versionObject


51
52
53
# File 'lib/barber/precompiler.rb', line 51

def compiler_version
  "handlebars:#{handlebars_version}"
end

#handlebarsObject


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

def handlebars
  @handlebars ||= File.new(Handlebars::Source.bundled_path)
end

#precompile_functionObject


36
37
38
# File 'lib/barber/precompiler.rb', line 36

def precompile_function
  "Barber.precompile"
end

#sourcesObject


40
41
42
43
44
45
# File 'lib/barber/precompiler.rb', line 40

def sources
  sources = []
  sources << precompiler
  sources << handlebars if self.class.handlebars_available?
  sources
end