| 1 | package MooseX::LazyCoercion; | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: Coercion deferred to attribute call time | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 1 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||||||
| 12 | with_meta => ['has_lazily_coerced'], | ||||||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub has_lazily_coerced { | ||||||||||||||||||||||||||||||
|
Subroutine
has_lazily_coerced: called CC=9 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 18 | 2 | my $meta = shift; | |||||||||||||||||||||||||||||||
| 19 | 2 | my $name = shift; | |||||||||||||||||||||||||||||||
| 20 | 2 | my $attrs = {@_}; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 2 | my $isa = $attrs->{isa}; | |||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | croak "has_lazily_coerced requires isa" unless ($isa); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 2 | my $attr_args = {}; | |||||||||||||||||||||||||||||||
| 27 | 2 | my $wrapper_name = "__$name"; | |||||||||||||||||||||||||||||||
| 28 | 2 | my $attr_builder_name = "_build_$name"; | |||||||||||||||||||||||||||||||
| 29 | 2 | my $attr_wrapper_args = {}; | |||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # stores the types this isa can be coerced from | ||||||||||||||||||||||||||||||||
| 32 | 2 | my @type_coercions; | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # check for MooseX::Types | ||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if (blessed($isa) && $isa->blessed() eq 'MooseX::Types::TypeDecorator') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($isa) and $isa->blessed eq "MooseX::Types::TypeDecorator"
MC/DC0%0/2
builtin::blessed($isa) and $isa->blessed eq "MooseX::Types::TypeDecorator"
builtin::blessed($isa)
$isa->blessed eq "MooseX::Types::TypeDecorator"
Truth table66%2/3
A and B
A= builtin::blessed($isa)B= $isa->blessed eq "MooseX::Types::TypeDecorator"
| |||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||
| 38 | 0 | for my $type (@{ $isa->__type_constraint->coercion->type_coercion_map }) { | |||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | next if $i++ %2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | push @type_coercions, $type; | |||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # Check for string types | ||||||||||||||||||||||||||||||||
| 45 | elsif (! ref $isa ) { | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||
| 48 | 1 | my $type = find_type_constraint($isa); | |||||||||||||||||||||||||||||||
| 49 | 1 | for my $type (@{ $type->coercion->type_coercion_map }) { | |||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | next if $i++ %2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 1 | push @type_coercions, find_type_constraint($type); | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||||||
| 56 | # dunno when this might actually happen. | ||||||||||||||||||||||||||||||||
| 57 | # I suppose someone might use a Moose::Meta::TypeConstraint | ||||||||||||||||||||||||||||||||
| 58 | # for a isa attribute... | ||||||||||||||||||||||||||||||||
| 59 | 1 | croak "Unknown type constraint " . Dumper($isa); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # build_method for the wrapped attribute | ||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $meta->add_method( $attr_builder_name => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Subroutine
_build_x: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 64 | 0 | $_[0]->$wrapper_name | |||||||||||||||||||||||||||||||
| 65 | 1 | }); | |||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | # add the attribute to be used | ||||||||||||||||||||||||||||||||
| 68 | 1 | $meta->add_attribute( $name => { | |||||||||||||||||||||||||||||||
| 69 | %$attrs, | ||||||||||||||||||||||||||||||||
| 70 | lazy_build => 1, | ||||||||||||||||||||||||||||||||
| 71 | coerce => 1, | ||||||||||||||||||||||||||||||||
| 72 | init_arg => undef, | ||||||||||||||||||||||||||||||||
| 73 | builder => $attr_builder_name, | ||||||||||||||||||||||||||||||||
| 74 | }); | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # add the wrapped attribute that handles the coercion | ||||||||||||||||||||||||||||||||
| 77 | 1 | $meta->add_attribute( $wrapper_name => { | |||||||||||||||||||||||||||||||
| 78 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 79 | isa => join("|", @type_coercions), | ||||||||||||||||||||||||||||||||
| 80 | init_arg => $name, | ||||||||||||||||||||||||||||||||
| 81 | required => 1, | ||||||||||||||||||||||||||||||||
| 82 | }); | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | # die Dumper( @type_coercions ); | ||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 1 | 1; | |||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||||||