Need make a module what accepts one or more paths
, and coerce them to array of Class::Path
. In the CPAN exists a module MooseX::Types::Path::Class. From it's source I discovered than the module defined subtypes Dir
and File
.
My example module:
package Web::Finder;
use namespace::sweep;
use Modern::Perl;
use Moose;
use MooseX::Types::Path::Class qw(Dir); #??? - is this correct?
use Method::Signatures::Simple;
use Path::Class;
#should accept one or more directories as Str or as Path::Class::Dir
has 'volumes' => (
is => 'ro',
isa => 'ArrayRef[Path::Class::Dir]', #Can I use here a simple 'ArrayRef[Dir]' ?
required => 1,
coerce => 1,
);
has 'fs' => (
is => 'ro',
isa => 'ArrayRef[File::System]',
lazy => 1,
builder => '_create_volumes',
);
#is the next correct?
method _create_volumes {
push $self->fs, File::System->new('Real', root => $_->stringify) for ($self->volumes);
}
and my script
#!/usr/bin/env perl
use Modern::Perl;
use Web::Finder;
my $f = Web::Finder->new(volumes => .... ???
What I should change in the above module to accept the next initializations?
my $f = My::Module->new(volumes => "/some/path");
and
my $f = My::Module->new(volumes => [qw(/some/path1 /another/path2)] );
or something like - so: one or more paths...
From the error message I understand than me doing something wrong... ;)
You cannot coerce an attribute (volumes) unless its type (ArrayRef[Path::Class::Dir]) has a coercion at Web/Finder.pm line 14.
require Web/Finder.pm called at x line 2
main::BEGIN() called at Web/Finder.pm line 0
eval {...} called at Web/Finder.pm line 0
Attribute (volumes) does not pass the type constraint because: Validation failed for 'ArrayRef[Path::Class::Dir]' with value ARRAY(0x7f812b0040b8) at constructor Web::Finder::new (defined at Web/Finder.pm line 33) line 42.
Web::Finder::new("Web::Finder", "volumes", ARRAY(0x7f812b0040b8)) called at x line 6
The next part of question is how to create for each volume
one File::System instance. Is the builder
method correct?
Will be happy with any help and/or comments.