1 package MooseX::Types::Path::Class;
3 use warnings FATAL => 'all';
9 -declare => [qw( Dir File )];
11 use MooseX::Types::Moose qw(Str ArrayRef);
13 class_type('Path::Class::Dir');
14 class_type('Path::Class::File');
16 subtype Dir, as 'Path::Class::Dir';
17 subtype File, as 'Path::Class::File';
19 for my $type ( 'Path::Class::Dir', Dir ) {
21 from Str, via { Path::Class::Dir->new($_) },
22 from ArrayRef, via { Path::Class::Dir->new(@$_) };
24 coerce ArrayRef[$type],
25 from ArrayRef[Str], via { [ map { Path::Class::Dir->new($_) } @$_ ] },
26 from ArrayRef[ArrayRef], via { [ map { Path::Class::Dir->new(@$_) } @$_ ] };
29 for my $type ( 'Path::Class::File', File ) {
31 from Str, via { Path::Class::File->new($_) },
32 from ArrayRef, via { Path::Class::File->new(@$_) };
34 coerce ArrayRef[$type],
35 from ArrayRef[Str], via { [ map { Path::Class::File->new($_) } @$_ ] },
36 from ArrayRef[ArrayRef], via { [ map { Path::Class::File->new(@$_) } @$_ ] };
39 # optionally add Getopt option type
40 eval { require MooseX::Getopt; };
42 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
43 for ( 'Path::Class::Dir', 'Path::Class::File', Dir, File, );
52 MooseX::Types::Path::Class - A Path::Class type library for Moose
59 use MooseX::Types::Path::Class;
60 with 'MooseX::Getopt'; # optional
64 isa => 'Path::Class::Dir',
71 isa => 'Path::Class::File',
76 # these attributes are coerced to the
77 # appropriate Path::Class objects
78 MyClass->new( dir => '/some/directory/', file => '/some/file' );
83 MooseX::Types::Path::Class creates common L<Moose> types,
84 coercions and option specifications useful for dealing
85 with L<Path::Class> objects as L<Moose> attributes.
87 Coercions (see L<Moose::Util::TypeConstraints>) are made
88 from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and
89 L<Path::Class::File> objects. If you have L<MooseX::Getopt> installed,
90 the Getopt option type ("=s") will be added for both
91 L<Path::Class::Dir> and L<Path::Class::File>.
96 None of these are exported by default. They are provided via
103 These exports can be used instead of the full class names. Example:
107 use MooseX::Types::Path::Class qw(Dir File);
123 Note that there are no quotes around Dir or File.
125 =item is_Dir($value), is_File($value)
127 Returns true or false based on whether $value is a valid Dir or File.
129 =item to_Dir($value), to_File($value)
131 Attempts to coerce $value to a Dir or File. Returns the coerced value
132 or false if the coercion failed.
139 L<MooseX::Types::Path::Class::MoreCoercions>, L<MooseX::FileAttribute>, L<MooseX::Types::URI>
144 L<Moose>, L<MooseX::Types>, L<Path::Class>
147 =head1 BUGS AND LIMITATIONS
149 If you find a bug please either email the author, or add
150 the bug to cpan-RT L<http://rt.cpan.org>.
155 Todd Hepler C<< <thepler@employees.org> >>
158 =head1 LICENCE AND COPYRIGHT
160 Copyright (c) 2007-2012, Todd Hepler C<< <thepler@employees.org> >>.
162 This module is free software; you can redistribute it and/or
163 modify it under the same terms as Perl itself. See L<perlartistic>.