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