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