| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # This file is part of MooX-LvalueAttribute | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2013 by Damien "dams" Krotkine. | ||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||
| 9 | package MooX::LvalueAttribute; | ||||||||||||||||||||||||||||||||
| 10 | { | ||||||||||||||||||||||||||||||||
| 11 | 5 | $MooX::LvalueAttribute::VERSION = '0.16'; | |||||||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use strictures 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | # ABSTRACT: Provides Lvalue accessors to Moo class attributes | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 5 | require Moo; | |||||||||||||||||||||||||||||||
| 18 | 5 | require Moo::Role; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 5 | our %INJECTED_IN_ROLE; | |||||||||||||||||||||||||||||||
| 21 | 5 | our %INJECTED_IN_CLASS; | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=21.1
| |||||||||||||||||||||||||||||||||
| 24 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||
| 25 | 4 | my $target = caller; | |||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | if ($Moo::Role::INFO{$target} && $Moo::Role::INFO{$target}{is_role}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$Moo::Role::INFO{$target} and $Moo::Role::INFO{$target}{"is_role"}
MC/DC50%1/2
$Moo::Role::INFO{$target} and $Moo::Role::INFO{$target}{"is_role"}
$Moo::Role::INFO{$target}
$Moo::Role::INFO{$target}{"is_role"}
Truth table66%2/3
A and B
A= $Moo::Role::INFO{$target}B= $Moo::Role::INFO{$target}{"is_role"}
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # We are loaded from a Moo role | ||||||||||||||||||||||||||||||||
| 30 | 2 | ▶ | $Moo::Role::INFO{$target}{accessor_maker} ||= do { | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$Moo::Role::INFO{$target}{"accessor_maker"} ||= do {
require Method::Generate::Accessor;
"Method::Generate::Accessor"->new
}
MC/DC0%0/2
$Moo::Role::INFO{$target}{"accessor_maker"} ||= do {
require Method::Generate::Accessor;
"Method::Generate::Accessor"->new
}
$Moo::Role::INFO{$target}{"accessor_maker"}
do {
require Method::Generate::Accessor;
"Method::Generate::Accessor"->new
}
Truth table33%1/3
A ||= B
A= $Moo::Role::INFO{$target}{"accessor_maker"}B= do {
require Method::Generate::Accessor;
"Method::Generate::Accessor"->new
}
| |||||||||||||||||||||||||||||||||
| 31 | 2 | require Method::Generate::Accessor; | |||||||||||||||||||||||||||||||
| 32 | 2 | Method::Generate::Accessor->new | |||||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||||||
| 34 | Moo::Role->apply_roles_to_object( | ||||||||||||||||||||||||||||||||
| 35 | $Moo::Role::INFO{$target}{accessor_maker}, | ||||||||||||||||||||||||||||||||
| 36 | 2 | 'Method::Generate::Accessor::Role::LvalueAttribute', | |||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||
| 38 | 2 | $INJECTED_IN_ROLE{$target} = 1; | |||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | ▶ | } elsif ($Moo::MAKERS{$target} && $Moo::MAKERS{$target}{is_class}) { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
$Moo::MAKERS{$target} and $Moo::MAKERS{$target}{"is_class"}
MC/DC0%0/2
$Moo::MAKERS{$target} and $Moo::MAKERS{$target}{"is_class"}
$Moo::MAKERS{$target}
$Moo::MAKERS{$target}{"is_class"}
Truth table33%1/3
A and B
A= $Moo::MAKERS{$target}B= $Moo::MAKERS{$target}{"is_class"}
| |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # We are loaded from a Moo class | ||||||||||||||||||||||||||||||||
| 43 | 2 | ▶ | if ( !$INJECTED_IN_CLASS{$target} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 2 | Moo::Role->apply_roles_to_object( | |||||||||||||||||||||||||||||||
| 45 | Moo->_accessor_maker_for($target), | ||||||||||||||||||||||||||||||||
| 46 | 'Method::Generate::Accessor::Role::LvalueAttribute', | ||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||
| 48 | 2 | $INJECTED_IN_CLASS{$target} = 1; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||||||
| 51 | 0 | die "MooX::LvalueAttribute can only be used in Moo classes or Moo roles."; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 5 | 1; | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | __END__ | ||||||||||||||||||||||||||||||||