1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
use 5.008001;
use strict;
use warnings;
package Types::Path::Tiny;
# ABSTRACT: Path::Tiny types and coercions for Moose and Moo
our $VERSION = '0.006';
use Path::Tiny qw();
use Type::Library 0.008 -base, -declare => qw( Path AbsPath File AbsFile Dir AbsDir );
use Type::Utils;
use Types::Standard qw( Str ArrayRef );
use Types::TypeTiny 0.004 StringLike => { -as => "Stringable" };
#<<<
class_type Path, { class => "Path::Tiny" };
declare AbsPath,
as Path, where { $_->is_absolute },
inline_as { $_[0]->parent->inline_check($_) . "&& ${_}->is_absolute" },
message {
is_Path($_) ? "Path '$_' is not absolute" : Path->get_message($_);
};
declare File,
as Path, where { $_->is_file },
inline_as { $_[0]->parent->inline_check($_) . "&& (-f $_)" },
message {
is_Path($_) ? "File '$_' does not exist" : Path->get_message($_);
};
declare Dir,
as Path, where { $_->is_dir },
inline_as { $_[0]->parent->inline_check($_) . "&& (-d $_)" },
message {
is_Path($_) ? "Directory '$_' does not exist" : Path->get_message($_);
};
declare AbsFile,
as intersection([AbsPath, File]),
message {
is_AbsPath($_) ? File->get_message($_) : AbsPath->get_message($_);
};
declare AbsDir,
as intersection([AbsPath, Dir]),
message {
is_AbsPath($_) ? Dir->get_message($_) : AbsPath->get_message($_);
};
#>>>
for my $type ( Path, File, Dir ) {
coerce(
$type,
from Str() => q{ Path::Tiny::path($_) },
from Stringable() => q{ Path::Tiny::path($_) },
from ArrayRef() => q{ Path::Tiny::path(@$_) },
);
}
for my $type ( AbsPath, AbsFile, AbsDir ) {
coerce(
$type,
from Path => q{ $_->absolute },
from Str() => q{ Path::Tiny::path($_)->absolute },
from Stringable() => q{ Path::Tiny::path($_)->absolute },
from ArrayRef() => q{ Path::Tiny::path(@$_)->absolute },
);
}
### optionally add Getopt option type (adapted from MooseX::Types:Path::Class
##eval { require MooseX::Getopt; };
##if ( !$@ ) {
## MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
## for ( 'Path::Tiny', Path );
##}
1;
# vim: ts=4 sts=4 sw=4 et:
__END__
=pod
=encoding UTF-8
=head1 NAME
Types::Path::Tiny - Path::Tiny types and coercions for Moose and Moo
=head1 VERSION
version 0.006
=head1 SYNOPSIS
Example with Moose:
### specification of type constraint with coercion
package Foo;
use Moose;
use Types::Path::Tiny qw/Path AbsPath/;
has filename => (
is => 'ro',
isa => Path,
coerce => 1,
);
has directory => (
is => 'ro',
isa => AbsPath,
coerce => 1,
);
### usage in code
Foo->new( filename => 'foo.txt' ); # coerced to Path::Tiny
Foo->new( directory => '.' ); # coerced to path('.')->absolute
Example with Moo:
### specification of type constraint with coercion
package Foo;
use Moo;
use Types::Path::Tiny qw/Path AbsPath/;
has 'directory' => (
is => 'rw',
isa => AbsPath,
required => 1,
coerce => AbsPath->coercion,
);
### usage in code
Foo->new( directory => '.' ); # coerced to path('.')->absolute
=head1 DESCRIPTION
This module provides L<Path::Tiny> types for Moose, Moo, etc.
It handles two important types of coercion:
=over 4
=item *
coercing objects with overloaded stringification
=item *
coercing to absolute paths
=back
It also can check to ensure that files or directories exist.
=for Pod::Coverage method_names_here
=head1 SUBTYPES
This module uses L<Type::Tiny> to define the following subtypes.
=head2 Path
C<Path> ensures an attribute is a L<Path::Tiny> object. Strings and
objects with overloaded stringification may be coerced.
=head2 AbsPath
C<AbsPath> is a subtype of C<Path> (above), but coerces to an absolute path.
=head2 File, AbsFile
These are just like C<Path> and C<AbsPath>, except they check C<-f> to ensure
the file actually exists on the filesystem.
=head2 Dir, AbsDir
These are just like C<Path> and C<AbsPath>, except they check C<-d> to ensure
the directory actually exists on the filesystem.
=head1 CAVEATS
=head2 Path vs File vs Dir
C<Path> just ensures you have a L<Path::Tiny> object.
C<File> and C<Dir> check the filesystem. Don't use them unless that's really
what you want.
=head2 Usage with File::Temp
Be careful if you pass in a File::Temp object. Because the argument is
stringified during coercion into a Path::Tiny object, no reference to the
original File::Temp argument is held. Be sure to hold an external reference to
it to avoid immediate cleanup of the temporary file or directory at the end of
the enclosing scope.
A better approach is to use Path::Tiny's own C<tempfile> or C<tempdir>
constructors, which hold the reference for you.
Foo->new( filename => Path::Tiny->tempfile );
=head1 SEE ALSO
=over 4
=item *
L<Path::Tiny>
=item *
L<Moose::Manual::Types>
=back
=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
=head2 Bugs / Feature Requests
Please report any bugs or feature requests through the issue tracker
at L<https://github.com/dagolden/types-path-tiny/issues>.
You will be notified automatically of any progress on your issue.
=head2 Source Code
This is open source software. The code repository is available for
public review and contribution under the terms of the license.
L<https://github.com/dagolden/types-path-tiny>
git clone https://github.com/dagolden/types-path-tiny.git
=head1 AUTHOR
David Golden <dagolden@cpan.org>
=head1 CONTRIBUTORS
=for stopwords Hobbestigrou Toby Inkster
=over 4
=item *
Hobbestigrou <hobbestigrou@erakis.eu>
=item *
Hobbestigrou <natal.ngetal@novapost.fr>
=item *
Toby Inkster <tobyink@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by David Golden.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|