add Existing* types
[gitmo/MooseX-Types-Path-Class.git] / lib / MooseX / Types / Path / Class.pm
CommitLineData
fd56ddb6 1package MooseX::Types::Path::Class;
2
3use warnings FATAL => 'all';
4use strict;
5
192bdd61 6our $VERSION = '0.05';
52e426e5 7our $AUTHORITY = 'cpan:THEPLER';
fd56ddb6 8
fd56ddb6 9use Path::Class ();
d752957e 10# TODO: export dir() and file() from Path::Class? (maybe)
fd56ddb6 11
12use MooseX::Types
4b3d0bfc 13 -declare => [qw( Dir File ExistingDir ExistingFile )];
fd56ddb6 14
dae5e463 15use MooseX::Types::Moose qw(Str ArrayRef);
fd56ddb6 16
dae5e463 17class_type('Path::Class::Dir');
18class_type('Path::Class::File');
fd56ddb6 19
dae5e463 20subtype Dir, as 'Path::Class::Dir';
21subtype File, as 'Path::Class::File';
fd56ddb6 22
4b3d0bfc 23subtype ExistingFile, as File, where { -e $_->stringify },
24 message { "File '$_' must exist." };
25
26subtype ExistingDir, as Dir, where { -e $_->stringify && -d $_->stringify },
27 message { "Directory '$_' must exist" };
28
29for my $type ( 'Path::Class::Dir', Dir, ExistingDir ) {
52e426e5 30 coerce $type,
31 from Str, via { Path::Class::Dir->new($_) },
32 from ArrayRef, via { Path::Class::Dir->new(@$_) };
52e426e5 33}
fd56ddb6 34
4b3d0bfc 35for my $type ( 'Path::Class::File', File, ExistingFile ) {
52e426e5 36 coerce $type,
37 from Str, via { Path::Class::File->new($_) },
38 from ArrayRef, via { Path::Class::File->new(@$_) };
dae5e463 39}
fd56ddb6 40
dae5e463 41# optionally add Getopt option type
dae5e463 42eval { require MooseX::Getopt; };
2b21d04e 43if ( !$@ ) {
dae5e463 44 MooseX::Getopt::OptionTypeMap->add_option_type_to_map( $_, '=s', )
4b3d0bfc 45 for ( 'Path::Class::Dir', 'Path::Class::File', Dir, File, ExistingDir, ExistingFile );
52e426e5 46}
fd56ddb6 47
481;
49__END__
50
d752957e 51
fd56ddb6 52=head1 NAME
53
54MooseX::Types::Path::Class - A Path::Class type library for Moose
55
56
57=head1 SYNOPSIS
58
59 package MyClass;
60 use Moose;
dae5e463 61 use MooseX::Types::Path::Class;
62 with 'MooseX::Getopt'; # optional
fd56ddb6 63
64 has 'dir' => (
65 is => 'ro',
dae5e463 66 isa => 'Path::Class::Dir',
fd56ddb6 67 required => 1,
68 coerce => 1,
69 );
70
71 has 'file' => (
72 is => 'ro',
dae5e463 73 isa => 'Path::Class::File',
fd56ddb6 74 required => 1,
75 coerce => 1,
76 );
77
78 # these attributes are coerced to the
79 # appropriate Path::Class objects
255a36e7 80 MyClass->new( dir => '/some/directory/', file => '/some/file' );
fd56ddb6 81
4b3d0bfc 82
fd56ddb6 83=head1 DESCRIPTION
84
dae5e463 85MooseX::Types::Path::Class creates common L<Moose> types,
86coercions and option specifications useful for dealing
87with L<Path::Class> objects as L<Moose> attributes.
fd56ddb6 88
dae5e463 89Coercions (see L<Moose::Util::TypeConstraints>) are made
fd56ddb6 90from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and
dae5e463 91L<Path::Class::File> objects. If you have L<MooseX::Getopt> installed,
92the Getopt option type ("=s") will be added for both
93L<Path::Class::Dir> and L<Path::Class::File>.
fd56ddb6 94
d752957e 95
fd56ddb6 96=head1 EXPORTS
97
dae5e463 98None of these are exported by default. They are provided via
99L<MooseX::Types>.
fd56ddb6 100
101=over
102
dae5e463 103=item Dir, File
104
105These exports can be used instead of the full class names. Example:
106
107 package MyClass;
108 use Moose;
109 use MooseX::Types::Path::Class qw(Dir File);
110
111 has 'dir' => (
112 is => 'ro',
113 isa => Dir,
114 required => 1,
115 coerce => 1,
116 );
117
118 has 'file' => (
119 is => 'ro',
120 isa => File,
121 required => 1,
122 coerce => 1,
123 );
124
4b3d0bfc 125=item ExistingDir, ExistingFile
126
127Like File and Dir, but the files or directories must exist on disk
128when the type is checked, and the object on disk must be a file (for
129ExistingFile) or directory (for ExistingDir).
130
dae5e463 131Note that there are no quotes around Dir or File.
132
133=item is_Dir($value), is_File($value)
134
135Returns true or false based on whether $value is a valid Dir or File.
136
137=item to_Dir($value), to_File($value)
fd56ddb6 138
dae5e463 139Attempts to coerce $value to a Dir or File. Returns the coerced value
4d069f3e 140or false if the coercion failed.
fd56ddb6 141
142=back
143
144
145=head1 DEPENDENCIES
146
dae5e463 147L<Moose>, L<MooseX::Types>, L<Path::Class>
fd56ddb6 148
149
150=head1 BUGS AND LIMITATIONS
151
192bdd61 152If you find a bug please either email the author, or add
52e426e5 153the bug to cpan-RT L<http://rt.cpan.org>.
fd56ddb6 154
155
156=head1 AUTHOR
157
158Todd Hepler C<< <thepler@employees.org> >>
159
160
161=head1 LICENCE AND COPYRIGHT
162
192bdd61 163Copyright (c) 2007-2008, Todd Hepler C<< <thepler@employees.org> >>.
fd56ddb6 164
165This module is free software; you can redistribute it and/or
166modify it under the same terms as Perl itself. See L<perlartistic>.
167
168