Skip to content

Commit

Permalink
add types/rules; glob rule "class"
Browse files Browse the repository at this point in the history
  • Loading branch information
jrockway committed Nov 28, 2009
1 parent 5b0ff99 commit 55e6e69
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Path/Filter/Rule.pm
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';
}
12 changes: 12 additions & 0 deletions lib/Path/Filter/Rule/EvaluateViaRegex.pm
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/;
}
}
19 changes: 19 additions & 0 deletions lib/Path/Filter/Rule/Glob.pm
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$/;
}
}
29 changes: 29 additions & 0 deletions lib/Path/Filter/Types.pm
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
}
};
19 changes: 19 additions & 0 deletions t/glob-basic.t
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;

0 comments on commit 55e6e69

Please sign in to comment.