Navigation Menu

Skip to content

Commit

Permalink
Changed name to Chuusha
Browse files Browse the repository at this point in the history
  • Loading branch information
trotter committed Apr 25, 2010
1 parent 559108f commit ce4e3d2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 96 deletions.
6 changes: 3 additions & 3 deletions rack-cached-templates.gemspec → 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 = <<EOS
Rack::CachedTemplates allows you to evaluate both javascript and css templates
Chuusha allows you to evaluate both javascript and css templates
within the same context, meaning that you can share constants between the
templates.
EOS
Expand Down
88 changes: 88 additions & 0 deletions lib/chuusha.rb
@@ -0,0 +1,88 @@
require 'yaml'
require 'erubis'

module Chuusha
class Rack
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
90 changes: 0 additions & 90 deletions lib/rack-cached-templates.rb

This file was deleted.

@@ -1,14 +1,14 @@
require 'test_helper'

class RackCachedTemplates < Test::Unit::TestCase
class ChuushaTest < Test::Unit::TestCase
SUPPORT_DIR = File.dirname(__FILE__) + '/../support'
PUBLIC_DIR = SUPPORT_DIR + '/public'

include Rack::Test::Methods

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
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
@@ -1,5 +1,5 @@
require 'rubygems'
require 'rack-cached-templates'
require 'chuusha'

require 'test/unit'
require 'rack/test'
Expand Down

0 comments on commit ce4e3d2

Please sign in to comment.