Class/Accessor.pm

filter: Statementstmt 60.6% 20 / 33 Branchbran 23.0% 6 / 26 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 38.7% 36 / 93 CC 19 SCAR 38.5
CC
19
Cov
39%
__ANON__
110.0
__ANON__
7.3
method_list
6.0
CRAP 47.2
↑ Summary BaseClass.pm »
1 package EntityModel::Class::Accessor;
2 5 $EntityModel::Class::Accessor::VERSION = '0.016';
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 EntityModel::Class::Accessor - generic class accessor
9  
10 =head1 VERSION
11  
12 Version 0.016
13  
14 =head1 DESCRIPTION
15  
16 See L<EntityModel::Class>.
17  
18 =cut
19  
20 =head2 add_to_class
21  
22 Returns (method name, coderef) pairs for new methods to add.
23  
24 =cut
25  
26 7 ▶sub add_to_class {
Subroutine add_to_class: called CC=3 SCAR=15.4
Pod documented
27 7 my ($class, $pkg, $k, $v) = @_;
28  
29 return $k => $class->method_list(
30 pkg => $pkg,
31 k => $k,
32 pre => $v->{pre},
33 post => $v->{post},
34 allowed => $v->{valid},
35 validate => defined $v->{valid}
36 ? ref $v->{valid} eq 'CODE'
37 0 ▶ ? $v->{valid} : sub { $_[0] eq $v->{valid} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 : undef
39 7 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
defined $v->{'valid'} ? : 0 7 7
ref $v->{'valid'} eq 'CODE' ? : 0 0 0
40 }
41  
42 =head2 method_list
43  
44 Returns the coderef for the method that should be applied to the requesting class.
45  
46 =cut
47  
48 4 ▶sub method_list {
Subroutine method_list: called CC=3 SCAR=18.0
Pod documented
49 4 my ($self, %opt) = @_;
50 4 my $k = delete $opt{k};
51 4 ▶ if($opt{pre} || $opt{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'pre'} or $opt{'post'}) { } 0 4 4
Condition33%1/3
$opt{"pre"} or $opt{"post"}
l!l&&r!l&&!r
004
MC/DC0%0/2
$opt{"pre"} or $opt{"post"}
$opt{"pre"} $opt{"post"}
Truth table33%1/3
A or B
A= $opt{"pre"}B= $opt{"post"}
ABresult
1X1
011
000
52 0 ▶ return sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
53 0 my $self = shift;
54 0 ▶ if($opt{pre}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"pre"}) 0 0 0
55 0 ▶ $opt{pre}->($self, @_)
Branch: 1 branch
BranchTrueFalseTotal
$opt{"pre"}->($self, @_) or return 0 0 0
56 or return;
57 }
58 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
59 0 ▶ die $_[0] . ' is invalid' if $opt{validate} && !$opt{validate}->($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"validate"} and not $opt{"validate"}->($_[0]) 0 0 0
Condition0%0/3
$opt{"validate"} and not $opt{"validate"}->($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$opt{"validate"} and not $opt{"validate"}->($_[0])
$opt{"validate"} not $opt{"validate"}->($_[0])
Truth table0%0/3
A and not(B)
A= $opt{"validate"}B= not $opt{"validate"}->($_[0])
ABresult
0X0
100
111
60 0 my $v = $_[0];
61 # Readonly values can be problematic, make a copy if we can - but don't trash refs.
62 0 ▶ $v = "$v" if Scalar::Util::readonly($v) && !ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::readonly($v) and not ref $v 0 0 0
Condition0%0/3
&Scalar::Util::readonly($v) and not ref $v
!ll&&!rl&&r
000
MC/DC0%0/2
&Scalar::Util::readonly($v) and not ref $v
&Scalar::Util::readonly($v) not ref $v
Truth table0%0/3
A and not(B)
A= &Scalar::Util::readonly($v)B= not ref $v
ABresult
0X0
100
111
63 0 $self->{$k} = $v;
64 }
65 0 ▶ $opt{post}->($self, @_) if $opt{post};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"post"} 0 0 0
66 0 ▶ return $self if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
67 0 $self->{$k};
68 0 };
69 } else {
70 4 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
71 4 ▶ return $_[0]->{$k} unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 2 2 4
72 2 ▶ die $_[1] . ' is invalid' if $opt{validate} && !$opt{validate}->(@_);
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"validate"} and not $opt{"validate"}->(@_) 0 2 2
Condition33%1/3
$opt{"validate"} and not $opt{"validate"}->(@_)
!ll&&!rl&&r
200
MC/DC0%0/2
$opt{"validate"} and not $opt{"validate"}->(@_)
$opt{"validate"} not $opt{"validate"}->(@_)
Truth table33%1/3
A and not(B)
A= $opt{"validate"}B= not $opt{"validate"}->(@_)
ABresult
0X0
100
111
73 2 my $v = $_[1];
74 # Readonly values can be problematic, make a copy if we can - but don't trash refs.
75 2 ▶ $v = "$v" if Scalar::Util::readonly($v) && !ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::readonly($v) and not ref $v 0 2 2
Condition33%1/3
&Scalar::Util::readonly($v) and not ref $v
!ll&&!rl&&r
200
MC/DC0%0/2
&Scalar::Util::readonly($v) and not ref $v
&Scalar::Util::readonly($v) not ref $v
Truth table33%1/3
A and not(B)
A= &Scalar::Util::readonly($v)B= not ref $v
ABresult
0X0
100
111
76 2 $_[0]->{$k} = $v;
77 2 return $_[0];
78 4 };
79 }
80 }
81  
82 5 1;
83  
84 __END__
↑ Summary BaseClass.pm »