From 75dce5b66bdb89d74b4fd7cb886805068a10572a Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Sun, 22 Jan 2012 18:15:45 -0500 Subject: [PATCH] Ability to register custom template engines via Jasmine::Headless.register_engine hook --- lib/jasmine/headless/files_list.rb | 38 ++++++++++++++++++++ spec/lib/jasmine/headless/files_list_spec.rb | 15 ++++++++ 2 files changed, 53 insertions(+) diff --git a/lib/jasmine/headless/files_list.rb b/lib/jasmine/headless/files_list.rb index eef115a..7844c7f 100644 --- a/lib/jasmine/headless/files_list.rb +++ b/lib/jasmine/headless/files_list.rb @@ -38,6 +38,7 @@ module Jasmine::Headless def reset! @asset_paths = nil + @registered_engines = {} # register haml-sprockets and handlebars_assets if it's available... %w{haml-sprockets handlebars_assets}.each do |library| @@ -67,6 +68,21 @@ module Jasmine::Headless register_engine '.css', Jasmine::Headless::CSSTemplate register_engine '.jst', Jasmine::Headless::JSTTemplate end + + end + + def registered_engines + @registered_engines ||= {} + end + + def register_engine(file_extension, template_class) + registered_engines[file_extension] = template_class + end + + def register_engines! + registered_engines.each do |file_extension, template_class| + Sprockets.register_engine file_extension, template_class + end end def default_files @@ -95,11 +111,21 @@ module Jasmine::Headless @required_files = UniqueAssetList.new @potential_files_to_filter = [] + register_engines! + load_initial_assets use_config if config? end + def register_engines! + begin + require spec_helper + rescue LoadError + end + self.class.register_engines! + end + def load_initial_assets self.class.default_files.each do |file| begin @@ -301,5 +327,17 @@ module Jasmine::Headless end end end + + def spec_helper + File.join(spec_dir, "helpers", "spec_helper") + end end end + +module Jasmine::Headless + extend self + + def register_engine(file_extension, template_class) + Jasmine::Headless::FilesList.register_engine(file_extension, template_class) + end +end \ No newline at end of file diff --git a/spec/lib/jasmine/headless/files_list_spec.rb b/spec/lib/jasmine/headless/files_list_spec.rb index bd24076..6bddb71 100644 --- a/spec/lib/jasmine/headless/files_list_spec.rb +++ b/spec/lib/jasmine/headless/files_list_spec.rb @@ -218,6 +218,21 @@ describe Jasmine::Headless::FilesList do files_list.files.any? { |file| file['.erb'] }.should be_false end end + + describe "#register_engine!" do + + before(:each) do + Jasmine::Headless::FilesList.reset! + end + + it "should register code added via configure blocks" do + template_class = mock() + described_class.register_engine ".foo", template_class + Sprockets.expects(:register_engine).with(".foo", template_class) + described_class.new + end + + end end end