Circle/Collection.pm

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