| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 16 | ▶ | my $attrs = $args{attrs} or croak "Need attrs"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 16 | ▶ | ref $attrs eq "ARRAY" or croak "Expected 'attrs' to be an ARRAY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 16 | ▶ | my $desc2 = $args{desc_plural} || $name; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'desc_plural'} || $name
MC/DC0%0/2
$args{'desc_plural'} || $name
$args{'desc_plural'}
$name
Truth table33%1/3
A || B
A= $args{'desc_plural'}B= $name
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 16 | ▶ | my $desc1 = $args{desc_single} || do { $_ = $name; s/s$//; $_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$args{'desc_single'} || do {
$_ = $name;
s/s$//;
$_
}
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$//;
$_
}
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 16 | ▶ | my $storage = $args{storage} or croak "Need a storage type"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 16 | ▶ | defined $method_list or croak "No list method defined for list subcommand"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 32 | ▶ | my $desc = $attrs{$_}{desc} || $_; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$attrs{$_}{'desc'} || $_
MC/DC0%0/2
$attrs{$_}{'desc'} || $_
$attrs{$_}{'desc'}
$_
Truth table66%2/3
A || B
A= $attrs{$_}{'desc'}B= $_
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 32 | ▶ | $desc .= qq[ (default \\"$attrs{$_}{default}\\")] if exists $attrs{$_}{default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 16 | ▶ | unless( exists $commands{add} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 12 | ▶ | defined $method_add or croak "No add method defined for add subcommand"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | defined $opts->{$_} and $item->{$_} = $opts->{$_} for @attrs_persisted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | unless( eval { $self->$method_add( $key, $item ); 1 } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 12 | ▶ | defined $method_get or croak "No get method defined for mod subcommand"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | exists $opts->{"no-$_"} and $mod{$_} = $attrs{$_}{default} for @attrs_persisted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | ▶ | if( $method_set ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | 12 | ▶ | defined $method_del or croak "No del method defined for del subcommand"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $config or $config
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
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 12 | ▶ | my $config_type = $config->{type} || "array"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'type'} || 'array'
MC/DC100%1/1
$config->{'type'} || 'array'
$config->{'type'}
Truth table100%2/2
A
A= $config->{'type'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$type_array or $type_hash
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | foreach my $this ( $type_array ? @$ynodes : keys %$ynodes ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my $item = {}; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | my $n = $type_array ? $this : $ynodes->{$this}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $item->{$_} = $n->{$_} for @attrs_persisted; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | $item->{$keyattr} = $this if $type_hash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
ref $method_load or $method_load and $self->can($method_load)
$method_load and $self->can($method_load)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$ynode->{$name} ||= $type_array ? [] : "YAML::Node"->new({})
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({})
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | $type_array ? ( @$ynodes = () ) : ( %$ynodes = () ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | if( ref $method_store or $method_store && $self->can( $method_store ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
ref $method_store or $method_store and $self->can($method_store)
$method_store and $self->can($method_store)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 12 | }; | |||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | 4 | 0x55AA; | |||||||||||||||||||||||||||||||||||||||||