Method/Generate/Accessor/Role/LvalueAttribute.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 77.2% 17 / 22 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 82.8% 87 / 105 CC 18 SCAR 24.2
CC
18
Cov
87%
__ANON__
17.2
__ANON__
3.0
BEGIN
1.0
CRAP 11.2
↑ Summary MooX/LvalueAttribute.pm »
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 Method::Generate::Accessor::Role::LvalueAttribute;
10 {
11 4 $Method::Generate::Accessor::Role::LvalueAttribute::VERSION = '0.16';
12 }
13 4 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: Provides Lvalue accessors to Moo class attributes
16  
17 4 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Variable::Magic qw(wizard cast);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 ▶use Hash::Util::FieldHash::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 4 Hash::Util::FieldHash::Compat::fieldhash my %LVALUES;
23  
24 4 require MooX::LvalueAttribute;
25  
26 8 ▶around generate_method => sub {
Subroutine __ANON__: called CC=16 SCAR=28.5
27 8 my $orig = shift;
28 8 my $self = shift;
29 # would like a better way to disable XS
30  
31 8 my ($into, $name, $spec, $quote_opts) = @_;
32  
33 $MooX::LvalueAttribute::INJECTED_IN_ROLE{$into}
34 8 ▶ || $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
Branch: 1 branch
BranchTrueFalseTotal
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} or $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into} or return $self->$orig(@_) 8 0 8
Condition66%2/3
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} or $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
l!l&&r!l&&!r
440
MC/DC0%0/2
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} or $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
$MooX::LvalueAttribute::INJECTED_IN_ROLE{$into} $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
Truth table66%2/3
A or B
A= $MooX::LvalueAttribute::INJECTED_IN_ROLE{$into}B= $MooX::LvalueAttribute::INJECTED_IN_CLASS{$into}
ABresult
1X1
011
000
35 or return $self->$orig(@_);
36  
37 8 ▶ if ($spec->{lvalue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"lvalue"}) 7 1 8
38  
39 7 my $is = $spec->{is};
40 7 ▶ if ($is eq 'rw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($is eq 'rw') { } 6 1 7
elsif ($is eq 'rwp') { } 0 1 1
41 $spec->{accessor} = $name unless exists $spec->{accessor}
42 6 ▶ or ( $spec->{reader} and $spec->{writer} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"accessor"} or $spec->{"reader"} and $spec->{"writer"} 6 0 6
Condition33%2/6
exists $spec->{"accessor"} or $spec->{"reader"} and $spec->{"writer"}
l!l&&r!l&&!r
006
$spec->{"reader"} and $spec->{"writer"}
!ll&&!rl&&r
600
MC/DC0%0/3
exists $spec->{"accessor"} or $spec->{"reader"} and $spec->{"writer"}
exists $spec->{"accessor"} $spec->{"reader"} $spec->{"writer"}
Truth table25%1/4
A or B and C
A= exists $spec->{"accessor"}B= $spec->{"reader"}C= $spec->{"writer"}
ABCresult
1XX1
0111
00X0
0100
43 } elsif ($is eq 'rwp') {
44 0 ▶ $spec->{writer} = "_set_${name}" unless exists $spec->{writer};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec->{"writer"} 0 0 0
45 }
46  
47 exists $spec->{writer} || exists $spec->{accessor}
48 7 ▶ or die "lvalue was set but no accessor nor reader, and attribute is not rw";
Branch: 1 branch
BranchTrueFalseTotal
exists $spec->{"writer"} or exists $spec->{"accessor"} or die "lvalue was set but no accessor nor reader, and attribute is not rw" 6 1 7
Condition66%2/3
exists $spec->{"writer"} or exists $spec->{"accessor"}
l!l&&r!l&&!r
061
MC/DC50%1/2
exists $spec->{"writer"} or exists $spec->{"accessor"}
exists $spec->{"writer"} exists $spec->{"accessor"}
Truth table66%2/3
A or B
A= exists $spec->{"writer"}B= exists $spec->{"accessor"}
ABresult
1X1
011
000
49 6 foreach( qw(writer accessor) ) {
50 12 ▶ my $t = $spec->{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $t = $spec->{$_} or next 6 6 12
51 or next;
52 6 $spec->{'lv_' . $_} = $t;
53 6 $spec->{$_} = '_lv_' . $t;
54 }
55 }
56  
57 7 my $methods = $self->$orig(@_);
58  
59 7 foreach ( qw(writer accessor) ) {
60 14 ▶ my $lv_name = $spec->{'lv_' . $_}
Branch: 1 branch
BranchTrueFalseTotal
my $lv_name = $$spec{'lv_' . $_} or next 6 8 14
61 or next;
62 6 my $name = $spec->{$_};
63 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 27 ▶ my $sub = sub : lvalue {
Subroutine __ANON__: called CC=3 SCAR=11.0
65 27 my $self = shift;
66 27 ▶ if (! exists $LVALUES{$self}{$lv_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $LVALUES{$self}{$lv_name}) 10 17 27
67 my $wiz = wizard(
68 7 ▶ set => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 7 $self->$name(${$_[0]});
70 7 return 1;
71 },
72 17 ▶ get => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 17 ${$_[0]} = $self->$name();
74 17 return 1;
75 },
76 10 );
77 10 cast $LVALUES{$self}{$lv_name}, $wiz;
78 }
79 27 ▶ @_ and $self->$name(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->$name(@_) 3 24 27
80 27 $LVALUES{$self}{$lv_name};
81 6 };
82 6 $methods->{$lv_name} = $sub;
83 6 *{"${into}::${lv_name}"} = $sub;
84 }
85 4 };
86  
87 4 1;
88  
89 __END__
↑ Summary MooX/LvalueAttribute.pm »