File Coverage

blib/lib/Params/Registry/Types.pm
Criterion Covered Total %
statement 48 71 67.6
branch 0 18 0.0
condition 0 3 0.0
subroutine 17 21 80.9
pod 0 1 0.0
total 65 114 57.0


line stmt bran cond sub pod time code
1             package Params::Registry::Types;
2              
3 1     1   102587 use 5.010;
  1         4  
4 1     1   6 use strict;
  1         3  
  1         35  
5 1     1   6 use warnings FATAL => 'all';
  1         2  
  1         51  
6              
7 1     1   635 use Moose;
  1         471653  
  1         6  
8 1     1   8166 use namespace::autoclean;
  1         8179  
  1         4  
9              
10 1     1   542 use Tie::IxHash;
  1         2967  
  1         34  
11              
12 1     1   494 use Set::Scalar;
  1         10801  
  1         49  
13 1     1   618 use Set::Infinite;
  1         31399  
  1         68  
14              
15 1     1   1050 use DateTime;
  1         524385  
  1         54  
16 1     1   679 use DateTime::Span;
  1         17722  
  1         28  
17 1     1   8 use DateTime::SpanSet;
  1         2  
  1         24  
18              
19 1     1   7 use Moose::Util::TypeConstraints qw(class_type);
  1         4  
  1         14  
20              
21             our @TYPES;
22              
23             BEGIN {
24 1     1   753 @TYPES = qw(Type Template TemplateSet Dependency Format XSDdate
25             XSDgYearMonth XSDgYear XSDgMonth XSDgDay DateSpan
26             DateSpanSet DateRange Currency Decimal3 XSDBool
27             NumberRange Set IntSet LCToken UCToken TokenSet
28             PositiveInt NegativeInt NonPositiveInt
29             NonNegativeInt);
30             }
31              
32 1         8 use MooseX::Types::Moose qw(ClassName RoleName ArrayRef HashRef CodeRef
33 1     1   507 Undef Maybe Bool Num Int Str);
  1         56397  
34              
35 1     1   6826 use MooseX::Types -declare => [@TYPES];
  1         3  
  1         7  
36              
37             # for Set::Infinite
38 1     1   18743 use constant INF => Set::Infinite->inf;
  1         2  
  1         12  
39 1     1   91 use constant NEG_INF => Set::Infinite->minus_inf;
  1         3  
  1         6  
40              
41             =head1 NAME
42              
43             Params::Registry::Types - Types for Params::Registry
44              
45             =head1 VERSION
46              
47             Version 0.03
48              
49             =cut
50              
51             our $VERSION = '0.03';
52              
53              
54             =head1 SYNOPSIS
55              
56             use Params::Registry::Types qw(:all);
57              
58             =head1 TYPES
59              
60             =head2 Type
61              
62             This is the type for types. XZibit-approved.
63              
64             =cut
65              
66             class_type 'MooseX::Types::UndefinedType';
67             class_type 'MooseX::Types::TypeDecorator';
68             class_type 'Moose::Meta::TypeConstraint';
69              
70             subtype Type, as join('|', qw( MooseX::Types::UndefinedType
71             MooseX::Types::TypeDecorator
72             Moose::Meta::TypeConstraint
73             ClassName RoleName Str ));
74              
75             # yo dawg i herd u liek types so we put a type in yo type so u can
76             # type whiel u type
77             coerce Type, from Str, via {
78             my $x = shift;
79             return Moose::Util::TypeConstraints::find_or_parse_type_constraint($x)
80             || class_type($x);
81             };
82             # ...that meme will never get old.
83              
84             =head2 Dependency
85              
86             A dependency is just a set of keys that both maintains its order and
87             can be conveniently queried for membership. It is implemented via
88             L<Tie::IxHash>.
89              
90             =cut
91              
92             subtype Dependency, as HashRef[Bool],
93             where { my $tied = tied %$_; $tied && $tied->isa('Tie::IxHash') };
94              
95             sub ixhash_ref {
96 0     0 0   tie my %ix, 'Tie::IxHash', @_;
97 0           \%ix;
98             }
99              
100             coerce Dependency, from Str, via { ixhash_ref($_[0] => 1) };
101              
102             coerce Dependency, from ArrayRef, via { ixhash_ref(map { $_ => 1 } @{$_[0]}) };
103              
104             # actually we don't want this to be exposed because we're usin g
105             # meaning
106              
107             # coerce Dependency, from HashRef,
108             # via { tie my %ix, 'Tie::IxHash', %{$_[0]}; \%ix };
109              
110              
111             =head2 Template
112              
113             This might not be used currently, i don't remember
114              
115             =cut
116              
117             class_type Template, { class => 'Params::Registry::Template' };
118             #coerce Template, from HashRef, via { Params::Registry::Template->new(shift) };
119              
120             #subtype TemplateSet, as HashRef[HashRef];
121             #coerce TemplateSet,
122              
123             =head2 Format
124              
125             =cut
126              
127             subtype Format, as CodeRef;
128             coerce Format, from Str, via { my $x = shift; sub { sprintf $x, shift } };
129              
130             =head2 PositiveInt
131              
132             =cut
133              
134             subtype PositiveInt, as Int, where { $_ > 0 };
135              
136             =head2 NegativeInt
137              
138             =cut
139              
140             subtype NegativeInt, as Int, where { $_ < 0 };
141              
142             =head2 NonPostiveInt
143              
144             =cut
145              
146             subtype NonPositiveInt, as Int, where { $_ <= 0 };
147              
148             =head2 NonNegativeInt
149              
150             =cut
151              
152             subtype NonNegativeInt, as Int, where { $_ >= 0 };
153              
154             =head2 XSDdate
155              
156             =cut
157              
158             class_type XSDdate, { class => 'DateTime' };
159              
160             sub _make_date {
161 0 0   0     if (@_ == 2) {
162 0           DateTime->last_day_of_month(year => $_[0], month => $_[1]);
163             }
164             else {
165 0           DateTime->new(year => $_[0], month => $_[1], day => $_[2]);
166             }
167             }
168              
169             coerce XSDdate, from Str, via { _make_date(split /-+/, $_[0]) };
170              
171             =head2 XSDgYearMonth
172              
173             =cut
174              
175             subtype XSDgYearMonth, as XSDdate;
176              
177             coerce XSDgYearMonth, from Str, via { _make_date(split(/-/, $_[0], 2)) };
178              
179             =head2 XSDgYear
180              
181             =cut
182              
183             subtype XSDgYear, as Int;
184              
185             =head2 XSDgMonth
186              
187             =cut
188              
189             subtype XSDgMonth, as Int, where { $_[0] > 0 && $_[0] < 13 };
190              
191             =head2 XSDgDay
192              
193             =cut
194              
195             subtype XSDgDay, as Int, where { $_[0] > 0 && $_[0] < 32 };
196              
197             =head2 XSDBool
198              
199             =cut
200              
201             subtype XSDBool, as Bool;
202             coerce XSDBool, from Undef, via { 0 };
203             coerce XSDBool, from Str, via { return ($_[0] =~ /(1|true|on|yes)/i) ? 1 : 0 };
204              
205             =head2 Currency
206              
207             =cut
208              
209             subtype Currency, as Num;
210             coerce Currency, from Num, via { my $x = int($_[0] * 100); return $x/100 };
211              
212             =head2 Decimal3
213              
214             =cut
215              
216             subtype Decimal3, as Num;
217             coerce Decimal3, from Num, via { my $x = int($_[0] * 1000); return $x/1000 };
218              
219             =head2 UCToken
220              
221             =cut
222              
223             subtype UCToken, as Str;
224             coerce UCToken, from Str, via { uc shift };
225              
226             =head2 LCToken
227              
228             =cut
229              
230             subtype LCToken, as Str;
231             coerce LCToken, from Str, via { lc shift };
232              
233             =head2 Set
234              
235             =cut
236              
237             class_type Set, { class => 'Set::Scalar' };
238              
239             =head2 IntSet
240              
241             =cut
242              
243             subtype IntSet, as Set;
244             coerce IntSet, from ArrayRef[Str],
245             via { Set::Scalar->new(map { int $_ } @{$_[0]}) };
246              
247             =head2 TokenSet
248              
249             =cut
250              
251             subtype TokenSet, as Set;
252             coerce TokenSet, from ArrayRef[Str], via { Set::Scalar->new(@{$_[0]}) };
253              
254             =head2 NumberRange
255              
256             =cut
257              
258             class_type NumberRange, { class => 'Set::Infinite' };
259              
260             sub _coerce_number_range {
261 0 0   0     my ($s, $e) = @{ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]]};
  0            
262             #warn "hi i'm here";
263             #warn defined $e;
264             #require Data::Dumper;
265             #warn Data::Dumper::Dumper
266 0           my ($ds, $de) = (defined $s, defined $e);
267 0 0 0       if (!$ds and !$de) {
    0          
    0          
268 0           ($s, $e) = (NEG_INF, INF);
269             }
270             elsif (!$ds) {
271 0           $s = NEG_INF;
272             }
273             elsif (!$de) {
274 0           $e = INF;
275             }
276             else {
277 0           ($s, $e) = sort { $a <=> $b } map { $_ + 0 } ($s, $e);
  0            
  0            
278             }
279              
280 0           Set::Infinite->new($s, $e);
281             }
282              
283             coerce NumberRange, from Num, via \&_coerce_number_range;
284             coerce NumberRange, from ArrayRef[Maybe[Num]], via \&_coerce_number_range;
285              
286             =head2 DateRange
287              
288             =cut
289              
290             class_type DateSpan, { class => 'DateTime::Span' };
291             class_type DateSpanSet, { class => 'DateTime::SpanSet' };
292              
293             #union DateRange, [DateSpan, DateSpanSet];
294             subtype DateRange, as DateSpan|DateSpanSet;
295              
296             sub _make_date_span {
297 0     0     my ($d1, $d2) = @_;
298 0 0         $d1 = defined $d1 ? ref $d1 ? $d1 : _make_date(split /-/, $d1) :
    0          
299             DateTime::Infinite::Past->new;
300 0 0         $d2 = defined $d2 ? ref $d2 ? $d2 : _make_date(split /-/, $d2) :
    0          
301             DateTime::Infinite::Future->new;
302              
303 0           my %p;
304 0           @p{qw(start end)} = sort { $a <=> $b } ($d1, $d2);
  0            
305              
306 0           DateTime::Span->from_datetimes(%p);
307             }
308              
309             coerce DateRange, from ArrayRef[Maybe[Str]],
310             via { _make_date_span(@{$_[0]}) };
311              
312             coerce DateRange, from ArrayRef[Maybe[XSDdate]],
313             via { _make_date_span(@{$_[0]}) };
314              
315             =head1 AUTHOR
316              
317             Dorian Taylor, C<< <dorian at cpan.org> >>
318              
319             =head1 LICENSE AND COPYRIGHT
320              
321             Copyright 2013 Dorian Taylor.
322              
323             Licensed under the Apache License, Version 2.0 (the "License"); you
324             may not use this file except in compliance with the License. You may
325             obtain a copy of the License at
326             L<http://www.apache.org/licenses/LICENSE-2.0> .
327              
328             Unless required by applicable law or agreed to in writing, software
329             distributed under the License is distributed on an "AS IS" BASIS,
330             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
331             implied. See the License for the specific language governing
332             permissions and limitations under the License.
333              
334             =cut
335              
336             1; # End of Params::Registry::Types