Skip to content

Commit

Permalink
re #29 fix #29 adding regex to dirs function
Browse files Browse the repository at this point in the history
  • Loading branch information
lowks committed Jan 28, 2015
1 parent f285507 commit 1964487
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
29 changes: 21 additions & 8 deletions lib/Radpath/directory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,37 @@ defmodule Radpath.Dirs do
"""

def dirs(path) when (is_bitstring(path) or is_list(path)) do
def dirs(path, regex_dir \\ "") when (is_bitstring(path) or is_list(path)) do
file_path = case String.valid? path do
true -> [path]
false -> path
end
do_dirs(file_path, [])
do_dirs(file_path, [], regex_dir)
end
defp do_dirs([], result) do
defp do_dirs([], result, regex_dir) do
result
end
defp do_dirs(paths ,result) do
defp do_dirs(paths ,result, regex_dir) do
[h | t] = paths
do_dirs(t, result ++ dirs_list(h))
do_dirs(t, result ++ dirs_list(h, regex_dir), regex_dir)
end

defp dirs_list(path) when is_bitstring(path) do
Finder.new() |> Finder.only_directories() |> Finder.find(Path.expand(path)) |> Enum.to_list
defp dirs_list(path, regex_dir) when is_bitstring(path) do
if regex_dir |> String.length == 0 do
Finder.new()
|> Finder.only_directories()
|> Finder.find(Path.expand(path))
|> Enum.to_list
|> Enum.sort
else
Finder.new()
|> Finder.with_directory_regex(Regex.compile!(regex_dir))
|> Finder.only_directories()
|> Finder.find(Path.expand(path))
|> Enum.to_list
|> Enum.sort
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions test/radpath_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ defmodule RadpathTests.RadpathFacts do
dirs = Radpath.dirs(fixture_path) |> Enum.map(&Path.basename(&1))
Enum.each(["testdir3", "testdir2", "testdir1"], fn(x) -> dirs |> contains x end)
end

fact "Test Filtering: Regex Directories" do
dirs = Radpath.dirs("test", "fixtures") |> Enum.map(&Path.basename(&1))
Enum.each(["fixtures"], fn(x) -> dirs |> contains x end)
end

fact "Test Filtering: files" do
Radpath.files(fixture_path, "log") |> Enum.map(&Path.basename(&1)) |> ["file3.log"]
Expand Down

0 comments on commit 1964487

Please sign in to comment.