Browse files

Merge branch 'pass_filename_in_each_logical_path'

  • Loading branch information...
2 parents c9ab2e4 + 93e5103 commit 37b584dc969f6475976105c55d20ce7501955ef7 @josh josh committed Oct 16, 2012
Showing with 53 additions and 8 deletions.
  1. +4 −0 README.md
  2. +19 −8 lib/sprockets/base.rb
  3. +30 −0 test/test_environment.rb
View
4 README.md
@@ -361,6 +361,10 @@ submit a pull request.
## Version History ##
+**2.8.0** (October 16, 2012)
+
+* Pass logical path and absolute path to each_logical_path iterator
+
**2.7.0** (October 10, 2012)
* Added --css-compressor and --js-compressor command line flags
View
27 lib/sprockets/base.rb
@@ -328,13 +328,20 @@ def each_file
nil
end
- def each_logical_path(*args)
+ def each_logical_path(*args, &block)
return to_enum(__method__, *args) unless block_given?
filters = args.flatten
files = {}
each_file do |filename|
if logical_path = logical_path_for_filename(filename, filters)
- yield logical_path unless files[logical_path]
+ unless files[logical_path]
+ if block.arity == 2
+ yield logical_path, filename.to_s
+ else
+ yield logical_path
+ end
+ end
+
files[logical_path] = true
end
end
@@ -393,31 +400,35 @@ def circular_call_protection(path)
def logical_path_for_filename(filename, filters)
logical_path = attributes_for(filename).logical_path.to_s
- if matches_filter(filters, logical_path)
+ if matches_filter(filters, logical_path, filename)
return logical_path
end
# If filename is an index file, retest with alias
if File.basename(logical_path)[/[^\.]+/, 0] == 'index'
path = logical_path.sub(/\/index\./, '.')
- if matches_filter(filters, path)
+ if matches_filter(filters, path, filename)
return path
end
end
nil
end
- def matches_filter(filters, filename)
+ def matches_filter(filters, logical_path, filename)
return true if filters.empty?
filters.any? do |filter|
if filter.is_a?(Regexp)
- filter.match(filename)
+ filter.match(logical_path)
elsif filter.respond_to?(:call)
- filter.call(filename)
+ if filter.arity == 1
+ filter.call(logical_path)
+ else
+ filter.call(logical_path, filename.to_s)
+ end
else
- File.fnmatch(filter.to_s, filename)
+ File.fnmatch(filter.to_s, logical_path)
end
end
end
View
30 test/test_environment.rb
@@ -233,8 +233,27 @@ def self.test(name, &block)
assert !paths.include?("coffee")
end
+ test "iterate over each logical path and filename" do
+ paths = []
+ filenames = []
+ @env.each_logical_path do |logical_path, filename|
+ paths << logical_path
+ filenames << filename
+ end
+ assert_equal FILES_IN_PATH, paths.length
+ assert_equal paths.size, paths.uniq.size, "has duplicates"
+
+ assert paths.include?("application.js")
+ assert paths.include?("coffee/foo.js")
+ assert paths.include?("coffee/index.js")
+ assert !paths.include?("coffee")
+
+ assert filenames.any? { |p| p =~ /application.js.coffee/ }
+ end
+
test "each logical path enumerator" do
enum = @env.each_logical_path
+ assert_kind_of String, enum.first
assert_equal FILES_IN_PATH, enum.to_a.length
end
@@ -292,6 +311,17 @@ def self.test(name, &block)
assert !paths.include?("gallery.css")
end
+ test "iterate over each logical path matching proc filters with full path arg" do
+ paths = []
+ @env.each_logical_path(proc { |_, fn| fn.match(fixture_path('default/mobile')) }) do |logical_path|
+ paths << logical_path
+ end
+
+ assert paths.include?("mobile/a.js")
+ assert paths.include?("mobile/b.js")
+ assert !paths.include?("application.js")
+ end
+
test "CoffeeScript files are compiled in a closure" do
script = @env["coffee"].to_s
assert_equal "undefined", ExecJS.exec(script)

0 comments on commit 37b584d

Please sign in to comment.