Builder/Base.pm

filter: Statementstmt 98.6% 142 / 144 Branchbran 60.0% 24 / 40 Conditioncond 53.3% 8 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 34 / 34 Podpod 88.0% 22 / 25 totaltotal 87.2% 232 / 266 CC 34 SCAR 11.0
CC
34
Cov
87%
__ANON__
7.0
new
5.2
default_hash_add
4.2
CRAP 3.0
↑ Summary Builder.pm »
1 package Meta::Builder::Base;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Meta::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
9 5 my $class = shift;
10 5 my ( $package, %metrics ) = @_;
11 5 my $meta = $class->meta_meta;
12 5 my $self = bless( [ $package ], $class );
13  
14 5 for my $metric ( keys %{ $meta->{metrics} }) {
15 11 my $idx = $meta->{metrics}->{$metric};
16 $self->[$idx] = $metrics{$metric}
17 11 ▶ || $meta->{generators}->[$idx]->();
Condition33%1/3
$metrics{$metric} || $meta->{'generators'}[$idx]->()
l!l&&r!l&&!r
0110
MC/DC0%0/2
$metrics{$metric} || $meta->{'generators'}[$idx]->()
$metrics{$metric} $meta->{'generators'}[$idx]->()
Truth table33%1/3
A || B
A= $metrics{$metric}B= $meta->{'generators'}[$idx]->()
ABresult
1X1
011
000
18 }
19  
20 inject(
21 $package,
22 ($meta->{accessor} || croak "$class does not have an accessor set."),
23 1 ▶ sub { $self }
Subroutine __ANON__: called CC=1 SCAR=0.0
24 5 ▶ );
Condition50%1/2
$meta->{'accessor'} || croak("$class does not have an accessor set.")
l!l
50
MC/DC0%0/1
$meta->{'accessor'} || croak("$class does not have an accessor set.")
$meta->{'accessor'}
Truth table50%1/2
A
A= $meta->{'accessor'}
Aresult
11
00
25  
26 5 ▶ $self->init( %metrics ) if $self->can( 'init' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init") 0 5 5
27  
28 5 return $self;
29 }
30  
31 58 ▶sub meta_meta {
Subroutine meta_meta: called CC=2 SCAR=6.9
Pod documented
32 58 my $class = shift;
33 58 ▶ return $class->_meta_meta
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_meta_meta") 56 2 58
34 if $class->can( '_meta_meta' );
35  
36 2 my $meta = { index => 1 };
37 2 ▶ inject( $class, "_meta_meta", sub { $meta });
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 return $meta;
39 }
40  
41 1 ▶sub package { shift->[0] }
Subroutine package: called CC=1 SCAR=0.0
Pod documented
42  
43 2 ▶sub set_accessor {
Subroutine set_accessor: called CC=1 SCAR=0.0
Pod documented
44 2 my $class = shift;
45 2 ($class->meta_meta->{accessor}) = @_;
46 }
47  
48 3 ▶sub add_hash_metric {
Subroutine add_hash_metric: called CC=1 SCAR=0.0
Pod documented
49 3 my $class = shift;
50 3 my ( $metric, %actions ) = @_;
51 3 $class->add_metric(
52 $metric,
53 \&gen_hash,
54 add => \&default_hash_add,
55 get => \&default_hash_get,
56 has => \&default_hash_has,
57 clear => \&default_hash_clear,
58 pull => \&default_hash_pull,
59 merge => \&default_hash_merge,
60 %actions,
61 );
62 }
63  
64 3 ▶sub add_lists_metric {
Subroutine add_lists_metric: called CC=1 SCAR=0.0
Pod documented
65 3 my $class = shift;
66 3 my ( $metric, %actions ) = @_;
67 3 $class->add_metric(
68 $metric,
69 \&gen_hash,
70 push => \&default_list_push,
71 get => \&default_list_get,
72 has => \&default_list_has,
73 clear => \&default_list_clear,
74 pull => \&default_list_pull,
75 merge => \&default_list_merge,
76 %actions,
77 );
78 }
79  
80 8 ▶sub add_metric {
Subroutine add_metric: called CC=2 SCAR=6.9
Pod documented
81 8 my $class = shift;
82 8 my ( $metric, $generator, %actions ) = @_;
83 8 my $meta = $class->meta_meta;
84 8 my $index = $meta->{index}++;
85  
86 croak "Already tracking metric '$metric'"
87 8 ▶ if $meta->{metrics}->{$metric};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"metrics"}{$metric} 0 8 8
88  
89 8 $meta->{metrics}->{$metric} = $index;
90 8 $meta->{generators}->[$index] = $generator;
91 8 $meta->{indexes}->{$index} = $metric;
92  
93 8 ▶ inject( $class, $metric, sub { shift->[$index] });
Subroutine __ANON__: called CC=1 SCAR=0.0
94 8 $class->add_action( $metric, %actions );
95 }
96  
97 10 ▶sub add_action {
Subroutine add_action: called CC=2 SCAR=6.9
Pod documented
98 10 my $class = shift;
99 10 my ( $metric, %actions ) = @_;
100 $class->_add_action( $metric, $_, $actions{ $_ })
101 10 for keys %actions;
102 }
103  
104 39 ▶sub _add_action {
Subroutine _add_action: called CC=4 SCAR=14.2
105 39 my $class = shift;
106 39 my ( $metric, $action, $code ) = @_;
107 39 ▶ croak "You must specify a metric, an action name, and a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $metric and $action and $code 0 39 39
Condition33%2/6
$metric and $action and $code
!ll&&!rl&&r
0039
$metric and $action
!ll&&!rl&&r
0039
MC/DC0%0/3
$metric and $action and $code
$metric $action $code
Truth table25%1/4
A and B and C
A= $metricB= $actionC= $code
ABCresult
0XX0
10X0
1100
1111
108 unless $metric && $action && $code;
109  
110 39 my $meta = $class->meta_meta;
111 39 my $name = $class->action_method_name( $metric, $action );
112  
113 35 ▶ inject( $class, $name, sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
114 35 my $self = shift;
115 35 my $args = \@_;
116 $_->( $self, $self->$metric, $metric, $action, @$args )
117 35 ▶ for @{ $meta->{before}->{$name} || [] };
Condition100%2/2
$meta->{'before'}{$name} || []
l!l
233
MC/DC100%1/1
$meta->{'before'}{$name} || []
$meta->{'before'}{$name}
Truth table100%2/2
A
A= $meta->{'before'}{$name}
Aresult
11
00
118 34 my @out = $code->( $self, $self->$metric, $metric, $action, @$args );
119 $_->( $self, $self->$metric, $metric, $action, @$args )
120 33 ▶ for @{ $meta->{after}->{$name} || [] };
Condition100%2/2
$meta->{'after'}{$name} || []
l!l
132
MC/DC100%1/1
$meta->{'after'}{$name} || []
$meta->{'after'}{$name}
Truth table100%2/2
A
A= $meta->{'after'}{$name}
Aresult
11
00
121 32 ▶ return @out ? (@out > 1 ? @out : $out[0]) : ();
Branch: 2 branches
BranchTrueFalseTotal
@out ? : 32 0 32
@out > 1 ? : 1 31 32
122 39 });
123 }
124  
125 74 ▶sub action_method_name {
Subroutine action_method_name: called CC=1 SCAR=0.0
Pod documented
126 74 my $class = shift;
127 74 my ( $metric, $action ) = @_;
128 74 return "$metric\_$action";
129 }
130  
131 1 ▶sub hook_before {
Subroutine hook_before: called CC=1 SCAR=0.0
Pod documented
132 1 my $class = shift;
133 1 my ( $metric, $action, $code ) = @_;
134 1 my $name = $class->action_method_name( $metric, $action );
135 1 push @{ $class->meta_meta->{before}->{$name} } => $code;
136 }
137  
138 1 ▶sub hook_after {
Subroutine hook_after: called CC=1 SCAR=0.0
Pod documented
139 1 my $class = shift;
140 1 my ( $metric, $action, $code ) = @_;
141 1 my $name = $class->action_method_name( $metric, $action );
142 1 push @{ $class->meta_meta->{after}->{$name} } => $code;
143 }
144  
145 10 ▶sub gen_hash { {} }
Subroutine gen_hash: called CC=1 SCAR=0.0
Pod documented
146  
147 9 ▶sub default_hash_add {
Subroutine default_hash_add: called CC=4 SCAR=14.3
Pod documented
148 9 my $self = shift;
149 9 my ( $data, $metric, $action, $item, @value ) = @_;
150 9 my $name = $self->action_method_name( $metric, $action );
151 9 ▶ croak "$name() called without anything to add"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 9 9
152 unless $item;
153  
154 9 ▶ croak "$name('$item') called without a value to add"
Branch: 1 branch
BranchTrueFalseTotal
unless @value 0 9 9
155 unless @value;
156  
157 croak "'$item' already added for metric $metric"
158 9 ▶ if $data->{$item};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$item} 0 9 9
159  
160 9 ($data->{$item}) = @value;
161 }
162  
163 3 ▶sub default_hash_get {
Subroutine default_hash_get: called CC=3 SCAR=11.3
Pod documented
164 3 my $self = shift;
165 3 my ( $data, $metric, $action, $item ) = @_;
166 3 my $name = $self->action_method_name( $metric, $action );
167 3 ▶ croak "$name() called without anything to get"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 3 3
168 unless $item;
169  
170 # Prevent autovivication
171 return exists $data->{$item}
172 3 ▶ ? $data->{$item}
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$item} ? : 3 0 3
173 : undef;
174 }
175  
176 8 ▶sub default_hash_has {
Subroutine default_hash_has: called CC=3 SCAR=11.0
Pod documented
177 8 my $self = shift;
178 8 my ( $data, $metric, $action, $item ) = @_;
179 8 my $name = $self->action_method_name( $metric, $action );
180 8 ▶ croak "$name() called without anything to find"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 8 8
181 unless $item;
182 8 ▶ return exists $data->{$item} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $data->{$item} ? : 3 5 8
183 }
184  
185 3 ▶sub default_hash_clear {
Subroutine default_hash_clear: called CC=2 SCAR=7.0
Pod documented
186 3 my $self = shift;
187 3 my ( $data, $metric, $action, $item ) = @_;
188 3 my $name = $self->action_method_name( $metric, $action );
189 3 ▶ croak "$name() called without anything to clear"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 3 3
190 unless $item;
191 3 delete $data->{$item};
192 3 return 1;
193 }
194  
195 1 ▶sub default_hash_pull {
Subroutine default_hash_pull: called CC=2 SCAR=7.0
Pod documented
196 1 my $self = shift;
197 1 my ( $data, $metric, $action, $item ) = @_;
198 1 my $name = $self->action_method_name( $metric, $action );
199 1 ▶ croak "$name() called without anything to pull"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 1 1
200 unless $item;
201 1 return delete $data->{$item};
202 }
203  
204 2 ▶sub default_hash_merge {
Subroutine default_hash_merge: called CC=3 SCAR=11.0
Pod undocumented
205 2 my $self = shift;
206 2 my ( $data, $metric, $action, $merge ) = @_;
207 2 for my $key ( keys %$merge ) {
208 croak "$key is defined for $metric in both meta-objects"
209 3 ▶ if $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$key} 1 2 3
210 2 $data->{$key} = $merge->{$key};
211 }
212 }
213  
214 6 ▶sub default_list_push {
Subroutine default_list_push: called CC=3 SCAR=11.2
Pod documented
215 6 my $self = shift;
216 6 my ( $data, $metric, $action, $item, @values ) = @_;
217 6 my $name = $self->action_method_name( $metric, $action );
218 6 ▶ croak "$name() called without an item to which data should be pushed"
Branch: 1 branch
BranchTrueFalseTotal
unless $item 0 6 6
219 unless $item;
220  
221 6 ▶ croak "$name('$item') called without values to push"
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 6 6
222 unless @values;
223  
224 6 push @{$data->{$item}} => @values;
225 }
226  
227 2 ▶sub default_list_get {
Subroutine default_list_get: called CC=2 SCAR=7.2
Pod documented
228 2 my $data = default_hash_get(@_);
229 2 ▶ return $data ? @$data : ();
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 2 0 2
230 }
231  
232 6 ▶sub default_list_has {
Subroutine default_list_has: called CC=1 SCAR=0.0
Pod documented
233 6 default_hash_has( @_ );
234 }
235  
236 2 ▶sub default_list_clear {
Subroutine default_list_clear: called CC=1 SCAR=0.0
Pod documented
237 2 default_hash_clear( @_ );
238 }
239  
240 1 ▶sub default_list_pull {
Subroutine default_list_pull: called CC=1 SCAR=0.0
Pod documented
241 1 my @out = default_list_get( @_ );
242 1 default_list_clear( @_ );
243 1 return @out;
244 }
245  
246 1 ▶sub default_list_merge {
Subroutine default_list_merge: called CC=2 SCAR=6.9
Pod undocumented
247 1 my $self = shift;
248 1 my ( $data, $metric, $action, $merge ) = @_;
249 1 for my $key ( keys %$merge ) {
250 1 push @{ $data->{$key} } => @{ $merge->{$key} };
251 }
252 }
253  
254 2 ▶sub merge {
Subroutine merge: called CC=3 SCAR=11.6
Pod undocumented
255 2 my $self = shift;
256 2 my ( $merge ) = @_;
257 2 for my $metric ( keys %{ $self->meta_meta->{ metrics }}) {
258 3 my $mergesub = $self->action_method_name( $metric, 'merge' );
259 3 ▶ unless( $self->can( $mergesub )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($mergesub)) 0 3 3
260 0 carp "Cannot merge metric '$metric', define a 'merge' action for it.";
261 0 next;
262 }
263 3 $self->$mergesub( $merge->$metric );
264 }
265 }
266  
267 2 1;
268  
269 __END__
↑ Summary Builder.pm »