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   519436 use strict;
  66         122  
  66         1787  
6 66     66   339 use warnings;
  66         139  
  66         1580  
7 66     66   5994 use version;
  66         45886  
  66         440  
8              
9 66         793 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   8178 )];
  66         570472  
36              
37 66         757 use MooseX::Types::Moose qw( Str Num ScalarRef ArrayRef Undef
38 66     66   1016351 HashRef FileHandle Object Int );
  66         82631  
39              
40 66     66   383097 use URI;
  66         17857  
  66         1700  
41 66     66   4591 use Path::Class::Dir;
  66         208901  
  66         1773  
42 66     66   461 use Path::Class::File;
  66         171  
  66         1320  
43 66     66   29624 use Term::ANSIColor;
  66         408174  
  66         6118  
44 66     66   142075 use Module::CoreList;
  66         5105220  
  66         1356  
45 66     66   71286 use IO::String;
  66         129149  
  66         2154  
46 66     66   539 use IO::Handle;
  66         157  
  66         3642  
47 66     66   380 use IO::File;
  66         132  
  66         8601  
48              
49 66     66   19757 use Pinto::Target;
  66         259  
  66         2712  
50 66     66   12875 use Pinto::Constants qw(:all);
  66         189  
  66         4447  
51              
52             #-----------------------------------------------------------------------------
53              
54             our $VERSION = '0.14'; # 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.14
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