File Coverage

blib/lib/PRANG/Coerce.pm
Criterion Covered Total %
statement 15 17 88.2
branch n/a
condition 1 4 25.0
subroutine 3 4 75.0
pod 0 1 0.0
total 19 26 73.0


line stmt bran cond sub pod time code
1             package PRANG::Coerce;
2             $PRANG::Coerce::VERSION = '0.19';
3 1     1   2054 use Moose;
  1         387438  
  1         8  
4 1     1   6511 use Moose::Util::TypeConstraints;
  1         2  
  1         10  
5              
6             # This is what this method is doing:
7             #
8             #coerce 'ArrayRef[`K]'
9             # => from 'K'
10             # => via { [ $_ ] };
11             #
12             sub coerce_arrayref_of {
13 2     2 0 4 my $what = shift;
14              
15 2         5 my $typeName = lc $what;
16 2   50     11 $typeName =~ s{^(\w)}{uc($1||"")}eg;
  2         13  
17 2   0     6 $typeName =~ s{::(\w?)}{uc($1||"")}eg;
  0         0  
18              
19 2         5 my $subtype = __PACKAGE__ . '::ArrayRefOf' . $typeName . 's';
20 2         4 my $as = 'ArrayRef[' . $typeName . ']';
21              
22 2         7 subtype $subtype
23             => as $as;
24             coerce $subtype
25             => from $what
26 2     0   12571 => via { [$_] };
  0            
27             }
28              
29             # Make these coercions from standard types
30             coerce_arrayref_of('Str');
31             coerce_arrayref_of('Int');
32              
33             1;
34              
35             =head1 NAME
36              
37             PRANG::Coerce - Easily create subtypes and coercions for any type
38              
39             =head1 SYNOPSIS
40              
41             use PRANG::Coerce;
42              
43             has_element 'an_array_of_strs' =>
44             is => 'rw',
45             isa => 'PRANG::Coerce::ArrayRefOfStrs',
46             coerce => 1,
47             ;
48              
49             # or
50              
51             use PRANG::Coerce;
52              
53             PRANG::Coerce::coerce_arrayref_of('Type');
54              
55             has_element 'an_array_of_types' =>
56             is => 'rw',
57             isa => 'PRANG::Coerce::ArrayRefOfTypes',
58             coerce => 1,
59             ;
60              
61             =head1 DESCRIPTION
62              
63             When defining a type which is an C<ArrayRef[Type]>, sometimes it's nice to be
64             able to just pass in a C<Type>. By using this module, that C<Type> can be
65             coerced into the 'ArrayRef[Type]' by using C<ArrayRefOfTypes>.
66              
67             =head1 PRE-DEFINED TYPES
68              
69             PRANG::Coerce already defines two array types. These are for C<Str> and C<Int>
70             and are defined as C<ArrayOfStrs> and C<ArrayOfInts> respectively.
71              
72             =head1 AUTHOR AND LICENCE
73              
74             Development commissioned by NZ Registry Services, and carried out by
75             Catalyst IT - L<http://www.catalyst.net.nz/>
76              
77             Copyright 2009, 2010, NZ Registry Services. This module is licensed
78             under the Artistic License v2.0, which permits relicensing under other
79             Free Software licenses.
80              
81             =cut