File Coverage

blib/lib/Pinto/Types.pm
Criterion Covered Total %
statement 45 45 100.0
branch n/a
condition n/a
subroutine 15 15 100.0
pod n/a
total 60 60 100.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Moose types used within Pinto
2              
3             package Pinto::Types;
4              
5 66     66   455925 use strict;
  66         120  
  66         1826  
6 66     66   326 use warnings;
  66         132  
  66         1659  
7 66     66   5680 use version;
  66         34582  
  66         410  
8              
9 66         828 use MooseX::Types -declare => [ qw(
10             ANSIColor
11             ANSIColorPalette
12             AuthorID
13             DiffStyle
14             Dir
15             DistributionTarget
16             DistributionTargetList
17             File
18             FileList
19             Io
20             PackageTarget
21             PackageTargetList
22             PerlVersion
23             PropertyName
24             RevisionHead
25             RevisionID
26             StackAll
27             StackDefault
28             StackName
29             StackObject
30             Target
31             TargetList
32             Uri
33             Username
34             Version
35 66     66   8067 )];
  66         546134  
36              
37 66         703 use MooseX::Types::Moose qw( Str Num ScalarRef ArrayRef Undef
38 66     66   1000817 HashRef FileHandle Object Int );
  66         80814  
39              
40 66     66   387378 use URI;
  66         14420  
  66         1617  
41 66     66   4384 use Path::Class::Dir;
  66         208224  
  66         1686  
42 66     66   486 use Path::Class::File;
  66         143  
  66         1279  
43 66     66   28941 use Term::ANSIColor;
  66         413496  
  66         5765  
44 66     66   140494 use Module::CoreList;
  66         5075388  
  66         1538  
45 66     66   68099 use IO::String;
  66         131056  
  66         2218  
46 66     66   565 use IO::Handle;
  66         152  
  66         3538  
47 66     66   369 use IO::File;
  66         140  
  66         8476  
48              
49 66     66   19280 use Pinto::Target;
  66         276  
  66         2480  
50 66     66   9531 use Pinto::Constants qw(:all);
  66         180  
  66         4256  
51              
52             #-----------------------------------------------------------------------------
53              
54             our $VERSION = '0.13'; # VERSION
55              
56             #-----------------------------------------------------------------------------
57              
58             subtype AuthorID, as Str,
59             where { $_ =~ $PINTO_AUTHOR_REGEX },
60             message { 'The author id (' . ( defined() ? $_ : 'undef' ) . ') must match /^[A-Z]{2}[-A-Z0-9]*$/' };
61              
62             coerce AuthorID, from Str, via { uc $_ };
63              
64             #-----------------------------------------------------------------------------
65              
66             subtype Username, as Str,
67             where { $_ =~ $PINTO_USERNAME_REGEX },
68             message { 'The username (' . ( defined() ? $_ : 'undef' ) . ') must be alphanumeric' };
69              
70             #-----------------------------------------------------------------------------
71              
72             subtype StackName, as Str,
73             where { $_ =~ $PINTO_STACK_NAME_REGEX },
74             message { 'The stack name (' . ( defined() ? $_ : 'undef' ) . ') must be alphanumeric' };
75              
76             #-----------------------------------------------------------------------------
77              
78             subtype StackAll, as Str,
79             where { $_ eq $PINTO_STACK_NAME_ALL },
80             message {qq{The stack name must be '$PINTO_STACK_NAME_ALL'}};
81              
82             #-----------------------------------------------------------------------------
83              
84             subtype StackDefault, as Undef;
85              
86             #-----------------------------------------------------------------------------
87              
88             class_type StackObject,
89             { class => 'Pinto::Schema::Result::Stack' };
90              
91             #-----------------------------------------------------------------------------
92              
93             subtype PropertyName, as Str,
94             where { $_ =~ $PINTO_PROPERTY_NAME_REGEX },
95             message { 'The property name (' . ( defined() ? $_ : 'undef' ) . 'must be alphanumeric' };
96              
97             #-----------------------------------------------------------------------------
98              
99             class_type Version,
100             { class => 'version' };
101              
102             coerce Version,
103             from Str, via { version->parse($_) };
104              
105             coerce Version,
106             from Num, via { version->parse($_) };
107              
108             #-----------------------------------------------------------------------------
109              
110             subtype PerlVersion, as Object,
111             where { $_->isa('version') && exists $Module::CoreList::version{ $_->numify + 0 } },
112             message {"perl version ($_) is unknown to me; try updating Pinto's copy of Module::CoreList"};
113              
114             coerce PerlVersion,
115             from Str, via { version->parse($_) };
116              
117             coerce PerlVersion,
118             from Num, via { version->parse($_) };
119              
120             #-----------------------------------------------------------------------------
121              
122             subtype ANSIColor, as Str,
123             where { Term::ANSIColor::colorvalid($_) },
124             message { 'The color name (' . ( defined() ? $_ : 'undef' ) . 'is not valid' };
125              
126             #-----------------------------------------------------------------------------
127              
128             subtype ANSIColorPalette, as ArrayRef[ANSIColor],
129             where { @{$_} == 3 },
130             message {'Must be exactly three colors'};
131              
132             #-----------------------------------------------------------------------------
133              
134             class_type Uri,
135             { class => 'URI' };
136              
137             coerce Uri,
138             from Str, via { URI->new($_) };
139              
140             #-----------------------------------------------------------------------------
141              
142             class_type Dir,
143             { class => 'Path::Class::Dir' };
144              
145             # file:/// URIs will be converted to plain paths
146              
147             coerce Dir,
148             from Str, via { $_ =~ s{^file://}{}; Path::Class::Dir->new($_) };
149              
150             #-----------------------------------------------------------------------------
151              
152             class_type File,
153             { class => 'Path::Class::File' };
154              
155             # file:/// URIs will be converted to plain paths
156              
157             coerce File,
158             from Str, via { $_ =~ s{^file://}{}; Path::Class::File->new($_) };
159              
160             #-----------------------------------------------------------------------------
161              
162             subtype FileList, as ArrayRef [File];
163              
164             coerce FileList,
165             from File, via { [ $_ ] },
166             from Str, via { s{^file://}{}; [ Path::Class::File->new($_) ] },
167             from ArrayRef[Str], via { [ map { s{^file://}{}; Path::Class::File->new($_) } @$_ ] };
168              
169             #-----------------------------------------------------------------------------
170              
171             class_type PackageTarget, { class => 'Pinto::Target::Package' };
172              
173             coerce PackageTarget,
174             from Str, via { Pinto::Target->new($_) },
175             from HashRef, via { Pinto::Target->new($_) };
176              
177             #-----------------------------------------------------------------------------
178              
179             class_type DistributionTarget, { class => 'Pinto::Target::Distribution' };
180              
181             coerce DistributionTarget,
182             from Str, via { Pinto::Target->new($_) },
183             from HashRef, via { Pinto::Target->new($_) };
184              
185             #-----------------------------------------------------------------------------
186              
187             subtype TargetList, as ArrayRef [ PackageTarget | DistributionTarget ]; ## no critic qw(ProhibitBitwiseOperators);
188              
189             coerce TargetList,
190             from PackageTarget, via { [ $_ ] },
191             from DistributionTarget, via { [ $_ ] },
192             from Str, via { [ Pinto::Target->new($_) ] },
193             from ArrayRef[Str], via { [ map { Pinto::Target->new($_) } @$_ ] };
194              
195             #-----------------------------------------------------------------------------
196              
197             subtype DistributionTargetList, as ArrayRef [DistributionTarget]; ## no critic qw(ProhibitBitwiseOperators);
198              
199             coerce DistributionTargetList,
200             from DistributionTarget, via { [$_] },
201             from Str, via { [ Pinto::Target::Distribution->new($_) ] },
202             from ArrayRef[Str], via { [ map { Pinto::Target::Distribution->new($_) } @$_ ] };
203              
204             #-----------------------------------------------------------------------------
205              
206             subtype PackageTargetList, as ArrayRef [PackageTarget]; ## no critic qw(ProhibitBitwiseOperators);
207              
208             coerce PackageTargetList,
209             from DistributionTarget, via { [ $_ ] },
210             from Str, via { [ Pinto::Target::Package->new($_) ] },
211             from ArrayRef[Str], via { [ map { Pinto::Target::Package->new($_) } @$_ ] };
212              
213             #-----------------------------------------------------------------------------
214              
215             subtype Io, as Object;
216              
217             coerce Io,
218             from Str, via { my $fh = IO::File->new(); $fh->open($_); return $fh },
219             from File, via { my $fh = IO::File->new(); $fh->open("$_"); return $fh },
220             from ArrayRef, via { IO::Handle->new_from_fd(@$_) },
221             from ScalarRef, via { IO::String->new( ${$_} ) };
222              
223             #-----------------------------------------------------------------------------
224              
225             subtype RevisionID, as Str,
226             where { $_ =~ $PINTO_REVISION_ID_REGEX and length($_) >= 4 },
227             message { 'The revision id (' . ( defined() ? $_ : 'undef' ) . ') must be a hexadecimal string of 4 or more chars' };
228              
229             coerce RevisionID, from Str, via { lc $_ };
230              
231             #-----------------------------------------------------------------------------
232              
233             subtype RevisionHead, as Undef;
234              
235             #-----------------------------------------------------------------------------
236              
237             enum DiffStyle, [$PINTO_DIFF_STYLE_CONCISE, $PINTO_DIFF_STYLE_DETAILED];
238              
239             #-----------------------------------------------------------------------------
240              
241             1;
242              
243             __END__
244              
245             =pod
246              
247             =encoding UTF-8
248              
249             =for :stopwords Jeffrey Ryan Thalhammer
250              
251             =head1 NAME
252              
253             Pinto::Types - Moose types used within Pinto
254              
255             =head1 VERSION
256              
257             version 0.13
258              
259             =head1 AUTHOR
260              
261             Jeffrey Ryan Thalhammer <jeff@stratopan.com>
262              
263             =head1 COPYRIGHT AND LICENSE
264              
265             This software is copyright (c) 2015 by Jeffrey Ryan Thalhammer.
266              
267             This is free software; you can redistribute it and/or modify it under
268             the same terms as the Perl 5 programming language system itself.
269              
270             =cut