From ce4e3d2e112599bda3dd62da63bec5110fb7843c Mon Sep 17 00:00:00 2001 From: Trotter Cashion Date: Sun, 25 Apr 2010 11:31:13 -0400 Subject: [PATCH] Changed name to Chuusha --- ...ached-templates.gemspec => chuusha.gempsec | 6 +- lib/chuusha.rb | 88 ++++++++++++++++++ lib/rack-cached-templates.rb | 90 ------------------- ...ched_templates_test.rb => chuusha_test.rb} | 4 +- test/test_helper.rb | 2 +- 5 files changed, 94 insertions(+), 96 deletions(-) rename rack-cached-templates.gemspec => chuusha.gempsec (79%) create mode 100644 lib/chuusha.rb delete mode 100644 lib/rack-cached-templates.rb rename test/integration/{rack_cached_templates_test.rb => chuusha_test.rb} (93%) diff --git a/rack-cached-templates.gemspec b/chuusha.gempsec similarity index 79% rename from rack-cached-templates.gemspec rename to chuusha.gempsec index 6e47371..1436218 100644 --- a/rack-cached-templates.gemspec +++ b/chuusha.gempsec @@ -1,13 +1,13 @@ -PKG_VERSION = "0.0.1" +PKG_VERSION = "0.1.0" PKG_FILES = Dir['README.textile', 'lib/**/*.rb'] $spec = Gem::Specification.new do |s| - s.name = 'rack-cached-templates' + s.name = 'chuusha' s.version = PKG_VERSION s.summary = "Run templates through erb and cache the resulting static file" s.description = < "text/html"}, render] + end + end +end diff --git a/lib/rack-cached-templates.rb b/lib/rack-cached-templates.rb deleted file mode 100644 index c50d701..0000000 --- a/lib/rack-cached-templates.rb +++ /dev/null @@ -1,90 +0,0 @@ -require 'yaml' -require 'erubis' - -module Rack - module CachedTemplates - class Adapter - def initialize(app, config, root_dir) - @app = app - @root_dir = root_dir - @config = Config.new(config) - cache_everything if @config.cache_on_load? - end - - def call(env) - renderer = Renderer.new(@config, @root_dir + env["PATH_INFO"]) - return renderer.respond if renderer.template_exists? - @app.call(env) - end - - private - def cache_everything - template_files.each do |file| - renderer = Renderer.new(@config, file) - renderer.write_cached_copy - end - end - - def template_files - Dir[@root_dir + "/**/*.erb"].map do |f| - f.gsub(/\.erb$/, '') - end - end - end - - class Config - attr_reader :variables - - def initialize(file) - @config = YAML.load_file(file) - @variables = @config["variables"] || {} - @cache = @config["cache"] || {} - end - - def cache_envs - @cache["envs"] - end - - def cache? - env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] - cache_envs.include?(env) - end - - def cache_on_load? - @cache["on_load"] && cache? - end - end - - class Renderer - def initialize(config, path) - @config = config - @outfile = path - @path = @outfile + ".erb" - end - - def template_exists? - ::File.exist?(@path) - end - - def evaluated - return @evaluated if @evaluated - eruby = Erubis::Eruby.new(::File.read(@path)) - @evaluated = eruby.result(@config.variables) - end - - def write_cached_copy - ::File.open(@outfile, "w") { |f| f.write evaluated } - end - - def render - write_cached_copy if @config.cache? - evaluated - end - - def respond - # TODO: Should get right content-type - [200, {"Content-Type" => "text/html"}, render] - end - end - end -end diff --git a/test/integration/rack_cached_templates_test.rb b/test/integration/chuusha_test.rb similarity index 93% rename from test/integration/rack_cached_templates_test.rb rename to test/integration/chuusha_test.rb index 605d1f8..0004bb1 100644 --- a/test/integration/rack_cached_templates_test.rb +++ b/test/integration/chuusha_test.rb @@ -1,6 +1,6 @@ require 'test_helper' -class RackCachedTemplates < Test::Unit::TestCase +class ChuushaTest < Test::Unit::TestCase SUPPORT_DIR = File.dirname(__FILE__) + '/../support' PUBLIC_DIR = SUPPORT_DIR + '/public' @@ -8,7 +8,7 @@ class RackCachedTemplates < Test::Unit::TestCase def app app = Rack::Builder.new { - use Rack::CachedTemplates::Adapter, SUPPORT_DIR + "/config.yml", PUBLIC_DIR + use Chuusha::Rack, SUPPORT_DIR + "/config.yml", PUBLIC_DIR run Proc.new { |env| [200, {}, "hi"] } } end diff --git a/test/test_helper.rb b/test/test_helper.rb index 11ca087..dedbccc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,5 @@ require 'rubygems' -require 'rack-cached-templates' +require 'chuusha' require 'test/unit' require 'rack/test'