Property.pm

filter: Statementstmt 94.4% 136 / 144 Branchbran 86.6% 26 / 30 Conditioncond 80.0% 12 / 15 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 87.8% 29 / 33 Podpod 0.0% 0 / 4 totaltotal 88.9% 209 / 235 CC 23 SCAR 20.1
CC
23
Cov
92%
__ANON__
19.1
__ANON__
2.1
__ANON__
2.0
CRAP 7.5
↑ Summary Property/RW/Lazy/CustomSet.pm »
1 package Class::Property;
2 1 ▶use strict; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 our $VERSION = '1.002'; # change in POD
8
9 1 our @EXPORT;
10
11 1 my $LAZY_INITS = {};
12
13 my $GEN = {
14 'default' => sub
15 3 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 3 my( $prop_name ) = @_;
17
18 12 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
19 {
20 12 return shift->{$prop_name};
21 3 };
22 },
23 'default_lazy' => sub
24 3 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
25 3 my( $prop_name, $lazy_init ) = @_;
26 3 require Class::Property::RW::Lazy;
27 3 my $dummy;
28 3 ▶ my $inits = $LAZY_INITS->{$prop_name} //= {};
Condition50%1/2
$LAZY_INITS->{$prop_name} //= {}
l!l
03
MC/DC0%0/1
$LAZY_INITS->{$prop_name} //= {}
$LAZY_INITS->{$prop_name}
Truth table50%1/2
A
A= $LAZY_INITS->{$prop_name}
Aresult
11
00
29 3 my $wrapper = tie $dummy, 'Class::Property::RW::Lazy', $prop_name, $lazy_init, $inits;
30
31 16 ▶ return sub: lvalue
Subroutine __ANON__: called CC=2 SCAR=6.9
32 {
33 16 my $self = shift;
34
35 16 ▶ if( defined $inits->{$self} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inits->{$self}) { } 10 6 16
36 {
37 10 return $self->{$prop_name};
38 }
39 else
40 {
41 6 $wrapper->set_object($self);
42 6 return $dummy;
43 }
44 3 };
45 },
46 'lazy_get_default_set' => sub
47 1 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.5
48 1 my( $prop_name, $lazy_init, $setter ) = @_;
49 1 require Class::Property::RW::Lazy::CustomSet;
50 1 my $dummy;
51 1 ▶ my $inits = $LAZY_INITS->{$prop_name} //= {};
Condition50%1/2
$LAZY_INITS->{$prop_name} //= {}
l!l
01
MC/DC0%0/1
$LAZY_INITS->{$prop_name} //= {}
$LAZY_INITS->{$prop_name}
Truth table50%1/2
A
A= $LAZY_INITS->{$prop_name}
Aresult
11
00
52 1 my $wrapper = tie $dummy, 'Class::Property::RW::Lazy::CustomSet', $prop_name, $lazy_init, $setter, $inits;
53
54 0 ▶ return sub: lvalue
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 {
56 0 $wrapper->set_object(shift);
57 0 return $dummy;
58 1 };
59 },
60 'custom' => sub
61 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 my( $getter, $setter ) = @_;
63 1 require Class::Property::RW::Custom;
64 1 my $dummy;
65 1 my $wrapper = tie $dummy, 'Class::Property::RW::Custom', $getter, $setter;
66
67 4 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
68 {
69 4 $wrapper->set_object(shift);
70 4 return $dummy;
71 1 };
72 },
73 'default_get_custom_set' => sub
74 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.2
75 1 my( $prop_name, $setter ) = @_;
76 1 require Class::Property::RW::CustomSet;
77 1 my $dummy;
78 1 my $wrapper = tie $dummy, 'Class::Property::RW::CustomSet', $prop_name, $setter;
79
80 0 ▶ return sub: lvalue
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 {
82 0 $wrapper->set_object(shift);
83 0 return $dummy;
84 1 };
85 },
86 'custom_get_default_set' => sub
87 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 1 my( $prop_name, $getter ) = @_;
89 1 require Class::Property::RW::CustomGet;
90 1 my $dummy;
91 1 my $wrapper = tie $dummy, 'Class::Property::RW::CustomGet', $prop_name, $getter;
92
93 6 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
94 {
95 6 $wrapper->set_object(shift);
96 6 return $dummy;
97 1 };
98 },
99 'default_ro' => sub
100 2 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 2 my( $prop_name ) = @_;
102 2 require Class::Property::RO;
103 2 my $dummy;
104 2 my $wrapper = tie $dummy, 'Class::Property::RO', $prop_name;
105
106 10 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
107 {
108 10 $wrapper->set_object(shift);
109 10 return $dummy;
110 2 };
111 },
112 'custom_ro' => sub
113 1 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.2
114 1 my( $prop_name, $getter ) = @_;
115 1 require Class::Property::RO::CustomGet;
116 1 my $dummy;
117 1 my $wrapper = tie $dummy, 'Class::Property::RO::CustomGet', $prop_name, $getter;
118
119 0 ▶ return sub: lvalue
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 {
121 0 $wrapper->set_object(shift);
122 0 return $dummy;
123 1 };
124 },
125 'lazy_ro' => sub
126 1 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
127 1 my( $prop_name, $lazy_init ) = @_;
128 1 require Class::Property::RO::Lazy;
129 1 my $dummy;
130 1 ▶ my $inits = $LAZY_INITS->{$prop_name} //= {};
Condition50%1/2
$LAZY_INITS->{$prop_name} //= {}
l!l
01
MC/DC0%0/1
$LAZY_INITS->{$prop_name} //= {}
$LAZY_INITS->{$prop_name}
Truth table50%1/2
A
A= $LAZY_INITS->{$prop_name}
Aresult
11
00
131 1 my $wrapper = tie $dummy, 'Class::Property::RO::Lazy', $prop_name, $lazy_init, $inits;
132
133 6 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
134 {
135 6 $wrapper->set_object(shift);
136 6 return $dummy;
137 1 };
138 },
139 2 ▶ 'default_wo' => sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
140 {
141 2 my( $prop_name ) = @_;
142 2 require Class::Property::WO;
143 2 my $dummy;
144 2 my $wrapper = tie $dummy, 'Class::Property::WO', $prop_name;
145
146 7 ▶ return sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.0
147 {
148 7 $wrapper->set_object(shift);
149 7 return $dummy;
150 2 };
151 },
152 1 ▶ 'custom_wo' => sub: lvalue
Subroutine __ANON__: called CC=1 SCAR=0.2
153 {
154 1 my( $prop_name, $setter ) = @_;
155 1 require Class::Property::WO::CustomSet;
156 1 my $dummy;
157 1 my $wrapper = tie $dummy, 'Class::Property::WO::CustomSet', $prop_name, $setter;
158
159 0 ▶ return sub: lvalue
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 {
161 0 $wrapper->set_object(shift);
162 0 return $dummy;
163 1 };
164 },
165 1 };
166
167 # creating new property by names
168 # input is a hash of
169 # property_name => hashref
170 # and hashref is:
171 #
172 # get => CODEREF | anything # creates getter custom or default
173 # get_lazy => CODEREF # creates default getter with lazy init method from CODEREF
174 # set => CODREF | anything # creates custom or default setter
175 #
176 my $make_property = sub
177 5 ▶{
Subroutine __ANON__: called CC=19 SCAR=29.5
178 5 my( $package, %kwargs ) = @_;
179
180 #use Data::Dumper; warn "Invoked $package with ".Dumper(\%kwargs);
181
182 5 foreach my $prop_name (keys(%kwargs))
183 {
184 17 my $prop_settings = $kwargs{$prop_name};
185 17 my $prop_methodname = "${package}::$prop_name";
186 17 my $prop_method;
187
188 17 ▶ if( # regular property
Branch: 5 branches
BranchTrueFalseTotal
if (exists $prop_settings->{'get'} and exists $prop_settings->{'set'}) { } 6 11 17
elsif (exists $prop_settings->{'get_lazy'} and exists $prop_settings->{'set'}) { } 4 7 11
elsif (exists $prop_settings->{'get'}) { } 3 4 7
elsif (exists $prop_settings->{'get_lazy'}) { } 1 3 4
elsif (exists $prop_settings->{'set'}) { } 3 0 3
Condition100%3/3
exists $prop_settings->{"get"} and exists $prop_settings->{"set"}
!ll&&!rl&&r
836
MC/DC100%2/2
exists $prop_settings->{"get"} and exists $prop_settings->{"set"}
exists $prop_settings->{"get"} exists $prop_settings->{"set"}
Truth table100%3/3
A and B
A= exists $prop_settings->{"get"}B= exists $prop_settings->{"set"}
ABresult
0X0
100
111
189 exists $prop_settings->{'get'}
190 and exists $prop_settings->{'set'}
191 )
192 {
193 6 my( $get_type, $set_type ) = ( ref $prop_settings->{'get'}, ref $prop_settings->{'set'} );
194
195 6 ▶ if( $get_type eq 'CODE' and $set_type eq 'CODE' ) # custom setter and gettter
Branch: 3 branches
BranchTrueFalseTotal
if ($get_type eq 'CODE' and $set_type eq 'CODE') { } 1 5 6
elsif ($get_type eq 'CODE') { } 1 4 5
elsif ($set_type eq 'CODE') { } 1 3 4
Condition100%3/3
$get_type eq "CODE" and $set_type eq "CODE"
!ll&&!rl&&r
411
MC/DC100%2/2
$get_type eq "CODE" and $set_type eq "CODE"
$get_type eq "CODE" $set_type eq "CODE"
Truth table100%3/3
A and B
A= $get_type eq "CODE"B= $set_type eq "CODE"
ABresult
0X0
100
111
196 {
197 1 $prop_method = $GEN->{'custom'}->(@{$prop_settings}{'get', 'set'});
198 }
199 elsif( $get_type eq 'CODE' ) # custom getter and default setter
200 {
201 1 $prop_method = $GEN->{'custom_get_default_set'}->($prop_name, @{$prop_settings}{'get'});
202 }
203 elsif( $set_type eq 'CODE' ) # default getter and custom setter
204 {
205 1 $prop_method = $GEN->{'default_get_custom_set'}->($prop_name, @{$prop_settings}{'set'});
206 }
207 else # default getter and setter
208 {
209 3 $prop_method = $GEN->{'default'}->($prop_name);
210 }
211 }
212 ▶ elsif( # regular property with lazy init
Condition100%3/3
exists $prop_settings->{"get_lazy"} and exists $prop_settings->{"set"}
!ll&&!rl&&r
614
MC/DC100%2/2
exists $prop_settings->{"get_lazy"} and exists $prop_settings->{"set"}
exists $prop_settings->{"get_lazy"} exists $prop_settings->{"set"}
Truth table100%3/3
A and B
A= exists $prop_settings->{"get_lazy"}B= exists $prop_settings->{"set"}
ABresult
0X0
100
111
213 exists $prop_settings->{'get_lazy'}
214 and exists $prop_settings->{'set'}
215 )
216 {
217 4 ▶ croak 'get_lazy parameter should be a coderef' if ref $prop_settings->{'get_lazy'} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $prop_settings->{"get_lazy"} ne "CODE" 0 4 4
218 4 my $set_type = ref $prop_settings->{'set'};
219 4 ▶ if( $set_type eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if ($set_type eq 'CODE') { } 1 3 4
220 {
221 1 $prop_method = $GEN->{'lazy_get_default_set'}->($prop_name, $prop_settings->{'get_lazy'}, $prop_settings->{'set'});
222 }
223 else
224 {
225 3 $prop_method = $GEN->{'default_lazy'}->($prop_name, $prop_settings->{'get_lazy'});
226 }
227 }
228 elsif( exists $prop_settings->{'get'} ) # ro property
229 {
230 3 ▶ if( ref $prop_settings->{'get'} eq 'CODE' ) # RO custom getter
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prop_settings->{'get'} eq 'CODE') { } 1 2 3
231 {
232 1 $prop_method = $GEN->{'custom_ro'}->($prop_name, $prop_settings->{'get'});
233 }
234 else
235 {
236 2 $prop_method = $GEN->{'default_ro'}->($prop_name);
237 }
238 }
239 elsif( exists $prop_settings->{'get_lazy'} ) # ro property with lazy init
240 {
241 1 ▶ croak 'get_lazy parameter should be a coderef' if ref $prop_settings->{'get_lazy'} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $prop_settings->{"get_lazy"} ne "CODE" 0 1 1
242 1 $prop_method = $GEN->{'lazy_ro'}->($prop_name, $prop_settings->{'get_lazy'});
243 }
244 elsif( exists $prop_settings->{'set'} ) # wo property
245 {
246 3 ▶ if( ref $prop_settings->{'set'} eq 'CODE' ) # WO custom setter
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prop_settings->{'set'} eq 'CODE') { } 1 2 3
247 {
248 1 $prop_method = $GEN->{'custom_wo'}->($prop_name, $prop_settings->{'set'});
249 }
250 else
251 {
252 2 $prop_method = $GEN->{'default_wo'}->($prop_name);
253 }
254 }
255
256 17 ▶ if(defined $prop_method)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prop_method) 17 0 17
257 {
258 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
259 17 *{$prop_methodname} = $prop_method;
260 }
261 }
262
263 5 return $package;
264 1 };
265
266 1 push @EXPORT, 'property';
267 2 ▶sub property{ return $make_property->( (caller)[0], @_);}
Subroutine property: called CC=1 SCAR=0.0
Pod undocumented
268 1 push @EXPORT, 'rw_property';
269 1 ▶sub rw_property{ return $make_property->( (caller)[0], map{$_ => {'set' => undef, 'get' => undef }} @_);}
Subroutine rw_property: called CC=1 SCAR=0.0
Pod undocumented
270 1 push @EXPORT, 'ro_property';
271 1 ▶sub ro_property{ return $make_property->( (caller)[0], map{$_ => {'get' => undef }} @_);}
Subroutine ro_property: called CC=1 SCAR=0.0
Pod undocumented
272 1 push @EXPORT, 'wo_property';
273 1 ▶sub wo_property{ return $make_property->( (caller)[0], map{$_ => {'set' => undef }} @_);}
Subroutine wo_property: called CC=1 SCAR=0.0
Pod undocumented
274
275 __END__
↑ Summary Property/RW/Lazy/CustomSet.pm »