File Coverage

blib/lib/MooseX/FileAttribute.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 4 100.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 0 2 0.0
total 43 45 95.5


line stmt bran cond sub pod time code
1             package MooseX::FileAttribute; # git description: v0.02-3-g9511cc8
2             # ABSTRACT: Sugar for classes that have file or directory attributes
3              
4 1     1   350566 use strict;
  1         2  
  1         27  
5 1     1   3 use warnings;
  1         1  
  1         20  
6 1     1   4 use Moose::Exporter;
  1         1  
  1         7  
7              
8             our $VERSION = '0.03';
9 1     1   64 use 5.008001;
  1         2  
10              
11 1     1   512 use MooseX::Types 0.11 -declare => ['ExistingFile', 'ExistingDir'];
  1         31255  
  1         8  
12 1     1   3576 use MooseX::Types::Moose qw(Str);
  1         9952  
  1         9  
13 1     1   4309 use MooseX::Types::Path::Class qw(File Dir);
  1         57629  
  1         5  
14              
15             Moose::Exporter->setup_import_methods(
16             with_meta => ['has_file', 'has_directory'],
17             # as_is => [qw/File Dir ExistingFile ExistingDir/],
18             );
19              
20             subtype ExistingFile, as File, where { -e $_->stringify },
21             message { "File '$_' must exist." };
22              
23             subtype ExistingDir, as Dir, where { -e $_->stringify && -d $_->stringify },
24             message { "Directory '$_' must exist" };
25              
26             coerce ExistingFile, from Str, via { Path::Class::file($_) };
27             coerce ExistingDir, from Str, via { Path::Class::dir($_) };
28              
29             sub has_file {
30 2     2 0 7099 my ($meta, $name, %options) = @_;
31              
32 2   100     12 my $must_exist = delete $options{must_exist} || 0;
33              
34 2 100       15 $meta->add_attribute(
35             $name,
36             is => 'ro',
37             isa => $must_exist ? ExistingFile : File,
38             coerce => 1,
39             %options,
40             );
41             }
42              
43             sub has_directory {
44 2     2 0 7104 my ($meta, $name, %options) = @_;
45              
46 2   100     13 my $must_exist = delete $options{must_exist} || 0;
47              
48 2 100       14 $meta->add_attribute(
49             $name,
50             is => 'ro',
51             isa => $must_exist ? ExistingDir : Dir,
52             coerce => 1,
53             %options,
54             );
55             }
56              
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             MooseX::FileAttribute - Sugar for classes that have file or directory attributes
68              
69             =head1 VERSION
70              
71             version 0.03
72              
73             =head1 SYNOPSIS
74              
75             Instead of C<has>, use C<has_file> or C<has_directory> to create
76             attributes that hold a file or directory:
77              
78             package Class;
79             use Moose;
80             use MooseX::FileAttribute;
81              
82             has_file 'foo' => (
83             documentation => 'path to the foo file',
84             must_exist => 1,
85             required => 1,
86             );
87              
88             has_directory 'bar' => (
89             required => 1,
90             );
91              
92             sub BUILD {
93             use autodie 'mkdir';
94             mkdir $self->bar unless -d $self->bar;
95             }
96              
97             Then use the class like you'd use any Moose class:
98              
99             my $c = Class->new( foo => '/quux/bar/foo', bar => '/quux/bar/' );
100             my $fh = $c->foo->openr; # string initarg promoted to Path::Class::File attribute
101             while( my $line = <$fh> ) { ... }
102              
103             =head1 DESCRIPTION
104              
105             I write a lot of classes that take files or directories on the
106             command-line. This results in a lot of boilerplate, usually:
107              
108             package Class;
109             use Moose;
110             use MooseX::Types::Path::Class qw(File);
111              
112             has 'foo' => (
113             is => 'ro',
114             isa => File,
115             coerce => 1,
116             required => 1,
117             );
118              
119             This module lets you save yourself some typing in this case:
120              
121             has_file 'foo' => ( required => 1 );
122              
123             These are exactly equivalent. C<has_directory> does the same thing
124             that C<has_file> does, but with a C<Dir> constraint.
125              
126             This module also defines two additional type constraints to ensure
127             that the specified file or directory exists and is a file or
128             directory. You can use these constraints instead of the defaults by
129             passing C<< must_exist => 1 >> to the C<has_*> function.
130              
131             =head1 BUGS
132              
133             The ExistingFile constraint will accept named pipes, ttys,
134             directories, etc., as files, as long as what's named exists on disk.
135             The ExistingDir constraint is more strict, only allowing directories.
136              
137             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-FileAttribute>
138             (or L<bug-MooseX-FileAttribute@rt.cpan.org|mailto:bug-MooseX-FileAttribute@rt.cpan.org>).
139              
140             I am also usually active on irc, as 'ether' at C<irc.perl.org>.
141              
142             =head1 AUTHOR
143              
144             Jonathan Rockway <jrockway@cpan.org>
145              
146             =head1 CONTRIBUTORS
147              
148             =for stopwords Karen Etheridge Jonathan Rockway Ken Crowell
149              
150             =over 4
151              
152             =item *
153              
154             Karen Etheridge <ether@cpan.org>
155              
156             =item *
157              
158             Jonathan Rockway <jon@jrock.us>
159              
160             =item *
161              
162             Ken Crowell <ken@oeuftete.com>
163              
164             =back
165              
166             =head1 COPYRIGHT AND LICENCE
167              
168             This software is copyright (c) 2009 by Jonathan Rockway.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut