File Coverage

blib/lib/Specio/Library/Path/Tiny.pm
Criterion Covered Total %
statement 30 30 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod n/a
total 40 40 100.0


line stmt bran cond sub pod time code
1             ## no critic (Modules::ProhibitExcessMainComplexity)
2             package Specio::Library::Path::Tiny;
3              
4 1     1   148247 use strict;
  1         1  
  1         23  
5 1     1   4 use warnings;
  1         1  
  1         31  
6              
7             our $VERSION = '0.04';
8              
9 1     1   4 use overload ();
  1         1  
  1         14  
10 1     1   6 use Path::Tiny 0.087;
  1         20  
  1         43  
11 1     1   4 use Scalar::Util qw( blessed );
  1         0  
  1         37  
12 1     1   460 use Specio 0.29 ();
  1         102  
  1         16  
13 1     1   3 use Specio::Declare;
  1         1  
  1         7  
14 1     1   157 use Specio::Library::Builtins;
  1         1  
  1         7  
15 1     1   4487 use Specio::PartialDump qw( partial_dump );
  1         1  
  1         47  
16              
17 1     1   10 use parent 'Specio::Exporter';
  1         1  
  1         5  
18              
19             my $not_blessed = sub {
20             return blessed $_[0] ? q{} : "$_[1] is not an object";
21             };
22              
23             my $not_path_tiny = sub {
24             return $_[0]->isa('Path::Tiny')
25             ? q{}
26             : "$_[1] is not a Path::Tiny object";
27             };
28              
29             my $not_absolute = sub {
30             return $_[0]->is_absolute ? q{} : "$_[0] is not an absolute path";
31             };
32              
33             my $not_real = sub {
34             return $_[0]->realpath eq $_[0] ? q{} : "$_[0] is not a real path";
35             };
36              
37             my $not_file = sub {
38             return $_[0]->is_file ? q{} : "$_[0] is not a file on disk";
39             };
40              
41             my $not_dir = sub {
42             return $_[0]->is_dir ? q{} : "$_[0] is not a directory on disk";
43             };
44              
45             declare(
46             'Path',
47             parent => object_isa_type('Path::Tiny'),
48             message_generator => sub {
49             my $dump = partial_dump( $_[1] );
50             return $not_blessed->( $_[1], $dump )
51             || $not_path_tiny->( $_[1], $dump );
52             },
53             );
54              
55             declare(
56             'AbsPath',
57             parent => t('Path'),
58             inline => sub {
59             return sprintf(
60             '( %s && %s->is_absolute )',
61             $_[0]->parent->inline_check( $_[1] ),
62             $_[1]
63             );
64             },
65             message_generator => sub {
66             my $dump = partial_dump( $_[1] );
67             return
68             $not_blessed->( $_[1], $dump )
69             || $not_path_tiny->( $_[1], $dump )
70             || $not_absolute->( $_[1], $dump );
71             },
72             );
73              
74             declare(
75             'RealPath',
76             parent => t('Path'),
77             inline => sub {
78             return sprintf(
79             '( %s && %s->realpath eq %s )',
80             $_[0]->parent->inline_check( $_[1] ),
81             $_[1], $_[1]
82             );
83             },
84             message_generator => sub {
85             my $dump = partial_dump( $_[1] );
86             return
87             $not_blessed->( $_[1], $dump )
88             || $not_path_tiny->( $_[1], $dump )
89             || $not_real->( $_[1], $dump );
90             },
91             );
92              
93             declare(
94             'File',
95             parent => t('Path'),
96             inline => sub {
97             return sprintf(
98             '( %s && %s->is_file )',
99             $_[0]->parent->inline_check( $_[1] ),
100             $_[1]
101             );
102             },
103             message_generator => sub {
104             my $dump = partial_dump( $_[1] );
105             return
106             $not_blessed->( $_[1], $dump )
107             || $not_path_tiny->( $_[1], $dump )
108             || $not_file->( $_[1], $dump );
109             },
110             );
111              
112             declare(
113             'AbsFile',
114             parent => t('Path'),
115             inline => sub {
116             return sprintf(
117             '( %s && %s->is_file && %s->is_absolute )',
118             $_[0]->parent->inline_check( $_[1] ),
119             $_[1], $_[1]
120             );
121             },
122             message_generator => sub {
123             my $dump = partial_dump( $_[1] );
124             return
125             $not_blessed->( $_[1], $dump )
126             || $not_path_tiny->( $_[1], $dump )
127             || $not_file->( $_[1], $dump )
128             || $not_absolute->( $_[1], $dump );
129             },
130             );
131              
132             declare(
133             'RealFile',
134             parent => t('Path'),
135             inline => sub {
136             return sprintf(
137             '( %s && %s->is_file && %s->realpath eq %s )',
138             $_[0]->parent->inline_check( $_[1] ),
139             $_[1], $_[1], $_[1]
140             );
141             },
142             message_generator => sub {
143             my $dump = partial_dump( $_[1] );
144             return
145             $not_blessed->( $_[1], $dump )
146             || $not_path_tiny->( $_[1], $dump )
147             || $not_file->( $_[1], $dump )
148             || $not_real->( $_[1], $dump );
149             },
150             );
151              
152             declare(
153             'Dir',
154             parent => t('Path'),
155             inline => sub {
156             return sprintf(
157             '( %s && %s->is_dir )',
158             $_[0]->parent->inline_check( $_[1] ),
159             $_[1]
160             );
161             },
162             message_generator => sub {
163             my $dump = partial_dump( $_[1] );
164             return
165             $not_blessed->( $_[1], $dump )
166             || $not_path_tiny->( $_[1], $dump )
167             || $not_dir->( $_[1], $dump );
168             },
169             );
170              
171             declare(
172             'AbsDir',
173             parent => t('Path'),
174             inline => sub {
175             return sprintf(
176             '( %s && %s->is_dir && %s->is_absolute )',
177             $_[0]->parent->inline_check( $_[1] ),
178             $_[1], $_[1],
179             );
180             },
181             message_generator => sub {
182             my $dump = partial_dump( $_[1] );
183             return
184             $not_blessed->( $_[1], $dump )
185             || $not_path_tiny->( $_[1], $dump )
186             || $not_dir->( $_[1], $dump )
187             || $not_absolute->( $_[1], $dump );
188             },
189             );
190              
191             declare(
192             'RealDir',
193             parent => t('Path'),
194             inline => sub {
195             return sprintf(
196             '( %s && %s->is_dir && %s->realpath eq %s )',
197             $_[0]->parent->inline_check( $_[1] ),
198             $_[1], $_[1], $_[1]
199             );
200             },
201             message_generator => sub {
202             my $dump = partial_dump( $_[1] );
203             return
204             $not_blessed->( $_[1], $dump )
205             || $not_path_tiny->( $_[1], $dump )
206             || $not_dir->( $_[1], $dump )
207             || $not_real->( $_[1], $dump );
208             },
209             );
210              
211             for my $type ( map { t($_) } qw( Path File Dir ) ) {
212             coerce(
213             $type,
214             from => t('Str'),
215             inline => sub {"Path::Tiny::path( $_[1] )"},
216             );
217              
218             coerce(
219             $type,
220             from => t('ArrayRef'),
221             inline => sub {"Path::Tiny::path( \@{ $_[1] } )"},
222             );
223             }
224              
225             for my $type ( map { t($_) } qw( AbsPath AbsFile AbsDir ) ) {
226             coerce(
227             $type,
228             from => t('Path'),
229             inline => sub { sprintf( '%s->absolute', $_[1] ) },
230             );
231              
232             coerce(
233             $type,
234             from => t('Str'),
235             inline =>
236             sub { sprintf( 'Path::Tiny::path( %s )->absolute', $_[1] ) },
237             );
238              
239             coerce(
240             $type,
241             from => t('ArrayRef'),
242             inline =>
243             sub { sprintf( 'Path::Tiny::path( @{ %s } )->absolute', $_[1] ) },
244             );
245             }
246              
247             for my $type ( map { t($_) } qw( RealPath RealFile RealDir ) ) {
248             coerce(
249             $type,
250             from => t('Path'),
251             inline => sub { sprintf( '%s->realpath', $_[1] ) },
252             );
253              
254             coerce(
255             $type,
256             from => t('Str'),
257             inline =>
258             sub { sprintf( 'Path::Tiny::path( %s )->realpath', $_[1] ) },
259             );
260              
261             coerce(
262             $type,
263             from => t('ArrayRef'),
264             inline =>
265             sub { sprintf( 'Path::Tiny::path( @{ %s } )->realpath', $_[1] ) },
266             );
267             }
268              
269             1;
270              
271             # ABSTRACT: Path::Tiny types and coercions for Specio
272              
273             __END__
274              
275             =pod
276              
277             =encoding UTF-8
278              
279             =head1 NAME
280              
281             Specio::Library::Path::Tiny - Path::Tiny types and coercions for Specio
282              
283             =head1 VERSION
284              
285             version 0.04
286              
287             =head1 SYNOPSIS
288              
289             use Specio::Library::Path::Tiny;
290              
291             has path => ( isa => t('Path') );
292              
293             =head1 DESCRIPTION
294              
295             This library provides a set of L<Path::Tiny> types and coercions for
296             L<Specio>. These types can be used with L<Moose>, L<Moo>,
297             L<Params::ValidationCompiler>, and other modules.
298              
299             =head1 TYPES
300              
301             This library provides the following types:
302              
303             =head2 Path
304              
305             A L<Path::Tiny> object.
306              
307             Will be coerced from a string or arrayref via C<Path::Tiny::path>.
308              
309             =head2 AbsPath
310              
311             A L<Path::Tiny> object where C<< $path->is_absolute >> returns true.
312              
313             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
314             call to C<< $path->absolute >>.
315              
316             =head2 RealPath
317              
318             A L<Path::Tiny> object where C<< $path->realpath eq $path >>.
319              
320             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
321             call to C<< $path->realpath >>.
322              
323             =head2 File
324              
325             A L<Path::Tiny> object which is a file on disk according to C<< $path->is_file
326             >>.
327              
328             Will be coerced from a string or arrayref via C<Path::Tiny::path>.
329              
330             =head2 AbsFile
331              
332             A L<Path::Tiny> object which is a file on disk according to C<< $path->is_file
333             >> where C<< $path->is_absolute >> returns true.
334              
335             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
336             call to C<< $path->absolute >>.
337              
338             =head2 RealFile
339              
340             A L<Path::Tiny> object which is a file on disk according to C<< $path->is_file
341             >> where C<< $path->realpath eq $path >>.
342              
343             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
344             call to C<< $path->realpath >>.
345              
346             =head2 Dir
347              
348             A L<Path::Tiny> object which is a directory on disk according to C<<
349             $path->is_dir >>.
350              
351             Will be coerced from a string or arrayref via C<Path::Tiny::path>.
352              
353             =head2 AbsDir
354              
355             A L<Path::Tiny> object which is a directory on disk according to C<<
356             $path->is_dir >> where C<< $path->is_absolute >> returns true.
357              
358             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
359             call to C<< $path->absolute >>.
360              
361             =head2 RealDir
362              
363             A L<Path::Tiny> object which is a directory on disk according to C<<
364             $path->is_dir >> where C<< $path->realpath eq $path >>.
365              
366             Will be coerced from a string or arrayref via C<Path::Tiny::path> followed by
367             call to C<< $path->realpath >>.
368              
369             =head1 CREDITS
370              
371             The vast majority of the code in this distribution comes from David Golden's
372             L<Types::Path::Tiny> distribution.
373              
374             =head1 SUPPORT
375              
376             Bugs may be submitted through L<the RT bug tracker|http://rt.cpan.org/Public/Dist/Display.html?Name=Specio-Library-Path-Tiny>
377             (or L<bug-specio-library-path-tiny@rt.cpan.org|mailto:bug-specio-library-path-tiny@rt.cpan.org>).
378              
379             I am also usually active on IRC as 'drolsky' on C<irc://irc.perl.org>.
380              
381             =head1 DONATIONS
382              
383             If you'd like to thank me for the work I've done on this module, please
384             consider making a "donation" to me via PayPal. I spend a lot of free time
385             creating free software, and would appreciate any support you'd care to offer.
386              
387             Please note that B<I am not suggesting that you must do this> in order for me
388             to continue working on this particular software. I will continue to do so,
389             inasmuch as I have in the past, for as long as it interests me.
390              
391             Similarly, a donation made in this way will probably not make me work on this
392             software much more, unless I get so many donations that I can consider working
393             on free software full time (let's all have a chuckle at that together).
394              
395             To donate, log into PayPal and send money to autarch@urth.org, or use the
396             button at L<http://www.urth.org/~autarch/fs-donation.html>.
397              
398             =head1 AUTHOR
399              
400             Dave Rolsky <autarch@urth.org>
401              
402             =head1 COPYRIGHT AND LICENSE
403              
404             This software is Copyright (c) 2016 by Dave Rolsky.
405              
406             This is free software, licensed under:
407              
408             The Apache License, Version 2.0, January 2004
409              
410             =cut