Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #10 from kelredd/cache

in response to #9: when caching, always write out new cache files
  • Loading branch information...
commit d2b8b1555061059b8ea015cc1c34bd1b36c7bdbb 2 parents cfb93a6 + d2a0841
Kelly Redding authored
2  lib/rack/less/request.rb
View
@@ -90,13 +90,11 @@ def cached?
# Determine if the request is for a non-cached existing LESS CSS source file
# This will be called on every request so speed is an issue
# => first check if the request is a GET on a css resource in :hosted_at (fast)
- # => don't process if a file has already been cached
# => otherwise, check for less source files that match the request (slow)
def for_less?
get? && # GET on css resource in :hosted_at (fast, check first)
for_css? &&
hosted_at? &&
- !cached? && # resource not cached (little slower)
!source.files.empty? # there is source for the resource (slow, check last)
end
8 lib/rack/less/source.rb
View
@@ -53,7 +53,7 @@ def compiled
:filename => File.basename(file_path)
}
less = File.send(File.respond_to?(:binread) ? :binread : :read, file_path.to_s)
- Less::Parser.new(opts).parse(less).to_css(:compress => !!@compress)
+ Less::Parser.new(opts).parse(less).to_css(:compress => compress?)
end.join("\n")
compiled_css = case @compress
@@ -69,11 +69,9 @@ def compiled
compiled_css
end
- if cache? && !File.exists?(cf = File.join(@cache, "#{@css_resource}.css"))
+ if cache? && (cf = File.join(@cache, "#{@css_resource}.css"))
FileUtils.mkdir_p(File.dirname(cf))
- File.open(cf, "w") do |file|
- file.write(compiled_css)
- end
+ File.open(cf, "w") { |file| file.write(compiled_css) }
end
compiled_css
10 test/source_test.rb
View
@@ -138,6 +138,15 @@ class SourceCachingTests < SourceTests
:cache => @cache
}).to_css
@cached_file = File.join(@cache, "normal.css")
+ @cached_file_time = File.mtime(@cached_file)
+ # shall wait some time to get a new mtime - sorry for letting you wait
+ sleep 2
+
+ Rack::Less::Source.new('normal', {
+ :folder => @source_folder,
+ :cache => @cache
+ }).to_css
+ @updated_cached_file_time = File.mtime(@cached_file)
end
teardown do
if File.exists?(File.dirname(@cache))
@@ -149,6 +158,7 @@ class SourceCachingTests < SourceTests
assert File.exists?(@cache), 'the cache folder does not exist'
assert File.exists?(@cached_file), 'the css was not cached to a file'
assert_equal @expected.strip, File.read(@cached_file).strip, "the compiled css is incorrect"
+ assert @cached_file_time < @updated_cached_file_time, 'the cached file was not updated ' + @cached_file_time.to_s + ' >= ' + @updated_cached_file_time.to_s
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.