Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace find with block api with find_all
  • Loading branch information
josh committed Apr 3, 2014
1 parent 4efc2db commit 9f74ff6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
40 changes: 22 additions & 18 deletions lib/hike/cached_trail.rb
Expand Up @@ -52,27 +52,31 @@ def cached
# time cache and delegates here.
#
# See `Trail#find` for usage.
def find(*logical_paths, &block)
if block_given?
options = extract_options!(logical_paths)
base_path = Pathname.new(options[:base_path] || @root)

logical_paths.each do |logical_path|
logical_path = Pathname.new(logical_path.sub(/^\//, ''))

if relative?(logical_path)
find_in_base_path(logical_path, base_path, &block)
else
find_in_paths(logical_path, &block)
end
end
def find(*logical_paths)
find_all(*logical_paths).first
end

nil
else
find(*logical_paths) do |path|
return path
# The real implementation of `find_all`. `Trail#find_all` generates a one
# time index and delegates here.
#
# See `Trail#find_all` for usage.
def find_all(*logical_paths, &block)
return to_enum(__method__, *logical_paths) unless block_given?

options = extract_options!(logical_paths)
base_path = Pathname.new(options[:base_path] || @root)

logical_paths.each do |logical_path|
logical_path = Pathname.new(logical_path.sub(/^\//, ''))

if relative?(logical_path)
find_in_base_path(logical_path, base_path, &block)
else
find_in_paths(logical_path, &block)
end
end

nil
end

# A cached version of `Dir.entries` that filters out `.` files and
Expand Down
22 changes: 13 additions & 9 deletions lib/hike/trail.rb
Expand Up @@ -123,20 +123,24 @@ def unalias_extension(extension)
#
# trail.find("hike") || trail.find("hike/index")
#
# Though `find` always returns the first match, it is possible
# to iterate over all shadowed matches and fallbacks by supplying
# a block.
def find(*args)
index.find(*args)
end

# `Trail#find_all` returns all matching paths including fallbacks and
# shadowed matches.
#
# trail.find("hike", "hike/index") { |path| warn path }
# trail.find_all("hike", "hike/index").each { |path| warn path }
#
# This allows you to filter your matches by any condition.
# `find_all` returns an `Enumerator`. This allows you to filter your
# matches by any condition.
#
# trail.find("application") do |path|
# return path if mime_type_for(path) == "text/css"
# trail.find_all("application").find do |path|
# mime_type_for(path) == "text/css"
# end
#
def find(*args, &block)
cached.find(*args, &block)
def find_all(*args, &block)
cached.find_all(*args, &block)
end

# `Trail#cached` returns an `CachedTrail` object that has the same
Expand Down
10 changes: 2 additions & 8 deletions test/test_trail.rb
Expand Up @@ -178,21 +178,15 @@ def test_relative_files_must_exist_in_the_path
end

def test_find_all_respects_path_order
results = []
trail.find("layouts/interstitial.html") do |path|
results << path
end
results = trail.find_all("layouts/interstitial.html").to_a
assert_equal [
fixture_path("app/views/layouts/interstitial.html.erb"),
fixture_path("vendor/plugins/signal_id/app/views/layouts/interstitial.html.erb")
], results
end

def test_find_all_with_multiple_extensions_respects_extension_order
results = []
trail.find("application.js") do |path|
results << path
end
results = trail.find_all("application.js").to_a
assert_equal [
fixture_path("app/views/application.js.coffee.str"),
fixture_path("app/views/application.js.coffee.erb")
Expand Down

0 comments on commit 9f74ff6

Please sign in to comment.