4adfdeb082ea327690f0e0218e3e22c984b66194
[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 our $VERSION = '0.02';
7
8 use MooseX::Getopt;
9 use Path::Class ();
10
11 use MooseX::Types
12     -declare => [qw( Dir File )];
13
14 use MooseX::Types::Moose qw(Object Str ArrayRef);
15
16 subtype Dir,
17     as Object, where { $_->isa('Path::Class::Dir') };
18
19 coerce Dir,
20     from Str,      via { Path::Class::Dir->new($_) },
21     from ArrayRef, via { Path::Class::Dir->new(@$_) };
22
23 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
24     Dir, '=s',
25 );
26
27
28 subtype 'Path::Class::Dir',
29     as Object, where { $_->isa('Path::Class::Dir') };
30
31 coerce 'Path::Class::Dir',
32     from Str,      via { Path::Class::Dir->new($_) },
33     from ArrayRef, via { Path::Class::Dir->new(@$_) };
34
35 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
36     'Path::Class::Dir', '=s',
37 );
38
39
40 subtype File,
41     as Object, where { $_->isa('Path::Class::File') };
42
43 coerce File,
44     from Str,      via { Path::Class::File->new($_) },
45     from ArrayRef, via { Path::Class::File->new(@$_) };
46
47 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
48     File, '=s',
49 );
50
51
52 subtype 'Path::Class::File',
53     as Object, where { $_->isa('Path::Class::File') };
54
55 coerce 'Path::Class::File',
56     from Str,      via { Path::Class::File->new($_) },
57     from ArrayRef, via { Path::Class::File->new(@$_) };
58
59 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
60     'Path::Class::File', '=s',
61 );
62
63
64 1;
65 __END__
66
67 =head1 NAME
68
69 MooseX::Types::Path::Class - A Path::Class type library for Moose
70
71
72 =head1 SYNOPSIS
73
74   package MyClass;
75   use Moose;
76   use MooseX::Types::Path::Class qw( Dir File );
77   with 'MooseX::Getopt';      # if you want the Getopt Role
78
79   has 'dir' => (
80       is       => 'ro',
81       isa      => Dir,
82       required => 1,
83       coerce   => 1,
84   );
85
86   has 'file' => (
87       is       => 'ro',
88       isa      => File,
89       required => 1,
90       coerce   => 1,
91   );
92
93   # these attributes are coerced to the
94   # appropriate Path::Class objects
95   MyClass->new( dir => '/some/directory/', file => '/some/file' );
96
97   
98 =head1 DESCRIPTION
99
100 This is a utility that creates common L<Moose> subtypes, coercions and
101 option specifications useful for dealing with L<Path::Class> objects
102 as L<Moose> attributes.
103
104 This module constructs coercions (see L<Moose::Util::TypeConstraints>)
105 from both 'Str' and 'ArrayRef' to both L<Path::Class::Dir> and
106 L<Path::Class::File> objects.  It also adds the Getopt option type
107 ("=s") for both L<Path::Class::Dir> and L<Path::Class::File>
108 (see L<MooseX::Getopt>).
109
110 This is just meant to be a central place for these constructs, so you
111 don't have to worry about whether they've been created or not, and you're
112 not tempted to copy them into yet another class (like I was).
113
114 =head1 EXPORTS
115
116 See L<MooseX::Types> for how these exports work.
117
118 =over
119
120 =item Dir is_Dir to_Dir
121
122 =item File is_File to_File
123
124 =back
125
126
127 =head1 DEPENDENCIES
128
129 L<Moose>, L<MooseX::Types>, L<MooseX::Getopt>, L<Path::Class>
130
131
132 =head1 BUGS AND LIMITATIONS
133
134 No bugs have been reported.
135
136
137 =head1 AUTHOR
138
139 Todd Hepler  C<< <thepler@employees.org> >>
140
141
142 =head1 LICENCE AND COPYRIGHT
143
144 Copyright (c) 2007, Todd Hepler C<< <thepler@employees.org> >>. All rights reserved.
145
146 This module is free software; you can redistribute it and/or
147 modify it under the same terms as Perl itself. See L<perlartistic>.
148
149