| 1 | package PRANG::Coerce; | ||||||||||||
| 2 | 1 | $PRANG::Coerce::VERSION = '0.21'; | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | # This is what this method is doing: | ||||||||||||
| 7 | # | ||||||||||||
| 8 | #coerce 'ArrayRef[`K]' | ||||||||||||
| 9 | # => from 'K' | ||||||||||||
| 10 | # => via { [ $_ ] }; | ||||||||||||
| 11 | # | ||||||||||||
| 12 | 2 | ▶ | sub coerce_arrayref_of { | ||||||||||
|
Subroutine
coerce_arrayref_of: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||
| 13 | 2 | my $what = shift; | |||||||||||
| 14 | |||||||||||||
| 15 | 2 | my $typeName = lc $what; | |||||||||||
| 16 | 2 | ▶ | $typeName =~ s{^(\w)}{uc($1||"")}eg; | ||||||||||
|
Condition50%1/2
$1 || ''
MC/DC0%0/1
$1 || ''
$1
Truth table50%1/2
A
A= $1
| |||||||||||||
| 17 | 0 | ▶ | $typeName =~ s{::(\w?)}{uc($1||"")}eg; | ||||||||||
|
Condition0%0/2
$1 || ''
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
| |||||||||||||
| 18 | |||||||||||||
| 19 | 2 | my $subtype = __PACKAGE__ . '::ArrayRefOf' . $typeName . 's'; | |||||||||||
| 20 | 2 | my $as = 'ArrayRef[' . $typeName . ']'; | |||||||||||
| 21 | |||||||||||||
| 22 | 2 | subtype $subtype | |||||||||||
| 23 | => as $as; | ||||||||||||
| 24 | coerce $subtype | ||||||||||||
| 25 | => from $what | ||||||||||||
| 26 | 0 | ▶ | => via { [$_] }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | # Make these coercions from standard types | ||||||||||||
| 30 | 1 | coerce_arrayref_of('Str'); | |||||||||||
| 31 | 1 | coerce_arrayref_of('Int'); | |||||||||||
| 32 | |||||||||||||
| 33 | 1 | 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 | ||||||||||||