blib/lib/Circle/Collection.pm

filter: Statementstmt 48.5% 100 / 206 Branchbran 39.3% 37 / 94 Conditioncond 34.3% 11 / 32 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 38.8% 7 / 18 Podpod n/a totaltotal 42.8% 158 / 369 SCAR 69.1
CC
74
Cov
45%
import
279.2
__ANON__
132.0
__ANON__
110.0
CRAP 1006.2
« blib/lib/Circle/RootObj.pm ↑ Summary blib/lib/Circle/Loggable.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2014 -- leonerd@leonerd.org.uk
4  
5 package Circle::Collection;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 require attributes;
12  
13 4 ▶use Attribute::Storage qw( apply_subattrs_for_pkg );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Class::Method::Modifiers qw( install_modifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # A template role to merge
17 sub import
18 {
19 16 ▶ my $pkg = shift;
Subroutine import: called CC=35 SCAR=56.3
20 16 my %args = @_;
21  
22 16 my $caller = caller;
23  
24 16 ▶ my $name = $args{name} or croak "Need a collection name";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $args{"name"} or croak("Need a collection name") 0 16 16
25  
26 16 ▶ my $attrs = $args{attrs} or croak "Need attrs";
Branch: 1 branch
BranchTrueFalseTotal
my $attrs = $args{"attrs"} or croak("Need attrs") 0 16 16
27 16 ▶ ref $attrs eq "ARRAY" or croak "Expected 'attrs' to be an ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs eq "ARRAY" or croak("Expected 'attrs' to be an ARRAY") 0 16 16
28  
29 16 ▶ my $desc2 = $args{desc_plural} || $name;
Condition33%1/3
$args{'desc_plural'} || $name
l!l&&r!l&&!r
0160
MC/DC0%0/2
$args{'desc_plural'} || $name
$args{'desc_plural'} $name
Truth table33%1/3
A || B
A= $args{'desc_plural'}B= $name
ABresult
1X1
011
000
30 16 ▶ my $desc1 = $args{desc_single} || do { $_ = $name; s/s$//; $_ };
Condition33%1/3
$args{'desc_single'} || do { $_ = $name; s/s$//; $_ }
l!l&&r!l&&!r
0160
MC/DC0%0/2
$args{'desc_single'} || do { $_ = $name; s/s$//; $_ }
$args{'desc_single'} do { $_ = $name; s/s$//; $_ }
Truth table33%1/3
A || B
A= $args{'desc_single'}B= do { $_ = $name; s/s$//; $_ }
ABresult
1X1
011
000
31  
32 16 ▶ my $storage = $args{storage} or croak "Need a storage type";
Branch: 1 branch
BranchTrueFalseTotal
my $storage = $args{"storage"} or croak("Need a storage type") 0 16 16
33 16 my $config = $args{config};
34  
35 # Now parse it down to several fields
36 16 my @attrs_all;
37 my @attrs_persisted;
38 16 my %attrs;
39  
40 16 ▶ for( my $i = 0; $i < @$attrs; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$attrs) 52 16 68
41 52 my $name = $attrs->[$i];
42 52 my $a = $attrs->[$i+1];
43  
44 52 push @attrs_all, $name;
45 52 ▶ push @attrs_persisted, $name unless $a->{transient};
Branch: 1 branch
BranchTrueFalseTotal
unless $a->{"transient"} 48 4 52
46  
47 52 $attrs{$name} = $a;
48 }
49  
50 16 my $keyattr = $attrs_all[0];
51  
52 16 my %commands;
53 16 ▶ %commands = %{ $args{commands} } if $args{commands};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"commands"} 4 12 16
54  
55 # Data access code
56  
57 16 my ( $method_list, $method_get, $method_set, $method_add, $method_del );
58  
59 16 ▶ if( ref $storage eq "HASH" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $storage eq 'HASH') { } 8 8 16
elsif ($storage eq 'methods') { } 4 4 8
elsif ($storage eq 'array') { } 4 0 4
60 8 $method_list = $storage->{list};
61 8 $method_get = $storage->{get};
62 8 $method_set = $storage->{set};
63 8 $method_add = $storage->{add};
64 8 $method_del = $storage->{del};
65 }
66 elsif( $storage eq "methods" ) {
67 4 $method_list = "${name}_list";
68 4 $method_get = "${name}_get";
69 4 $method_set = "${name}_set";
70 4 $method_add = "${name}_add";
71 4 $method_del = "${name}_del";
72 }
73 elsif( $storage eq "array" ) {
74 $method_list = sub {
75 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 return @{ $self->{$name} }
77 4 };
78  
79 $method_get = sub {
80 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ( $key ) = @_;
82 0 return ( grep { $_->{$keyattr} eq $key } @{ $self->{$name} } )[0];
83 4 };
84  
85 $method_add = sub {
86 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 my ( $key, $item ) = @_;
88 # TODO: something with key
89 0 push @{ $self->{$name} }, $item;
90 4 };
91  
92 $method_del = sub {
93 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
94 0 my ( $key, $item ) = @_;
95  
96 0 my $items = $self->{$name};
97 0 my ( $idx ) = grep { $items->[$_] == $item } 0 .. $#$items;
98  
99 0 ▶ return 0 unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 0 0 0
100  
101 0 splice @$items, $idx, 1, ();
102 0 return 1;
103 4 };
104 }
105 else {
106 0 croak "Unrecognised storage type $storage";
107 }
108  
109 # Manipulation commands
110  
111 16 ▶ unless( exists $commands{list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{"list"}) 16 0 16
112 16 ▶ defined $method_list or croak "No list method defined for list subcommand";
Branch: 1 branch
BranchTrueFalseTotal
defined $method_list or croak("No list method defined for list subcommand") 0 16 16
113  
114 $commands{list} = apply_subattrs_for_pkg( $caller,
115 Command_description => qq("List the $desc2"),
116 Command_subof => qq('$name'),
117 Command_default => qq(),
118 sub {
119 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=5 SCAR=34.0
120 0 my ( $cinv ) = @_;
121  
122 0 my @items = $self->$method_list;
123  
124 0 ▶ unless( @items ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items) 0 0 0
125 0 $cinv->respond( "No $desc2" );
126 0 return;
127 }
128  
129 0 my @table;
130  
131 0 foreach my $item ( @items ) {
132 0 my @shown_item;
133 0 foreach my $attr ( @attrs_all ) {
134 0 my $value = $item->{$attr};
135 0 ▶ push @shown_item, exists $attrs{$attr}{show} ? $attrs{$attr}{show}->( local $_ = $value ) : $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs{$attr}{'show'} ? : 0 0 0
136 }
137 0 push @table, \@shown_item;
138 }
139  
140 0 $cinv->respond_table( \@table, headings => \@attrs_all );
141 0 return;
142 }
143 16 );
144 }
145  
146 16 my @opts_add;
147 my @opts_mod;
148  
149 16 foreach ( @attrs_persisted ) {
150 48 ▶ next if $_ eq $keyattr;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $keyattr 16 32 48
151  
152 32 ▶ my $desc = $attrs{$_}{desc} || $_;
Condition66%2/3
$attrs{$_}{'desc'} || $_
l!l&&r!l&&!r
2480
MC/DC0%0/2
$attrs{$_}{'desc'} || $_
$attrs{$_}{'desc'} $_
Truth table66%2/3
A || B
A= $attrs{$_}{'desc'}B= $_
ABresult
1X1
011
000
153  
154 32 ▶ $desc .= qq[ (default \\"$attrs{$_}{default}\\")] if exists $attrs{$_}{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attrs{$_}{"default"} 4 28 32
155  
156 32 push @opts_add, qq('$_=\$', desc => "$desc");
157  
158 push @opts_mod, qq('$_=\$', desc => "$desc"),
159 32 ▶ qq('no-$_=+', desc => "remove $_") unless $attrs{$_}{nomod};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{$_}{"nomod"} 24 8 32
160 }
161  
162 16 ▶ unless( exists $commands{add} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{"add"}) 12 4 16
163 12 ▶ defined $method_add or croak "No add method defined for add subcommand";
Branch: 1 branch
BranchTrueFalseTotal
defined $method_add or croak("No add method defined for add subcommand") 0 12 12
164  
165 $commands{add} = apply_subattrs_for_pkg( $caller,
166 Command_description => qq("Add a $desc1"),
167 Command_subof => qq('$name'),
168 Command_arg => qq('$keyattr'),
169 28 ( map { +Command_opt => $_ } @opts_add ),
170 sub {
171 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=7 SCAR=40.3
172 0 my ( $key, $opts, $cinv ) = @_;
173  
174 0 ▶ if( $self->$method_get( $key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$method_get($key)) 0 0 0
175 0 $cinv->responderr( "Already have a $desc1 '$key'" );
176 0 return;
177 }
178  
179 0 my $item = { $keyattr => $key };
180 0 ▶ exists $attrs{$_}{default} and $item->{$_} = $attrs{$_}{default} for @attrs_persisted;
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs{$_}{"default"} and $item->{$_} = $attrs{$_}{"default"} 0 0 0
181  
182 0 ▶ defined $opts->{$_} and $item->{$_} = $opts->{$_} for @attrs_persisted;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{$_} and $item->{$_} = $opts->{$_} 0 0 0
183  
184 0 ▶ unless( eval { $self->$method_add( $key, $item ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->$method_add($key, $item); 1 } }) 0 0 0
185 0 my $err = "$@"; chomp $err;
186 0 $cinv->responderr( "Cannot add $desc1 '$key' - $err" );
187 0 return;
188 }
189  
190 0 $cinv->respond( "Added $desc1 '$key'" );
191 0 return;
192 }
193 12 );
194 }
195  
196 16 ▶ unless( exists $commands{mod} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{"mod"}) 12 4 16
197 12 ▶ defined $method_get or croak "No get method defined for mod subcommand";
Branch: 1 branch
BranchTrueFalseTotal
defined $method_get or croak("No get method defined for mod subcommand") 0 12 12
198  
199 $commands{mod} = apply_subattrs_for_pkg( $caller,
200 Command_description => qq("Modify an existing $desc1"),
201 Command_subof => qq('$name'),
202 Command_arg => qq('$keyattr'),
203 48 ( map { +Command_opt => $_ } @opts_mod ),
204 sub {
205 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
206 0 my ( $key, $opts, $cinv ) = @_;
207  
208 0 my $item = $self->$method_get( $key );
209  
210 0 ▶ unless( $item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item) 0 0 0
211 0 $cinv->responderr( "No such $desc1 '$key'" );
212 0 return;
213 }
214  
215 0 my %mod;
216 0 ▶ exists $opts->{$_} and $mod{$_} = $opts->{$_} for @attrs_persisted;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{$_} and $mod{$_} = $opts->{$_} 0 0 0
217 0 ▶ exists $opts->{"no-$_"} and $mod{$_} = $attrs{$_}{default} for @attrs_persisted;
Branch: 1 branch
BranchTrueFalseTotal
exists $$opts{"no-$_"} and $mod{$_} = $attrs{$_}{"default"} 0 0 0
218  
219 0 ▶ if( $method_set ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_set) { } 0 0 0
220 0 $self->$method_set( $key, \%mod );
221 }
222 else {
223 0 $item->{$_} = $mod{$_} for keys %mod;
224 }
225  
226 0 $cinv->respond( "Modified $desc1 '$key'" );
227 0 return;
228 }
229 12 );
230 }
231  
232 16 ▶ unless( exists $commands{del} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $commands{"del"}) 12 4 16
233 12 ▶ defined $method_del or croak "No del method defined for del subcommand";
Branch: 1 branch
BranchTrueFalseTotal
defined $method_del or croak("No del method defined for del subcommand") 0 12 12
234  
235 $commands{del} = apply_subattrs_for_pkg( $caller,
236 Command_description => qq("Delete a $desc1"),
237 Command_subof => qq('$name'),
238 Command_arg => qq('$keyattr'),
239 sub {
240 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
241 0 my ( $key, $cinv ) = @_;
242  
243 0 my $item = $self->$method_get( $key );
244  
245 0 ▶ unless( $item ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item) 0 0 0
246 0 $cinv->responderr( "No such $desc1 '$key'" );
247 0 return;
248 }
249  
250 0 ▶ unless( eval { $self->$method_del( $key, $item ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->$method_del($key, $item); 1 } }) 0 0 0
251 0 my $err = "$@"; chomp $err;
252 0 $cinv->responderr( "Cannot delete $desc1 '$key' - $err" );
253 0 return;
254 }
255  
256 0 $cinv->respond( "Removed $desc1 '$key'" );
257 0 return;
258 }
259 12 );
260 }
261  
262 # Now delete present-but-undef ones; these are where the caller vetoed the
263 # above autogeneration
264 16 ▶ defined $commands{$_} or delete $commands{$_} for keys %commands;
Branch: 1 branch
BranchTrueFalseTotal
defined $commands{$_} or delete $commands{$_} 12 52 64
265  
266 16 my %subs;
267 16 $subs{"command_${name}_$_"} = $commands{$_} for keys %commands;
268  
269 $subs{"command_${name}"} = apply_subattrs_for_pkg( $caller,
270 Command_description => qq("Display or manipulate $desc2"),
271 # body matters not but it needs to be a cloned closure
272 16 ▶ do { my $dummy; sub { undef $dummy } }
Subroutine __ANON__: not called CC=1 SCAR=0.2
273 );
274  
275 {
276 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
277 16 *{"${caller}::$_"} = $subs{$_} for keys %subs;
278 }
279  
280 16 ▶ if( !defined $config or $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $config or $config) 12 4 16
Condition100%3/3
not defined $config or $config
l!l&&r!l&&!r
844
MC/DC100%2/2
not defined $config or $config
not defined $config $config
Truth table100%3/3
not(A) or B
A= not defined $configB= $config
ABresult
1X1
011
000
281 12 ▶ my $config_type = $config->{type} || "array";
Condition100%2/2
$config->{'type'} || 'array'
l!l
48
MC/DC100%1/1
$config->{'type'} || 'array'
$config->{'type'}
Truth table100%2/2
A
A= $config->{'type'}
Aresult
11
00
282 12 my $type_array = $config_type eq "array";
283 12 my $type_hash = $config_type eq "hash";
284 12 ▶ $type_array or $type_hash or
Branch: 1 branch
BranchTrueFalseTotal
$type_array or $type_hash or die "Expected config type either 'array' or 'hash'" 0 12 12
Condition66%2/3
$type_array or $type_hash
l!l&&r!l&&!r
840
MC/DC0%0/2
$type_array or $type_hash
$type_array $type_hash
Truth table66%2/3
A or B
A= $type_arrayB= $type_hash
ABresult
1X1
011
000
285 die "Expected config type either 'array' or 'hash'";
286  
287 # Optional config-related methods
288 12 my $method_store = $config->{store};
289 12 my $method_load = $config->{load};
290  
291 # Configuration load/store
292 install_modifier $caller, after => load_configuration => sub {
293 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=10 SCAR=47.0
294 0 my ( $ynode ) = @_;
295  
296 0 ▶ my $ynodes = $ynode->{$name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ynodes = $ynode->{$name} or return 0 0 0
297  
298 0 ▶ foreach my $this ( $type_array ? @$ynodes : keys %$ynodes ) {
Branch: 1 branch
BranchTrueFalseTotal
$type_array ? : 0 0 0
299 0 my $item = {};
300 0 ▶ my $n = $type_array ? $this : $ynodes->{$this};
Branch: 1 branch
BranchTrueFalseTotal
$type_array ? : 0 0 0
301  
302 0 $item->{$_} = $n->{$_} for @attrs_persisted;
303 0 ▶ $item->{$keyattr} = $this if $type_hash;
Branch: 1 branch
BranchTrueFalseTotal
if $type_hash 0 0 0
304  
305 0 $self->$method_add( $item->{$keyattr}, $item );
306  
307 0 ▶ if( ref $method_load or $method_load && $self->can( $method_load ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_load or $method_load and $self->can($method_load)) 0 0 0
Condition0%0/6
ref $method_load or $method_load and $self->can($method_load)
l!l&&r!l&&!r
000
$method_load and $self->can($method_load)
!ll&&!rl&&r
000
MC/DC0%0/3
ref $method_load or $method_load and $self->can($method_load)
ref $method_load $method_load $self->can($method_load)
Truth table0%0/4
A or B and C
A= ref $method_loadB= $method_loadC= $self->can($method_load)
ABCresult
1XX1
0111
00X0
0100
308 0 $self->$method_load( $item->{$keyattr}, $n );
309 }
310 }
311 12 };
312  
313 install_modifier $caller, after => store_configuration => sub {
314 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=11 SCAR=48.8
315 0 my ( $ynode ) = @_;
316  
317 0 ▶ my $ynodes = $ynode->{$name} ||= $type_array ? [] : YAML::Node->new({});
Branch: 1 branch
BranchTrueFalseTotal
$type_array ? : 0 0 0
Condition0%0/3
$ynode->{$name} ||= $type_array ? [] : "YAML::Node"->new({})
l!l&&r!l&&!r
000
MC/DC0%0/2
$ynode->{$name} ||= $type_array ? [] : "YAML::Node"->new({})
$ynode->{$name} $type_array ? [] : "YAML::Node"->new({})
Truth table0%0/3
A ||= B
A= $ynode->{$name}B= $type_array ? [] : "YAML::Node"->new({})
ABresult
1X1
011
000
318 0 ▶ $type_array ? ( @$ynodes = () ) : ( %$ynodes = () );
Branch: 1 branch
BranchTrueFalseTotal
$type_array ? : 0 0 0
319  
320 0 foreach my $item ( $self->$method_list ) {
321 0 my $n = YAML::Node->new({});
322  
323 0 ▶ defined $item->{$_} and $n->{$_} = $item->{$_} for @attrs_persisted;
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{$_} and $n->{$_} = $item->{$_} 0 0 0
324  
325 0 ▶ if( ref $method_store or $method_store && $self->can( $method_store ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method_store or $method_store and $self->can($method_store)) 0 0 0
Condition0%0/6
ref $method_store or $method_store and $self->can($method_store)
l!l&&r!l&&!r
000
$method_store and $self->can($method_store)
!ll&&!rl&&r
000
MC/DC0%0/3
ref $method_store or $method_store and $self->can($method_store)
ref $method_store $method_store $self->can($method_store)
Truth table0%0/4
A or B and C
A= ref $method_storeB= $method_storeC= $self->can($method_store)
ABCresult
1XX1
0111
00X0
0100
326 0 $self->$method_store( $item->{$keyattr}, $n );
327 }
328  
329 $type_array ?
330 ( push @$ynodes, $n ) :
331 0 ▶ do { $ynodes->{$n->{$keyattr}} = $n; delete $n->{$keyattr} };
Branch: 1 branch
BranchTrueFalseTotal
$type_array ? : 0 0 0
332 }
333 12 };
334 }
335 }
336  
337 4 0x55AA;
« blib/lib/Circle/RootObj.pm ↑ Summary blib/lib/Circle/Loggable.pm »