-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use MooseX::Declare; | ||
|
||
role Path::Filter::Rule { | ||
requires 'evaluate'; # return 1 if the File or Dir should be filtered (i.e., is in the set) | ||
|
||
# these may die if the filter is too complex to be expressed in that format | ||
requires 'as_regexp'; | ||
requires 'as_glob'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use MooseX::Declare; | ||
|
||
role Path::Filter::Rule::EvaluateViaRegex { | ||
use Path::Filter::Types qw(Path); | ||
|
||
requires 'as_regexp'; | ||
|
||
method evaluate(Path $file) { | ||
my $rx = $self->as_regexp; | ||
return $file->stringify =~ /$rx/; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use MooseX::Declare; | ||
|
||
class Path::Filter::Rule::Glob | ||
with Path::Filter::Rule | ||
with Path::Filter::Rule::EvaluateViaRegex { | ||
has 'glob' => ( | ||
is => 'ro', | ||
reader => 'as_glob', | ||
isa => 'Str', | ||
required => 1, | ||
); | ||
|
||
method as_regexp { | ||
my $glob = $self->as_glob; | ||
$glob =~ s/[.]/[.]/g; | ||
$glob =~ s/\*/.*/g; | ||
return qr/^$glob$/; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package Path::Filter::Types; | ||
use strict; | ||
use warnings; | ||
|
||
use Path::Class; | ||
use MooseX::Types::Path::Class qw(File Dir); | ||
use MooseX::Types::Moose qw(Str); | ||
use MooseX::Types -declare => [qw/Rule Path/]; | ||
|
||
role_type Rule, { role => 'Path::Filter::Rule' }; | ||
|
||
coerce Rule, from Str, via { | ||
my $f = $_; | ||
my $class = "Path::Filter::Rule::$f"; | ||
Class::MOP::load_class($class); | ||
|
||
return $class->new; | ||
}; | ||
|
||
subtype Path, as File|Dir, where { defined $_ }; # Moose bug | ||
|
||
coerce Path, from Str, via { | ||
if(-e $_){ | ||
return -d $_ ? dir($_) : file($_); # on disk and is a dir, then dir | ||
} | ||
else { | ||
m{[/\\]$} ? dir($_) : file($_); # ends in \ or /, then dir | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
|
||
use Path::Filter::Rule::Glob; | ||
use Path::Class; | ||
|
||
my $txt = Path::Filter::Rule::Glob->new( | ||
glob => '*.txt', | ||
); | ||
|
||
is $txt->as_glob, '*.txt'; | ||
is $txt->as_regexp, '(?-xism:^.*[.]txt$)', 'got regex'; | ||
|
||
ok $txt->evaluate(Path::Class::file('', 'foo', 'bar.txt')); | ||
ok $txt->evaluate(Path::Class::dir('', 'foo', 'bar.txt')); | ||
ok !$txt->evaluate(Path::Class::file('', 'foo', 'bar.txt', 'omg_a_file')); | ||
|
||
done_testing; |