Skip to content

Commit

Permalink
Basic rack middleware is go
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed Oct 15, 2009
1 parent ddfe522 commit 9dfa3bd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
38 changes: 32 additions & 6 deletions lib/hassle.rb
Expand Up @@ -4,13 +4,39 @@
class Hassle
def initialize(app)
@app = app
@compiler = Hassle::Compiler.new
@compiler.compile
compiler = Hassle::Compiler.new
compiler.compile
@stylesheets = compiler.stylesheets
end

def call(env)
@app.call(env)
end
def call(env)
if !@stylesheets.empty? && env['PATH_INFO'] =~ css_request_regexp
return render_sass($1)
end
@app.call(env)
end

def render_sass(name)
css_file = File.join("tmp", "hassle", name)
[
200,
{
'Cache-Control' => 'public, max-age=86400',
'Content-Length' => File.size(css_file).to_s,
'Content-Type' => 'text/css'
},
File.read(css_file)
]
end

def css_request_regexp
@css_request_regexp ||= build_regexp
end

def build_regexp
files = @stylesheets.map { |f| Regexp.escape(f) }.join('|')
/^(#{files})(\?.*)?$/
end
end

class Hassle::Compiler
Expand Down Expand Up @@ -50,7 +76,7 @@ def prepare
def stylesheets
options[:template_location].to_a.map do |location|
Dir[File.join(location.last, "**", "*.css")]
end.flatten.sort
end.flatten.sort.map { |css| css.gsub(File.join(Dir.pwd, "tmp", "hassle"), "") }
end

def compile
Expand Down
6 changes: 6 additions & 0 deletions spec/base.rb
Expand Up @@ -24,6 +24,12 @@ def be_compiled
simple_matcher("contain compiled sass") { |given| File.read(given) =~ /h1 \{/ }
end

def have_tmp_dir_removed(*stylesheets)
simple_matcher("remove tmp dir") do |given|
given == stylesheets.map { |css| css.gsub(File.join(Dir.pwd, "tmp", "hassle"), "") }
end
end

def reset
Sass::Plugin.options.clear
Sass::Plugin.options = SASS_OPTIONS
Expand Down
8 changes: 4 additions & 4 deletions spec/hassle_compiler_spec.rb
Expand Up @@ -28,7 +28,7 @@
@hassle.compile

sass.should be_compiled
@hassle.stylesheets.should == [sass]
@hassle.stylesheets.should have_tmp_dir_removed(sass)
end

it "should not create sass cache" do
Expand Down Expand Up @@ -70,7 +70,7 @@

sass_one.should be_compiled
sass_two.should be_compiled
@hassle.stylesheets.should == [sass_one, sass_two]
@hassle.stylesheets.should have_tmp_dir_removed(sass_one, sass_two)
end

it "should compile sass if template location is an array with multiple locations" do
Expand All @@ -84,7 +84,7 @@

sass_one.should be_compiled
sass_two.should be_compiled
@hassle.stylesheets.should == [sass_one, sass_two]
@hassle.stylesheets.should have_tmp_dir_removed(sass_one, sass_two)
end

it "should not overwrite similarly name files in different directories" do
Expand All @@ -98,7 +98,7 @@

sass_one.should be_compiled
sass_two.should be_compiled
@hassle.stylesheets.should == [sass_one, sass_two]
@hassle.stylesheets.should have_tmp_dir_removed(sass_one, sass_two)
end
end
end
3 changes: 2 additions & 1 deletion spec/hassle_spec.rb
Expand Up @@ -12,7 +12,7 @@ def app

before do
reset
write_sass("./public/stylesheets")
write_sass("./public/stylesheets/sass")
end

it "sends through basic responses" do
Expand All @@ -25,5 +25,6 @@ def app
get '/stylesheets/screen.css'
last_response.status.should == 200
last_response.body.should =~ /h1 \{/
last_response.headers['Cache-Control'].should =~ /max-age=86400/
end
end

0 comments on commit 9dfa3bd

Please sign in to comment.