preaction / Statocles

Static website CMS
http://preaction.me/statocles
Other
84 stars 33 forks source link

separate logic for obtaining list of files and filtering files so can modify the former #539

Closed djerius closed 6 years ago

djerius commented 6 years ago

I'm implementing a store where the list of files is read from a manifest file. I ended up copying find_files and modifying the definition of the iterator to

my @manifest = $self->path->child('MANIFEST')->lines( { chomp => 1 } );
my $iter = sub { return @manifest ? Path::Tiny::path(shift @manifest ) : undef }

Instead of duplicating find_files, it'd be cleaner to separate the generation of the original list of files from the filtering of the files. That's what this PR does. It adds a files method which returns an iterator. With this, my code now looks like:

package StatoclesX::Role::Store::Manifest;
use Moo::Role;
around files => sub {
    my ( $orig, $self ) = @_;
    my @manifest = $self->path->child('MANIFEST')->lines( { chomp => 1 } );
    sub { return @manifest ? Path::Tiny::path(shift @manifest ) : undef }
};
1;
preaction commented 6 years ago

Yeah, that makes sense. There's probably a lot of stuff can be refactored to make customizations easier like this. Thanks for the patch!

coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.01%) to 92.078% when pulling 1280cefc4989fd2a48974fd668726759a0206dbd on djerius:store-files into 2445fda405a571c88dacd7a48c6286cbb2492648 on preaction:master.