blib/lib/Mooish/AttributeBuilder.pm

filter: Statementstmt 100% 122 / 122 Branchbran 84.2% 32 / 38 Conditioncond 82.5% 33 / 40 MC/DCmcdc 63.6% 14 / 22 Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 10 totaltotal 88.0% 228 / 259 CC 39 SCAR 15.7
CC
39
Cov
94%
__ANON__
13.0
import
5.0
__ANON__
5.0
CRAP 4.8
↑ Summary
1 package Mooish::AttributeBuilder;
2 15 $Mooish::AttributeBuilder::VERSION = '1.003';
3 15 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 my $set_subname;
11 15 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.5
12 15 ▶ if (eval { require Sub::Util } && Sub::Util->VERSION >= 1.40) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Sub::Util } } and "Sub::Util"->VERSION >= "1.4") 15 0 15
Condition33%1/3
eval { do { require Sub::Util } } and "Sub::Util"->VERSION >= "1.4"
!ll&&!rl&&r
0015
MC/DC0%0/2
eval { do { require Sub::Util } } and "Sub::Util"->VERSION >= "1.4"
eval { do { require Sub::Util } } "Sub::Util"->VERSION >= "1.4"
Truth table33%1/3
A and B
A= eval { do { require Sub::Util } }B= "Sub::Util"->VERSION >= "1.4"
ABresult
0X0
100
111
13 15 $set_subname = \&Sub::Util::set_subname;
14 }
15 }
16  
17 ### These subs can be extended in subclasses
18  
19 # List of available attribute types. May be extended if a custom function will
20 # call expand_shortcuts
21 sub attribute_types
22 121 ▶{
Subroutine attribute_types: called CC=1 SCAR=0.0
Pod undocumented
23 return {
24 121 field => {
25 is => 'ro',
26 init_arg => undef,
27 },
28 param => {
29 is => 'ro',
30 required => 1,
31 },
32 option => {
33 is => 'ro',
34 required => 0,
35 predicate => 1,
36 },
37 extended => {},
38 };
39 }
40  
41 # Prefix of hidden methods. Will be joined with the rest of the method name
42 # with an underscore, so an empty prefix means starting with an underscore
43 sub hidden_prefix
44 13 ▶{
Subroutine hidden_prefix: called CC=1 SCAR=0.0
Pod undocumented
45 13 return '';
46 }
47  
48 # The list of methods which are hidden by default
49 sub hidden_methods
50 15 ▶{
Subroutine hidden_methods: called CC=1 SCAR=0.0
Pod undocumented
51 return {
52 15 builder => 1,
53 trigger => 1,
54 };
55 }
56  
57 # The list of method name prefixes. Undef means no prefix at all, just use
58 # attribute name
59 sub method_prefixes
60 52 ▶{
Subroutine method_prefixes: called CC=1 SCAR=0.0
Pod undocumented
61 return {
62 52 reader => 'get',
63 writer => 'set',
64 clearer => 'clear',
65 predicate => 'has',
66 builder => 'build',
67 trigger => 'trigger',
68 init_arg => undef,
69 };
70 }
71  
72 ### General functions called in sub context
73  
74 sub import
75 17 ▶{
Subroutine import: called CC=5 SCAR=16.1
76 17 my ($self, $caller) = (shift, scalar caller);
77 17 state $export_cache = {};
78  
79 17 my %flags = map { $_ => $_ } @_;
80  
81 17 ▶ my $cache_key = $self . ($flags{-standard} || '');
Condition100%2/2
$flags{'-standard'} || ''
l!l
116
MC/DC100%1/1
$flags{'-standard'} || ''
$flags{'-standard'}
Truth table100%2/2
A
A= $flags{'-standard'}
Aresult
11
00
82 17 foreach my $type (keys %{$self->attribute_types}) {
83 12 ▶ my $function = $export_cache->{$cache_key . $type} //= sub {
Subroutine extended: called CC=1 SCAR=0.0
Subroutine field: called CC=1 SCAR=0.0
84 12 my ($name, %args) = @_;
85 12 return $self->expand_shortcuts($flags{-standard}, $type => $name, %args);
86 69 ▶ };
Condition66%2/3
$$export_cache{$cache_key . $type} //= sub { my($name, %args) = @_; return $self->expand_shortcuts($flags{"-standard"}, $type, $name, %args); }
l!l&&r!l&&!r
8610
MC/DC0%0/2
$$export_cache{$cache_key . $type} //= sub { my($name, %args) = @_; return $self->expand_shortcuts($flags{"-standard"}, $type, $name, %args); }
$$export_cache{$cache_key . $type} sub { my($name, %args) = @_; return $self->expand_shortcuts($flags{"-standard"}, $type, $name, %args); }
Truth table66%2/3
A //= B
A= $$export_cache{$cache_key . $type}B= sub { my($name, %args) = @_; return $self->expand_shortcuts($flags{"-standard"}, $type, $name, %args); }
ABresult
1X1
011
000
87  
88 69 ▶ $set_subname->("${self}::${type}", $function)
Branch: 1 branch
BranchTrueFalseTotal
if $set_subname 69 0 69
89 if $set_subname;
90  
91 NO_STRICT: {
92 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 69 *{"${caller}::${type}"} = $function;
94 }
95 }
96 }
97  
98 my @custom_shortcuts;
99  
100 sub custom_shortcuts
101 51 ▶{
Subroutine custom_shortcuts: called CC=1 SCAR=0.0
Pod undocumented
102 51 return [@custom_shortcuts];
103 }
104  
105 sub add_shortcut
106 4 ▶{
Subroutine add_shortcut: called CC=2 SCAR=7.0
Pod undocumented
107 4 my ($sub) = @_;
108  
109 4 ▶ croak 'Custom shortcut passed to add_shortcut must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 4 4
110 unless ref $sub eq 'CODE';
111  
112 4 push @custom_shortcuts, $sub;
113 4 return;
114 }
115  
116 sub standard_shortcuts
117 52 ▶{
Subroutine standard_shortcuts: called CC=1 SCAR=0.0
Pod undocumented
118 52 my ($self) = @_;
119  
120 return [
121 # expand attribute type
122 52 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
123 52 my ($name, %args) = @_;
124 52 my $type = delete $args{_type};
125  
126 52 ▶ if ($type && $self->attribute_types->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and $self->attribute_types->{$type}) 52 0 52
Condition33%1/3
$type and $self->attribute_types->{$type}
!ll&&!rl&&r
0052
MC/DC0%0/2
$type and $self->attribute_types->{$type}
$type $self->attribute_types->{$type}
Truth table33%1/3
A and B
A= $typeB= $self->attribute_types->{$type}
ABresult
0X0
100
111
127 %args = (
128 52 %{$self->attribute_types->{$type}},
129 %args,
130 );
131 }
132  
133 52 return %args;
134 },
135  
136 # merge lazy + default / lazy + builder
137 52 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
138 52 my ($name, %args) = @_;
139  
140 52 ▶ if ($args{lazy} && !exists $args{default} && !$args{builder}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"lazy"} and not exists $args{"default"} and not $args{"builder"}) 5 47 52
Condition100%6/6
$args{"lazy"} and not exists $args{"default"} and not $args{"builder"}
!ll&&!rl&&r
4615
$args{"lazy"} and not exists $args{"default"}
!ll&&!rl&&r
4516
MC/DC100%3/3
$args{"lazy"} and not exists $args{"default"} and not $args{"builder"}
$args{"lazy"} not exists $args{"default"} not $args{"builder"}
Truth table100%4/4
A and not(B) and not(C)
A= $args{"lazy"}B= not exists $args{"default"}C= not $args{"builder"}
ABCresult
0XX0
10X0
1100
1111
141 5 my $lazy = $args{lazy};
142 5 $args{lazy} = 1;
143  
144 5 ▶ if (ref $lazy eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lazy eq 'CODE') { } 3 2 5
145 3 check_and_set(\%args, $name, default => $lazy);
146 }
147 else {
148 2 check_and_set(\%args, $name, builder => $lazy);
149 }
150 }
151  
152 52 return %args;
153 },
154  
155 # merge coerce + isa
156 52 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
157 52 my ($name, %args) = @_;
158  
159 52 ▶ if (blessed $args{coerce}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args{"coerce"})) 1 51 52
160 1 check_and_set(\%args, $name, isa => $args{coerce});
161 1 $args{coerce} = 1;
162 }
163  
164 52 return %args;
165 },
166  
167 # make sure params with defaults are not required
168 52 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
169 52 my ($name, %args) = @_;
170  
171 52 ▶ if ($args{required} && (exists $args{default} || $args{builder})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"required"} and exists $args{"default"} || $args{"builder"}) 1 51 52
Condition83%5/6
$args{"required"} and exists $args{"default"} || $args{"builder"}
!ll&&!rl&&r
4561
exists $args{"default"} or $args{"builder"}
l!l&&r!l&&!r
106
MC/DC66%2/3
$args{"required"} and exists $args{"default"} || $args{"builder"}
$args{"required"} exists $args{"default"} $args{"builder"}
Truth table75%3/4
A and B or C
A= $args{"required"}B= exists $args{"default"}C= $args{"builder"}
ABCresult
0XX0
1000
11X1
1011
172 1 delete $args{required};
173 }
174  
175 52 return %args;
176 },
177  
178 # method names from shortcuts
179 52 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=25.7
180 52 my ($name, %args) = @_;
181  
182 # initialized lazily
183 52 my $normalized_name;
184 my $hidden_field;
185  
186 # inflate names from shortcuts
187 52 my %prefixes = %{$self->method_prefixes};
188 52 foreach my $method_type (keys %prefixes) {
189 362 ▶ next unless defined $args{$method_type};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{$method_type} 328 34 362
190 34 ▶ next if ref $args{$method_type};
Branch: 1 branch
BranchTrueFalseTotal
if ref $args{$method_type} 0 34 34
191 34 ▶ next unless grep { $_ eq $args{$method_type} } '1', -public, -hidden;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $args{$method_type};} 1, "-public", "-hidden" 2 32 34
192  
193 32 ▶ $normalized_name //= get_normalized_name($name, $method_type);
Condition100%3/3
$normalized_name //= get_normalized_name($name, $method_type)
l!l&&r!l&&!r
1301
MC/DC100%2/2
$normalized_name //= get_normalized_name($name, $method_type)
$normalized_name get_normalized_name($name, $method_type)
Truth table100%3/3
A //= B
A= $normalized_nameB= get_normalized_name($name, $method_type)
ABresult
1X1
011
000
194 31 ▶ $hidden_field //= $name ne $normalized_name;
Condition100%2/2
$hidden_field //= $name ne $normalized_name
l!l
130
MC/DC100%1/1
$hidden_field //= $name ne $normalized_name
$hidden_field
Truth table100%2/2
A
A= $hidden_field
Aresult
11
00
195  
196 my $is_hidden =
197 $args{$method_type} eq -hidden
198 || (
199 $args{$method_type} eq '1'
200 31 ▶ && ($hidden_field || $self->hidden_methods->{$method_type})
Condition100%9/9
$args{$method_type} eq '-hidden' || $args{$method_type} eq '1' && ($hidden_field || $self->hidden_methods->{$method_type})
l!l&&r!l&&!r
7717
$args{$method_type} eq 1 and $hidden_field || $self->hidden_methods->{$method_type}
!ll&&!rl&&r
7107
$hidden_field or $self->hidden_methods->{$method_type}
l!l&&r!l&&!r
2510
MC/DC100%4/4
$args{$method_type} eq '-hidden' || $args{$method_type} eq '1' && ($hidden_field || $self->hidden_methods->{$method_type})
$args{$method_type} eq '-hidden' $args{$method_type} eq 1 $hidden_field $self->hidden_methods->{$method_type}
Truth table100%5/5
A || B and C or D
A= $args{$method_type} eq '-hidden'B= $args{$method_type} eq 1C= $hidden_fieldD= $self->hidden_methods->{$method_type}
ABCDresult
1XXX1
011X1
01011
00XX0
01000
201 );
202  
203 93 ▶ $args{$method_type} = join '_', grep { defined }
Branch: 1 branch
BranchTrueFalseTotal
$is_hidden ? : 14 17 31
204 ($is_hidden ? $self->hidden_prefix : undef),
205 31 $prefixes{$method_type},
206 $normalized_name;
207 }
208  
209 # special treatment for trigger
210 51 ▶ if ($args{trigger} && !ref $args{trigger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"trigger"} and not ref $args{"trigger"}) 2 49 51
Condition66%2/3
$args{"trigger"} and not ref $args{"trigger"}
!ll&&!rl&&r
4902
MC/DC50%1/2
$args{"trigger"} and not ref $args{"trigger"}
$args{"trigger"} not ref $args{"trigger"}
Truth table66%2/3
A and not(B)
A= $args{"trigger"}B= not ref $args{"trigger"}
ABresult
0X0
100
111
211 2 my $trigger = $args{trigger};
212 2 ▶ $args{trigger} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
213 2 return shift->$trigger(@_);
214 2 };
215 }
216  
217 51 return %args;
218 },
219  
220 # literal parameters (prepended with -)
221 51 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
222 51 my ($name, %args) = @_;
223  
224 51 foreach my $literal (keys %args) {
225 155 ▶ if ($literal =~ m{\A - (.+) \z}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal =~ /\A - (.+) \z/x) 2 153 155
226 2 $args{$1} = delete $args{$literal};
227 }
228 }
229  
230 51 return %args;
231 },
232 52 ];
233 }
234  
235 sub expand_shortcuts
236 52 ▶{
Subroutine expand_shortcuts: called CC=5 SCAR=16.1
Pod undocumented
237 52 my ($self, $standard, $attribute_type, $name, %args) = @_;
238  
239 52 $args{_type} = $attribute_type;
240  
241 # NOTE: don't use custom shortcuts if we stick to the standard
242 52 my @filters;
243 52 ▶ push @filters, @{$self->custom_shortcuts} unless $standard;
Branch: 1 branch
BranchTrueFalseTotal
unless $standard 51 1 52
244 52 push @filters, @{$self->standard_shortcuts};
245  
246 # NOTE: builtin shortcuts are executed after custom shortcuts
247 52 foreach my $sub (@filters) {
248 314 %args = $sub->($name, %args);
249 }
250  
251 # TODO: dirty hack for 'extended' attribute. Can be done better?
252 51 ▶ if ($attribute_type eq 'extended') {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute_type eq "extended") 4 47 51
253 4 ▶ if (ref $name eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name eq 'ARRAY') { } 2 2 4
254 2 $name = [map { "+$_" } @{$name}];
255 }
256 else {
257 2 $name = "+$name";
258 }
259 }
260  
261 51 return ($name, %args);
262 }
263  
264 ### Helpers - not called in pkg context
265  
266 sub check_and_set
267 6 ▶{
Subroutine check_and_set: called CC=3 SCAR=11.1
Pod undocumented
268 6 my ($hash_ref, $name, %pairs) = @_;
269  
270 6 foreach my $key (keys %pairs) {
271 croak "Could not expand shortcut: $key already exists for $name"
272 6 ▶ if exists $hash_ref->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash_ref->{$key} 0 6 6
273  
274 6 $hash_ref->{$key} = $pairs{$key};
275 }
276  
277 6 return;
278 }
279  
280 sub get_normalized_name
281 31 ▶{
Subroutine get_normalized_name: called CC=2 SCAR=6.9
Pod undocumented
282 31 my ($name, $for) = @_;
283  
284 31 ▶ croak "Could not use attribute shortcut with array fields: $for is not supported"
Branch: 1 branch
BranchTrueFalseTotal
if ref $name 1 30 31
285 if ref $name;
286  
287 30 $name =~ s/^_//;
288 30 return $name;
289 }
290  
291 15 1;
292  
293 # ABSTRACT: build Mooish attribute definitions with less boilerplate
294  
↑ Summary