Permalink
Browse files

add wrapper around File::Next that filters!

  • Loading branch information...
1 parent d7bb534 commit 56fdf84b1e47b2812b6efcdb7456023eee0059ec @jrockway committed Nov 28, 2009
Showing with 78 additions and 0 deletions.
  1. +44 −0 lib/File/Next/Filtered.pm
  2. +34 −0 t/file-next.t
@@ -0,0 +1,44 @@
+package File::Next::Filtered;
+
+use strict;
+use warnings;
+
+use File::Next;
+use Path::Filter;
+
+use Sub::Exporter -setup => {
+ exports => ['files'],
+};
+
+sub files {
+ my ($first, @rest) = @_;
+
+ my %args;
+ my @files;
+ if(ref $first){
+ %args = %$first;
+ @files = @rest;
+ }
+ else {
+ @files = ($first, @rest);
+ }
+
+ my $rules = delete $args{filters} || [qw/Backup VersionControl EditorJunk/];
+ my $filter = Path::Filter->new(
+ rules => $rules,
+ (@files == 1) ? ( root => $files[0] ) : (),
+ );
+
+ my $iter = File::Next::files(\%args, @files);
+
+ my $self; $self = sub {
+ my $next = $iter->();
+ return unless defined $next;
+ return $self->() if($filter->filter($next));
+ return $next;
+ };
+
+ return $self;
+}
+
+1;
View
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use Test::More;
+
+use File::Next::Filtered qw(files);
+use Directory::Scratch;
+
+my $tmp = Directory::Scratch->new;
+$tmp->touch('.git/refs/heads/master');
+$tmp->touch('.git/config');
+$tmp->touch('Makefile.PL');
+$tmp->touch('Makefile.PL~');
+$tmp->touch('.gitignore');
+$tmp->touch('lib/Foo/Bar.pm');
+$tmp->touch('t/basic.t');
+$tmp->touch('t/.#basic.t#');
+
+{
+ my $i = 0;
+ my $baseline = File::Next::files("$tmp");
+ while( $baseline->() ){ $i++ }
+ is $i, 8, 'got all files with plain iterator';
+}
+
+
+
+{
+ my $i = 0;
+ my $iter = File::Next::Filtered::files({ filters => [qw/EditorJunk VersionControl::Git Backup/] }, "$tmp");
+ while( $iter->() ){ $i++ }
+ is $i, 4, 'with filter, only got relevant files';
+}
+
+done_testing;

0 comments on commit 56fdf84

Please sign in to comment.