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