Circle/Configurable.pm

filter: Statementstmt 43.7% 74 / 169 Branchbran 12.5% 8 / 64 Conditioncond 6.9% 3 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 66.6% 20 / 30 Podpod 0.0% 0 / 10 totaltotal 30.7% 105 / 342 CC 54 SCAR 53.3
CC
54
Cov
31%
command_set
650.0
__ANON__
56.0
load_configuration
42.0
CRAP 207.4
↑ Summary Circle/Net/IRC.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::Configurable;
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 base qw( Circle::Commandable );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.173320';
13  
14 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Attribute::Storage qw( get_subattr get_subattrs apply_subattrs_for_pkg find_subs_with_attr );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Data::Dump qw( pp );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 require mro;
19  
20 #############################################
21 ### Attribute handlers for setting_* subs ###
22 #############################################
23  
24 my %setting_types = (
25 str => {},
26  
27 int => {
28 0 ▶ check => sub { m/^\d+$/ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 },
30  
31 bool => {
32 0 ▶ parse => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
33 0 ▶ return 1 if lc $_ eq "true" or lc $_ eq "on" or $_ eq "1";
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ eq "true" or lc $_ eq "on" or $_ eq 1 0 0 0
Condition0%0/6
lc $_ eq "true" or lc $_ eq "on" or $_ eq 1
l!l&&r!l&&!r
000
lc $_ eq "true" or lc $_ eq "on"
l!l&&r!l&&!r
000
MC/DC0%0/3
lc $_ eq "true" or lc $_ eq "on" or $_ eq 1
lc $_ eq "true" lc $_ eq "on" $_ eq 1
Truth table0%0/4
A or B or C
A= lc $_ eq "true"B= lc $_ eq "on"C= $_ eq 1
ABCresult
1XX1
01X1
0011
0000
34 0 ▶ return 0 if lc $_ eq "false" or lc $_ eq "off" or $_ eq "0";
Branch: 1 branch
BranchTrueFalseTotal
if lc $_ eq "false" or lc $_ eq "off" or $_ eq 0 0 0 0
Condition0%0/6
lc $_ eq "false" or lc $_ eq "off" or $_ eq 0
l!l&&r!l&&!r
000
lc $_ eq "false" or lc $_ eq "off"
l!l&&r!l&&!r
000
MC/DC0%0/3
lc $_ eq "false" or lc $_ eq "off" or $_ eq 0
lc $_ eq "false" lc $_ eq "off" $_ eq 0
Truth table0%0/4
A or B or C
A= lc $_ eq "false"B= lc $_ eq "off"C= $_ eq 0
ABCresult
1XX1
01X1
0011
0000
35 0 die;
36 },
37 0 ▶ print => sub { $_ ? "true" : "false" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
38 },
39 4 );
40  
41 32 ▶sub Setting_description :ATTR(CODE)
Subroutine Setting_description: called CC=1 SCAR=0.0
Pod undocumented
42 {
43 32 my $class = shift;
44 32 my ( $text ) = @_;
45  
46 32 return $text;
47 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 32 ▶sub Setting_type :ATTR(CODE)
Subroutine Setting_type: called CC=2 SCAR=7.0
Pod undocumented
50 {
51 32 my $class = shift;
52 32 my ( $typename ) = @_;
53  
54 32 ▶ exists $setting_types{$typename} or croak "Not a recognised type name '$typename'";
Branch: 1 branch
BranchTrueFalseTotal
exists $setting_types{$typename} or croak("Not a recognised type name '${typename}'") 32 0 32
55  
56 32 return $setting_types{$typename};
57 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 4 ▶sub Setting_default :ATTR(CODE)
Subroutine Setting_default: called CC=1 SCAR=0.0
Pod undocumented
60 {
61 4 my $class = shift;
62 4 my ( $value ) = @_;
63  
64 4 return $value;
65 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 8 ▶sub Setting_inheritable :ATTR(CODE)
Subroutine Setting_inheritable: called CC=1 SCAR=0.0
Pod undocumented
68 {
69 8 return 1;
70 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 sub APPLY_Setting
73 24 ▶{
Subroutine APPLY_Setting: called CC=3 SCAR=12.4
Pod undocumented
74 24 my $class = shift;
75 24 my ( $name, %args ) = @_;
76  
77 24 ▶ my $storage = $args{storage} || $name;
Condition66%2/3
$args{'storage'} || $name
l!l&&r!l&&!r
4200
MC/DC0%0/2
$args{'storage'} || $name
$args{'storage'} $name
Truth table66%2/3
A || B
A= $args{'storage'}B= $name
ABresult
1X1
011
000
78  
79 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 24 *{"${class}::setting_$name"} = apply_subattrs_for_pkg $class,
81 Setting_description => qq("\Q$args{description}\E"),
82 Setting_type => qq("\Q$args{type}\E"),
83 ( exists $args{default} ?
84 ( Setting_default => pp($args{default}) ) : () ),
85 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my $self = shift;
87 0 my ( $newvalue ) = @_;
88  
89 0 ▶ $self->{$storage} = $newvalue if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
90 0 return $self->{$storage};
91 24 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'default'} ? : 4 20 24
92 }
93  
94 sub APPLY_Inheritable_Setting
95 8 ▶{
Subroutine APPLY_Inheritable_Setting: called CC=3 SCAR=11.7
Pod undocumented
96 8 my $class = shift;
97 8 my ( $name, %args ) = @_;
98  
99 8 ▶ my $storage = $args{storage} || $name;
Condition33%1/3
$args{'storage'} || $name
l!l&&r!l&&!r
080
MC/DC0%0/2
$args{'storage'} || $name
$args{'storage'} $name
Truth table33%1/3
A || B
A= $args{'storage'}B= $name
ABresult
1X1
011
000
100  
101 8 my $setting = "setting_$name";
102  
103 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104 8 *{"${class}::setting_$name"} = apply_subattrs_for_pkg $class,
105 Setting_description => qq("\Q$args{description}\E"),
106 Setting_type => qq("\Q$args{type}\E"),
107 Setting_inheritable => qq(),
108 ( exists $args{default} ?
109 ( Setting_default => pp($args{default}) ) : () ),
110 12 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
111 12 my $self = shift;
112 12 my ( $newvalue ) = @_;
113  
114 12 ▶ $self->{$storage} = $newvalue if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12 12
115 12 ▶ return $self->{$storage} if defined $self->{$storage};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$storage} 0 12 12
116 12 ▶ if( my $parent = $self->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->parent) { } 4 8 12
117 4 return $parent->$setting;
118 }
119 else {
120 8 return undef;
121 }
122 8 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'default'} ? : 0 8 8
123 8 ▶ *{"${class}::_setting_${name}_inherits"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=14.6
124 0 my $self = shift;
125 0 ▶ return $self->parent && !defined $self->{$storage};
Condition0%0/3
$self->parent && !defined($self->{$storage})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->parent && !defined($self->{$storage})
$self->parent !defined($self->{$storage})
Truth table0%0/3
A && not(B)
A= $self->parentB= !defined($self->{$storage})
ABresult
0X0
100
111
126 8 };
127 }
128  
129 sub _get_settings
130 0 ▶{
Subroutine _get_settings: not called CC=5 SCAR=34.0
131 0 my $self = shift;
132  
133 0 ▶ my $class = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
134  
135 0 my %subs = find_subs_with_attr( mro::get_linear_isa( $class ), "Setting_description",
136 matching => qr/^setting_/
137 );
138  
139 0 my %settings;
140 0 foreach my $name ( keys %subs ) {
141 0 ( my $settingname = $name ) =~ s/^setting_//;
142 0 my $cv = $subs{$name};
143  
144 0 my $attrs = $settings{$settingname} = get_subattrs( $cv );
145 0 ▶ m/^Setting_(.*)$/ and $attrs->{$1} = delete $attrs->{$_} for keys %$attrs;
Branch: 1 branch
BranchTrueFalseTotal
/^Setting_(.*)$/ and $attrs->{$1} = delete $attrs->{$_} 0 0 0
146 }
147  
148 0 return \%settings;
149 }
150  
151 sub command_set
152 0 ▶ : Command_description("Display or manipulate configuration settings")
Subroutine command_set: not called CC=25 SCAR=64.8
Pod undocumented
153 : Command_arg('setting?')
154 : Command_arg('value?')
155 : Command_opt('inherit=+', desc => "Inherit value from parent")
156 : Command_opt('help=+', desc => "Display help on setting(s)")
157 : Command_opt('values=+', desc => "Display value of each setting")
158 {
159 0 my $self = shift;
160 0 my ( $setting, $newvalue, $opts, $cinv ) = @_;
161  
162 0 my $opt_inherit = $opts->{inherit};
163 0 my $opt_help = $opts->{help};
164 0 my $opt_values = $opts->{values};
165  
166 0 ▶ if( !defined $setting ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $setting) 0 0 0
167 0 my $settings = $self->_get_settings;
168  
169 0 ▶ keys %$settings or $cinv->respond( "No settings exist" ), return;
Branch: 1 branch
BranchTrueFalseTotal
keys %$settings or $cinv->respond("No settings exist"), return 0 0 0
170  
171 0 ▶ if( $opt_values ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_values) { } 0 0 0
172 0 my @table;
173 0 foreach my $settingname ( sort keys %$settings ) {
174 0 $setting = $settings->{$settingname};
175  
176 0 my $curvalue = $self->can( "setting_$settingname" )->( $self );
177 0 ▶ if( $setting->{type}->{print} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"type"}{"print"}) 0 0 0
178 0 $curvalue = $setting->{type}->{print}->( local $_ = $curvalue );
179 }
180  
181 0 ▶ if( $setting->{inheritable} && $self->can( "_setting_${settingname}_inherits" )->( $self ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setting->{"inheritable"} and $self->can("_setting_${settingname}_inherits")->($self)) 0 0 0
Condition0%0/3
$setting->{"inheritable"} and $self->can("_setting_${settingname}_inherits")->($self)
!ll&&!rl&&r
000
MC/DC0%0/2
$setting->{"inheritable"} and $self->can("_setting_${settingname}_inherits")->($self)
$setting->{"inheritable"} $self->can("_setting_${settingname}_inherits")->($self)
Truth table0%0/3
A and B
A= $setting->{"inheritable"}B= $self->can("_setting_${settingname}_inherits")->($self)
ABresult
0X0
100
111
182 0 $settingname .= " [I]";
183 }
184  
185 0 ▶ push @table, [
Branch: 1 branch
BranchTrueFalseTotal
defined $curvalue ? : 0 0 0
186 $settingname,
187 ( defined $curvalue ? $curvalue : "" ),
188 ];
189 }
190  
191 0 $self->respond_table( \@table, colsep => ": ", headings => [ "Setting", "Value" ] );
192 }
193 else {
194 0 my @table;
195 0 foreach my $settingname ( sort keys %$settings ) {
196 0 my $setting = $settings->{$settingname};
197  
198 0 ▶ push @table, [ $settingname, ( $setting->{Setting_description} || "[no description]" ) ];
Condition0%0/2
$setting->{'Setting_description'} || '[no description]'
l!l
00
MC/DC0%0/1
$setting->{'Setting_description'} || '[no description]'
$setting->{'Setting_description'}
Truth table0%0/2
A
A= $setting->{'Setting_description'}
Aresult
11
00
199 }
200  
201 0 $cinv->respond_table( \@table, colsep => " - ", headings => [ "Setting", "Description" ] );
202 }
203  
204 0 return;
205 }
206  
207 0 my $cv = $self->can( "setting_$setting" );
208 0 ▶ if( !defined $cv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cv) 0 0 0
209 0 $cinv->responderr( "No such setting $setting" );
210 0 return;
211 }
212  
213 0 ▶ if( $opt_help ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_help) 0 0 0
214 0 ▶ my $description = get_subattr( $cv, 'Setting_description' ) || "[no description]";
Condition0%0/2
get_subattr($cv, 'Setting_description') || '[no description]'
l!l
00
MC/DC0%0/1
get_subattr($cv, 'Setting_description') || '[no description]'
get_subattr($cv, 'Setting_description')
Truth table0%0/2
A
A= get_subattr($cv, 'Setting_description')
Aresult
11
00
215 0 $cinv->respond( "$setting - $description" );
216 0 return;
217 }
218  
219 0 my $type = get_subattr( $cv, 'Setting_type' );
220  
221 0 my $curvalue;
222 0 ▶ if( defined $newvalue or $opt_inherit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newvalue or $opt_inherit) { } 0 0 0
Condition0%0/3
defined $newvalue or $opt_inherit
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $newvalue or $opt_inherit
defined $newvalue $opt_inherit
Truth table0%0/3
A or B
A= defined $newvalueB= $opt_inherit
ABresult
1X1
011
000
223 0 ▶ if( !$opt_inherit and $type->{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt_inherit and $type->{"check"}) 0 0 0
Condition0%0/3
not $opt_inherit and $type->{"check"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $opt_inherit and $type->{"check"}
not $opt_inherit $type->{"check"}
Truth table0%0/3
not(A) and B
A= not $opt_inheritB= $type->{"check"}
ABresult
0X0
100
111
224 0 local $_ = $newvalue;
225 0 ▶ $type->{check}->( $newvalue ) or
Branch: 1 branch
BranchTrueFalseTotal
$type->{"check"}->($newvalue) or $cinv->responderr("'${newvalue}' is not a valid value for $setting"), return 0 0 0
226 $cinv->responderr( "'$newvalue' is not a valid value for $setting" ), return;
227 }
228  
229 0 ▶ if( !$opt_inherit and $type->{parse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opt_inherit and $type->{"parse"}) 0 0 0
Condition0%0/3
not $opt_inherit and $type->{"parse"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $opt_inherit and $type->{"parse"}
not $opt_inherit $type->{"parse"}
Truth table0%0/3
not(A) and B
A= not $opt_inheritB= $type->{"parse"}
ABresult
0X0
100
111
230 0 local $_ = $newvalue;
231 0 ▶ eval { $newvalue = $type->{parse}->( $newvalue ); 1 } or
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $newvalue = $type->{"parse"}->($newvalue); 1 } } or $cinv->responderr("'${newvalue}' is not a valid value for $setting"), return 0 0 0
232 $cinv->responderr( "'$newvalue' is not a valid value for $setting" ), return;
233 }
234  
235 0 ▶ undef $newvalue if $opt_inherit;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_inherit 0 0 0
236 0 $curvalue = $cv->( $self, $newvalue );
237 }
238 else {
239 0 $curvalue = $cv->( $self );
240 }
241  
242 0 ▶ if( $type->{print} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->{"print"}) 0 0 0
243 0 local $_ = $curvalue;
244 0 $curvalue = $type->{print}->( local $_ = $curvalue );
245 }
246  
247 0 ▶ if( defined $curvalue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curvalue) { } 0 0 0
248 0 $cinv->respond( "$setting: $curvalue" );
249 }
250 else {
251 0 $cinv->respond( "$setting is not set" );
252 }
253  
254 0 return;
255 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
256  
257 sub get_configuration
258 0 ▶{
Subroutine get_configuration: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my $self = shift;
260  
261 0 my $ynode = YAML::Node->new({});
262 0 $self->store_configuration( $ynode );
263  
264 0 return $ynode;
265 }
266  
267 sub load_configuration
268 0 ▶{
Subroutine load_configuration: not called CC=6 SCAR=37.4
Pod undocumented
269 0 my $self = shift;
270 0 my ( $ynode ) = @_;
271  
272 0 foreach my $setting ( keys %{ $self->_get_settings } ) {
273 0 ▶ my $cv = $self->can( "setting_$setting" ) or croak "$self has no setting $setting";
Branch: 1 branch
BranchTrueFalseTotal
my $cv = $self->can("setting_$setting") or croak("$self has no setting $setting") 0 0 0
274 0 my $value = $ynode->{$setting};
275 0 ▶ if( !defined $value and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and defined(my $default = get_subattr($cv, "Setting_default"))) 0 0 0
Condition0%0/3
not defined $value and defined(my $default = get_subattr($cv, "Setting_default"))
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $value and defined(my $default = get_subattr($cv, "Setting_default"))
not defined $value defined(my $default = get_subattr($cv, "Setting_default"))
Truth table0%0/3
not(A) and B
A= not defined $valueB= defined(my $default = get_subattr($cv, "Setting_default"))
ABresult
0X0
100
111
276 defined( my $default = get_subattr( $cv, "Setting_default" ) ) ) {
277 0 $value = $default;
278 }
279 0 ▶ $cv->( $self, $value ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
280 }
281 }
282  
283 sub store_configuration
284 0 ▶{
Subroutine store_configuration: not called CC=4 SCAR=30.0
Pod undocumented
285 0 my $self = shift;
286 0 my ( $ynode ) = @_;
287  
288 0 foreach my $setting ( keys %{ $self->_get_settings } ) {
289 0 ▶ my $cv = $self->can( "setting_$setting" ) or croak "$self has no setting $setting";
Branch: 1 branch
BranchTrueFalseTotal
my $cv = $self->can("setting_$setting") or croak("$self has no setting $setting") 0 0 0
290 0 my $value = $cv->( $self );
291 0 ▶ $ynode->{$setting} = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
292 }
293 }
294  
295 4 0x55AA;
↑ Summary Circle/Net/IRC.pm »