From b6a64c5b1dfd1f8b5c823d1daeb0d2393cad58c5 Mon Sep 17 00:00:00 2001 From: hysios Date: Wed, 8 Feb 2012 21:54:34 +0800 Subject: [PATCH] feature: add jst template file have cache --- lib/jasmine/headless.rb | 19 +--------------- lib/jasmine/headless/jst_template.rb | 16 ++++++++++++- lib/jasmine/headless/jst_template_cache.rb | 26 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 lib/jasmine/headless/jst_template_cache.rb diff --git a/lib/jasmine/headless.rb b/lib/jasmine/headless.rb index 0a60efa..33ee28c 100644 --- a/lib/jasmine/headless.rb +++ b/lib/jasmine/headless.rb @@ -24,6 +24,7 @@ module Jasmine autoload :CoffeeTemplate, 'jasmine/headless/coffee_template' autoload :JSTemplate, 'jasmine/headless/js_template' + autoload :JSTTemplateCache, 'jasmine/headless/jst_template_cache' autoload :JSTTemplate, 'jasmine/headless/jst_template' autoload :CSSTemplate, 'jasmine/headless/css_template' autoload :NilTemplate, 'jasmine/headless/nil_template' @@ -35,24 +36,6 @@ module Jasmine def root @root ||= Pathname(File.expand_path('../../..', __FILE__)) end - - def warn(message) - output.puts message if show_warnings? - end - - def show_warnings=(show) - @show_warnings = show - end - - def show_warnings? - @show_warnings = true if @show_warnings.nil? - - @show_warnings - end - - def output - $stdout - end end end end diff --git a/lib/jasmine/headless/jst_template.rb b/lib/jasmine/headless/jst_template.rb index b6d8f1c..9ef20b2 100644 --- a/lib/jasmine/headless/jst_template.rb +++ b/lib/jasmine/headless/jst_template.rb @@ -8,7 +8,21 @@ module Jasmine::Headless alert_bad_format(file) return '' end - %{} + begin + data = super + cache = Jasmine::Headless::JSTTemplateCache.new(file, data) + + source = cache.handle + if cache.cached? + %{ + } + else + %{} + end + rescue StandardError => e + puts "[%s] Error in compiling file: %s" % [ 'jst'.color(:red), file.color(:yellow) ] + raise e + end end end end diff --git a/lib/jasmine/headless/jst_template_cache.rb b/lib/jasmine/headless/jst_template_cache.rb new file mode 100644 index 0000000..ff7dcdc --- /dev/null +++ b/lib/jasmine/headless/jst_template_cache.rb @@ -0,0 +1,26 @@ +require 'sprockets/jst_processor' +require 'digest/sha1' +require 'fileutils' + +module Jasmine + module Headless + class JSTTemplateCache < CacheableAction + + def initialize(file, data) + @file = file + @data = data + end + + class << self + def cache_type + "jst_template" + end + end + + def action + @data + end + end + end +end +