Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rename find logical paths helper

  • Loading branch information...
commit 05d382de11cbbe8438fda8d0ea4d1acd58afc294 1 parent 6c7b2dc
@josh josh authored
View
66 lib/sprockets/base.rb
@@ -282,36 +282,6 @@ def [](*args)
find_asset(*args)
end
- def each_file
- return to_enum(__method__) unless block_given?
- paths.each do |root|
- recursive_stat(root) do |path, stat|
- yield path if stat.file?
- end
- end
- nil
- end
-
- 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)
- 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
- nil
- end
-
# Pretty inspect
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} " +
@@ -361,42 +331,6 @@ def circular_call_protection(path)
Thread.current[:sprockets_circular_calls] = nil if reset
end
- def logical_path_for_filename(filename, filters)
- logical_path = attributes_for(filename).logical_path.to_s
-
- 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, filename)
- return path
- end
- end
-
- nil
- end
-
- def matches_filter(filters, logical_path, filename)
- return true if filters.empty?
-
- filters.any? do |filter|
- if filter.is_a?(Regexp)
- filter.match(logical_path)
- elsif filter.respond_to?(:call)
- if filter.arity == 1
- filter.call(logical_path)
- else
- filter.call(logical_path, filename.to_s)
- end
- else
- File.fnmatch(filter.to_s, logical_path)
- end
- end
- end
-
def json_decode(obj)
JSON.parse(obj, create_additions: false)
end
View
2  lib/sprockets/manifest.rb
@@ -112,7 +112,7 @@ def compile(*args)
raise Error, "manifest requires environment for compilation"
end
- paths = environment.each_logical_path(*args).to_a +
+ paths = environment.find_logical_paths(*args).to_a +
args.flatten.select { |fn| Pathname.new(fn).absolute? if fn.is_a?(String)}
paths.each do |path|
View
61 lib/sprockets/paths.rb
@@ -90,7 +90,68 @@ def recursive_stat(root, &block)
nil
end
+ def find_logical_paths(*args, &block)
+ return to_enum(__method__, *args) unless block_given?
+ filters = args.flatten
+ files = {}
+
+ paths.each do |root|
+ recursive_stat(root) do |path, stat|
+ next unless stat.file?
+
+ if logical_path = logical_path_for_filename(path, filters)
+ unless files[logical_path]
+ if block.arity == 2
+ yield logical_path, path.to_s
+ else
+ yield logical_path
+ end
+ end
+
+ files[logical_path] = true
+ end
+ end
+ end
+ nil
+ end
+
protected
attr_reader :trail
+
+ def logical_path_for_filename(filename, filters)
+ logical_path = attributes_for(filename).logical_path.to_s
+
+ 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, filename)
+ return path
+ end
+ end
+
+ nil
+ end
+
+ def matches_filter(filters, logical_path, filename)
+ return true if filters.empty?
+
+ filters.any? do |filter|
+ if filter.is_a?(Regexp)
+ filter.match(logical_path)
+ elsif filter.respond_to?(:call)
+ if filter.arity == 1
+ filter.call(logical_path)
+ else
+ filter.call(logical_path, filename.to_s)
+ end
+ else
+ File.fnmatch(filter.to_s, logical_path)
+ end
+ end
+ end
end
end
View
33 test/test_environment.rb
@@ -204,22 +204,9 @@ def self.test(name, &block)
FILES_IN_PATH = 36
- test "iterate over each file" do
- files = []
- @env.each_file do |filename|
- files << filename
- end
- assert_equal FILES_IN_PATH, files.length
- end
-
- test "each file enumerator" do
- enum = @env.each_file
- assert_equal FILES_IN_PATH, enum.to_a.length
- end
-
test "iterate over each logical path" do
paths = []
- @env.each_logical_path do |logical_path|
+ @env.find_logical_paths do |logical_path|
paths << logical_path
end
assert_equal FILES_IN_PATH, paths.length
@@ -234,7 +221,7 @@ def self.test(name, &block)
test "iterate over each logical path and filename" do
paths = []
filenames = []
- @env.each_logical_path do |logical_path, filename|
+ @env.find_logical_paths do |logical_path, filename|
paths << logical_path
filenames << filename
end
@@ -249,15 +236,15 @@ def self.test(name, &block)
assert filenames.any? { |p| p =~ /application.js.coffee/ }
end
- test "each logical path enumerator" do
- enum = @env.each_logical_path
+ test "find logical path enumerator" do
+ enum = @env.find_logical_paths
assert_kind_of String, enum.first
assert_equal FILES_IN_PATH, enum.to_a.length
end
test "iterate over each logical path matching fnmatch filters" do
paths = []
- @env.each_logical_path("*.js") do |logical_path|
+ @env.find_logical_paths("*.js") do |logical_path|
paths << logical_path
end
@@ -268,7 +255,7 @@ def self.test(name, &block)
test "iterate over each logical path matches index files" do
paths = []
- @env.each_logical_path("coffee.js") do |logical_path|
+ @env.find_logical_paths("coffee.js") do |logical_path|
paths << logical_path
end
assert paths.include?("coffee.js")
@@ -277,7 +264,7 @@ def self.test(name, &block)
test "each logical path enumerator matching fnmatch filters" do
paths = []
- enum = @env.each_logical_path("*.js")
+ enum = @env.find_logical_paths("*.js")
enum.to_a.each do |logical_path|
paths << logical_path
end
@@ -289,7 +276,7 @@ def self.test(name, &block)
test "iterate over each logical path matching regexp filters" do
paths = []
- @env.each_logical_path(/.*\.js/) do |logical_path|
+ @env.find_logical_paths(/.*\.js/) do |logical_path|
paths << logical_path
end
@@ -300,7 +287,7 @@ def self.test(name, &block)
test "iterate over each logical path matching proc filters" do
paths = []
- @env.each_logical_path(proc { |fn| File.extname(fn) == '.js' }) do |logical_path|
+ @env.find_logical_paths(proc { |fn| File.extname(fn) == '.js' }) do |logical_path|
paths << logical_path
end
@@ -311,7 +298,7 @@ def self.test(name, &block)
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|
+ @env.find_logical_paths(proc { |_, fn| fn.match(fixture_path('default/mobile')) }) do |logical_path|
paths << logical_path
end
Please sign in to comment.
Something went wrong with that request. Please try again.