Permalink
Browse files

Implement ignored files. Closes #17.

Also, refactor HashArray.
  • Loading branch information...
1 parent 55b6338 commit 3b8604d3fcb565b33662dfd3e4dab164b747477d @rstacruz committed Sep 17, 2011
View
@@ -437,6 +437,59 @@ end
# In views: <%= js :application %>
```
+### assets.ignore
+Excludes any URL paths that match the given spec.
+
+These files will not show up in packages, and they will not be accessible.
+
+By default, `.*` and `_*` are ignored. The former protects folders such as
+`.svn` from being accessed, and the latter protects Sass partial files from
+being accessed directly.
+
+Note that this matches against URL paths, not local file paths. This means
+something like `*.scss` will not work, as all stylesheet files will be compiled
+to `.css`.
+
+``` ruby
+# Usage:
+assets {
+ ignore FILESPEC
+}
+```
+
+#### Example
+Here's an example.
+
+``` ruby
+class App < Sinatra::Base
+ assets {
+ # Ignores all files matching *.private.js in any folder.
+ ignore '*.private.js'
+
+ # Ignores all files in `/app/js/foo/**/*`
+ ignore '/js/foo'
+ }
+end
+```
+
+#### Advanced usage
+By default, `.*` and `_*` are ignored. To disable this behavior, you can use
+`clear_ignores!` before your `ignore` lines.
+
+``` ruby
+assets {
+ clear_ignores!
+ ignore '*.private.js'
+}
+```
+
+To check if a certain file is ignored, use `assets.ignore?`
+
+``` ruby
+assets.ignored?("/css/_chrome.css") #=> true
+```
+
+
### assets.prebuild
Caches the built packages on application startup.
@@ -45,6 +45,8 @@ def add_individual_routes!
pass unless AssetPack.supported_formats.include?(fmt)
fn = asset_path_for(file, from) or pass
+ pass if settings.assets.ignored?("#{path}/#{file}")
+
# Send headers
content_type fmt.to_sym
last_modified File.mtime(fn).to_i
@@ -38,20 +38,6 @@ def self.[](*arr)
new arr.each_slice(2).map { |(k, v)| Hash[k, v] }
end
- # Works like Hash#each.
- #
- # By extension, methods that rely on #each (like #inject) will work
- # as intended.
- #
- def each(&block)
- super { |hash| yield hash.to_a.flatten }
- end
-
- # Works like Hash#map.
- def map(&block)
- super { |hash| yield hash.to_a.flatten }
- end
-
# Works like Hash#values.
def values
inject([]) { |a, (k, v)| a << v; a }
@@ -65,6 +51,16 @@ def to_hash
def keys
inject([]) { |a, (k, v)| a << k; a }
end
+
+ [:each, :map, :map!, :reject, :reject!, :select, :select!].each do |meth|
+ send(:define_method, meth) { |*a, &block|
+ if block.respond_to?(:call)
+ super(*a) { |hash| block.call *hash.to_a }
+ else
+ super(*a)
+ end
+ }
+ end
end
end
end
@@ -49,13 +49,18 @@ def initialize(app, &blk)
@js_compression_options = Hash.new
@css_compression_options = Hash.new
+ @ignored = Array.new
+
reset!
# Defaults!
serve '/css', :from => 'app/css'
serve '/js', :from => 'app/js'
serve '/images', :from => 'app/images'
+ ignore '.*'
+ ignore '_*'
+
blk.arity <= 0 ? instance_eval(&blk) : yield(self) if block_given?
end
@@ -75,6 +80,27 @@ def reset!
@packages = Hash.new
end
+ # Ignores a given path spec.
+ def ignore(spec)
+ if spec[0] == '/'
+ @ignored << "#{spec}"
+ @ignored << "#{spec}/**"
+ else
+ @ignored << "**/#{spec}"
+ @ignored << "**/#{spec}/**"
+ end
+ end
+
+ # Makes nothing ignored. Use this if you don't want to ignore dotfiles and underscore files.
+ def clear_ignores!
+ @ignored = Array.new
+ end
+
+ # Checks if a given path is ignored.
+ def ignored?(fn)
+ @ignored.any? { |spec| File.fnmatch spec, fn }
+ end
+
# Adds some JS packages.
#
# js :foo, [ '/js/vendor/jquery.*.js', ... ]
@@ -37,7 +37,9 @@ def initialize(assets, name, type, path, filespecs)
# Returns a list of URIs
def paths_and_files
- @assets.glob @filespecs, :preserve => true
+ list = @assets.glob(@filespecs, :preserve => true)
+ list.reject! { |path, file| @assets.ignored?(path) }
+ list
end
def files
@@ -0,0 +1 @@
+alert("HELLO I SHOULD BE IGNORED. BUMBLEBEE.");
View
@@ -1,8 +1,7 @@
require File.expand_path('../test_helper', __FILE__)
class ArityTest < UnitTest
- class App < Sinatra::Base
- set :root, File.expand_path('../app', __FILE__)
+ class App < UnitTest::App
register Sinatra::AssetPack
assets do |a|
View
@@ -0,0 +1,30 @@
+require File.expand_path('../test_helper', __FILE__)
+
+class IgnoreTest < UnitTest
+ class App < UnitTest::App
+ register Sinatra::AssetPack
+
+ assets do
+ js :main, '/main.js', %w[/js/*.js]
+ end
+ end
+
+ def app
+ App
+ end
+
+ test "ignore as individual file" do
+ get '/js/_ignoreme.js'
+ assert last_response.status == 404
+ end
+
+ test "ignore in package" do
+ get '/main.js'
+ assert body.size > 0
+ assert !body.include?("BUMBLEBEE")
+ end
+
+ test "package files" do
+ assert !app.assets.packages['main.js'].files.any? { |s| s.include? '_ignoreme.js' }
+ end
+end

0 comments on commit 3b8604d

Please sign in to comment.