blib/lib/Circle/Configurable.pm

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