| 1 | package MooseX::CoercePerAttribute; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use 5.008_005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '1.001'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | Moose::Util::meta_attribute_alias('CoercePerAttribute'); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 16 | ▶ | before _process_coerce_option => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.6
| |||||||||||||||||||||||||||||
| 13 | 16 | my ($class, $name, $options) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 16 | my $coercion = $options->{coerce}; | |||||||||||||||||||||||||||
| 16 | 16 | ▶ | return unless $coercion && $coercion != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$coercion and $coercion != 1
MC/DC0%0/2
$coercion and $coercion != 1
$coercion
$coercion != 1
Truth table33%1/3
A and B
A= $coercionB= $coercion != 1
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Create an anonymous subtype of the TC object so as to not mess with the existing TC | ||||||||||||||||||||||||||||
| 19 | my $anon_subtype = $options->{type_constraint} = Moose::Meta::TypeConstraint->new( | ||||||||||||||||||||||||||||
| 20 | parent => $options->{type_constraint}, | ||||||||||||||||||||||||||||
| 21 | 16 | ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 16 | ▶ | $class->throw_error( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref($coercion) =~ /ARRAY|HASH/ and $anon_subtype
MC/DC0%0/2
ref($coercion) =~ /ARRAY|HASH/ and $anon_subtype
ref($coercion) =~ /ARRAY|HASH/
$anon_subtype
Truth table33%1/3
A and B
A= ref($coercion) =~ /ARRAY|HASH/B= $anon_subtype
| |||||||||||||||||||||||||||||
| 24 | "Couldn't build coercion from supplyed arguments for ($name)", | ||||||||||||||||||||||||||||
| 25 | data => $coercion, | ||||||||||||||||||||||||||||
| 26 | ) unless ((ref $coercion) =~ /ARRAY|HASH/) && $anon_subtype; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | my @coercions; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # NOTE: Depricated behavior. Just set it to the approved usage. | ||||||||||||||||||||||||||||
| 31 | # TODO: Remove in version 1.000 | ||||||||||||||||||||||||||||
| 32 | 16 | ▶ | if (ref $coercion eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 5 | warn "The use of a HashRef for declaration of inline coercions is depricated. See perldoc MooseX::CoercePerAttribute. This feature will be removed in version 1.100 of MooseX::CoercePerAttribute"; | |||||||||||||||||||||||||||
| 34 | $coercion = [ | ||||||||||||||||||||||||||||
| 35 | 5 | map { $_ => $coercion->{$_} } keys %$coercion, | |||||||||||||||||||||||||||
| 36 | ]; | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 16 | ▶ | if (ref $coercion eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 16 | ▶ | while (scalar @$coercion) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 20 | my $coerce_type = shift @$coercion; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # The user can supply the coercion in its full form... | ||||||||||||||||||||||||||||
| 44 | 20 | ▶ | if (ref $coerce_type eq 'CODE'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 6 | push @coercions, $coerce_type; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Or they can give us the pieces to make the coercion from. | ||||||||||||||||||||||||||||
| 49 | 20 | ▶ | if (!ref $coerce_type){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 14 | my $via = shift @$coercion; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Create the coercion sub ref from the list of Type => via pairs. | ||||||||||||||||||||||||||||
| 53 | 14 | ▶ | push @coercions, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 14 | &coerce(shift, &from($coerce_type, &via($via)) ) | |||||||||||||||||||||||||||
| 55 | 14 | }; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Create each coercion object from a anonymous subtype | ||||||||||||||||||||||||||||
| 61 | 16 | for my $coercion (@coercions){ | |||||||||||||||||||||||||||
| 62 | 20 | ▶ | $coercion->($anon_subtype) if ref $coercion eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | $class->throw_error( | ||||||||||||||||||||||||||||
| 66 | 16 | ▶ | "Coerce for ($name) doesn't set a coercion for ($anon_subtype), see man MooseX::CoercePerAttribute for usage", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | data => $coercion | ||||||||||||||||||||||||||||
| 68 | ) unless $anon_subtype->has_coercion; | ||||||||||||||||||||||||||||
| 69 | 7 | }; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 7 | 1; | |||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||