class Ember::Handlebars::Template
Attributes
config[R]
Public Class Methods
call(input)
click to toggle source
# File lib/ember/handlebars/template.rb, line 38 def call(input) instance.call(input) end
config()
click to toggle source
# File lib/ember/handlebars/template.rb, line 15 def config @config ||= Config.new end
configure() { |config| ... }
click to toggle source
# File lib/ember/handlebars/template.rb, line 11 def configure yield config end
handlebars_available?()
click to toggle source
# File lib/ember/handlebars/template.rb, line 42 def handlebars_available? Barber::Precompiler.handlebars_available? end
instance()
click to toggle source
# File lib/ember/handlebars/template.rb, line 34 def instance @instance ||= new(config) end
new(config = self.class.config.dup)
click to toggle source
# File lib/ember/handlebars/template.rb, line 55 def initialize(config = self.class.config.dup) @config = config end
setup(env)
click to toggle source
# File lib/ember/handlebars/template.rb, line 19 def setup(env) env.register_mime_type 'text/x-handlebars', extensions: with_js_extension(%w(.raw.hbs .raw.hjs .raw.handlebars)) env.register_transformer 'text/x-handlebars', 'application/javascript', self env.register_mime_type 'text/x-ember-mustache', extensions: with_js_extension(%w(.mustache.hbs .mustache.hjs .mustache.handlebars)) env.register_transformer 'text/x-ember-mustache', 'application/javascript', self env.register_mime_type 'text/x-ember-handlebars', extensions: with_js_extension(%w(.hbs .hjs .handlebars)) env.register_transformer 'text/x-ember-handlebars', 'application/javascript', self end
setup_ember_template_compiler(path)
click to toggle source
# File lib/ember/handlebars/template.rb, line 30 def setup_ember_template_compiler(path) Barber::Ember::Precompiler.ember_template_compiler_path = path end
Private Class Methods
with_js_extension(extensions)
click to toggle source
# File lib/ember/handlebars/template.rb, line 48 def with_js_extension(extensions) extensions + extensions.map {|ext| ".js#{ext}" } end
Public Instance Methods
call(input)
click to toggle source
# File lib/ember/handlebars/template.rb, line 59 def call(input) data = input[:data] filename = input[:filename] raw = handlebars?(filename) if raw template = data else template = mustache_to_handlebars(filename, data) end template_name = input[:name] module_name = case config.output_type when :amd amd_template_target(config.amd_namespace, template_name) when :global template_path(template_name, config) else raise "Unsupported `output_type`: #{config.output_type}" end meta = meta_supported? ? {moduleName: module_name} : false if config.precompile if raw template = precompile_handlebars(template, input) else template = precompile_ember_handlebars(template, config.ember_template, input, meta) end else if raw template = compile_handlebars(data) else template = compile_ember_handlebars(template, config.ember_template, meta) end end case config.output_type when :amd "define('#{module_name}', ['exports'], function(__exports__){ __exports__['default'] = #{template} });" when :global namespace = raw ? config.raw_template_namespace : 'Ember.TEMPLATES' target = global_template_target(namespace, template_name, config) "#{target} = #{template}\n" else raise "Unsupported `output_type`: #{config.output_type}" end end
Private Instance Methods
_cache_key()
click to toggle source
# File lib/ember/handlebars/template.rb, line 182 def _cache_key [ self.class.name, VERSION, Barber::VERSION ] end
amd_template_target(namespace, module_name)
click to toggle source
# File lib/ember/handlebars/template.rb, line 126 def amd_template_target(namespace, module_name) [namespace, module_name].compact.join('/') end
compile_ember_handlebars(string, ember_template, options = nil)
click to toggle source
# File lib/ember/handlebars/template.rb, line 174 def compile_ember_handlebars(string, ember_template, options = nil) "Ember.#{ember_template}.compile(#{indent(string).inspect}, #{options.to_json});" end
compile_handlebars(string)
click to toggle source
# File lib/ember/handlebars/template.rb, line 170 def compile_handlebars(string) "Handlebars.compile(#{indent(string).inspect});" end
global_template_target(namespace, module_name, config)
click to toggle source
# File lib/ember/handlebars/template.rb, line 130 def global_template_target(namespace, module_name, config) "#{namespace}[#{template_path(module_name, config).inspect}]" end
handlebars?(filename)
click to toggle source
# File lib/ember/handlebars/template.rb, line 114 def handlebars?(filename) filename.to_s =~ /\.raw\.(handlebars|hjs|hbs)/ end
indent(string)
click to toggle source
# File lib/ember/handlebars/template.rb, line 178 def indent(string) string.gsub(/$(.)/m, "\\1 ").strip end
meta_supported?()
click to toggle source
# File lib/ember/handlebars/template.rb, line 190 def meta_supported? Gem::Version.new(Ember::VERSION) >= Gem::Version.new('2.7.0') end
mustache_to_handlebars(filename, template)
click to toggle source
# File lib/ember/handlebars/template.rb, line 118 def mustache_to_handlebars(filename, template) if filename =~ /\.mustache\.(handlebars|hjs|hbs)/ template.gsub(/\{\{(\w[^\}]+)\}\}/){ |x| "{{unbound #{$1}}}" } else template end end
precompile_ember_handlebars(template, ember_template, input, options = nil)
click to toggle source
# File lib/ember/handlebars/template.rb, line 157 def precompile_ember_handlebars(template, ember_template, input, options = nil) dependencies = [ Barber::Ember::Precompiler.compiler_version, ember_template, template, options ] input[:cache].fetch(_cache_key + dependencies) do "Ember.#{ember_template}.template(#{Barber::Ember::Precompiler.compile(template, options)});" end end
precompile_handlebars(template, input)
click to toggle source
# File lib/ember/handlebars/template.rb, line 146 def precompile_handlebars(template, input) dependencies = [ Barber::Precompiler.compiler_version, template, ] input[:cache].fetch(_cache_key + dependencies) do "Handlebars.template(#{Barber::Precompiler.compile(template)});" end end
template_path(path, config)
click to toggle source
# File lib/ember/handlebars/template.rb, line 134 def template_path(path, config) root = config.templates_root unless root.empty? Array(root).each.each do |r| path = path.sub(/#{Regexp.quote(r)}\//, '') end end path.split('/').join(config.templates_path_separator) end