Browse files

each_entry iterator

  • Loading branch information...
1 parent a21808f commit ae0a0e1131dad9ba791c877e559fe45ca6f40a82 @josh josh committed Sep 14, 2011
Showing with 39 additions and 18 deletions.
  1. +26 −16 lib/sprockets/base.rb
  2. +13 −2 test/test_environment.rb
View
42 lib/sprockets/base.rb
@@ -110,11 +110,34 @@ def [](*args)
find_asset(*args)
end
+ def each_entry(root, &block)
+ return to_enum(__method__, root) unless block_given?
+ root = Pathname.new(root) unless root.is_a?(Pathname)
+
+ paths = []
+ entries(root).sort.each do |filename|
+ path = root.join(filename)
+ paths << path
+
+ if stat(path).directory?
+ each_entry(path) do |subpath|
+ paths << subpath
+ end
+ end
+ end
+
+ paths.sort_by(&:to_s).each(&block)
+
+ nil
+ end
+
def each_file
return to_enum(__method__) unless block_given?
- paths.each do |base_path|
- each_file_in_directory(base_path) do |path|
- yield path
+ paths.each do |root|
+ each_entry(root) do |path|
+ if !stat(path).directory?
+ yield path
+ end
end
end
nil
@@ -159,18 +182,5 @@ def build_asset(logical_path, pathname, options)
StaticAsset.new(self, logical_path, pathname)
end
end
-
- def each_file_in_directory(base, &block)
- base = Pathname.new(base) unless base.is_a?(Pathname)
- entries(base).each do |filename|
- path = base.join(filename)
- stats = stat(path)
- if stats.directory?
- each_file_in_directory(path, &block)
- else
- yield path
- end
- end
- end
end
end
View
15 test/test_environment.rb
@@ -166,6 +166,19 @@ def self.test(name, &block)
@env[fixture_path("default/mobile/a.js")].logical_path
end
+ test "iterate over each entry" do
+ entries = []
+ @env.each_entry(fixture_path("default")) do |path|
+ entries << path
+ end
+ assert_equal 30, entries.length
+ end
+
+ test "each entry enumerator" do
+ enum = @env.each_entry(fixture_path("default"))
+ assert_equal 30, enum.to_a.length
+ end
+
test "iterate over each file" do
files = []
@env.each_file do |filename|
@@ -175,7 +188,6 @@ def self.test(name, &block)
end
test "each file enumerator" do
- files = []
enum = @env.each_file
assert_equal 27, enum.to_a.length
end
@@ -195,7 +207,6 @@ def self.test(name, &block)
end
test "each logical path enumerator" do
- files = []
enum = @env.each_logical_path
assert_equal 27, enum.to_a.length
end

0 comments on commit ae0a0e1

Please sign in to comment.