Fritz/ConfigFile.pm

filter: Statementstmt 100% 49 / 49 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 98.6% 72 / 73 CC 9 SCAR 10.2
CC
9
Cov
100%
_find_default_configfile
5.0
_build_configuration
3.0
__ANON__
2.0
CRAP 2.8
« Fritz/Device.pm ↑ Summary Fritz/Action.pm »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # Copyright (C) 2017 Christian Garbs <mitch@cgarbs.de>
4 # Licensed under GNU GPL v2 or later.
5  
6 package Net::Fritz::ConfigFile;
7 # ABSTRACT: configuration file handler for L<Net::Fritz::Box>
8 6 $Net::Fritz::ConfigFile::VERSION = 'v0.0.9';
9  
10 6 ▶use AppConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 12 ▶sub _find_default_configfile {
Subroutine _find_default_configfile: called CC=5 SCAR=16.1
17 12 my $original = shift;
18  
19 12 my $try;
20  
21 12 ▶ if (exists $ENV{XDG_CONFIG_HOME}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{"XDG_CONFIG_HOME"}) 6 6 12
22 6 $try = File::Spec->catfile( $ENV{XDG_CONFIG_HOME}, 'fritzrc' );
23 6 ▶ return $try if -e $try;
Branch: 1 branch
BranchTrueFalseTotal
if -e $try 3 3 6
24 }
25  
26 9 $try = File::Spec->catfile( $ENV{HOME}, '.config', 'fritzrc' );
27 9 ▶ return $try if -e $try;
Branch: 1 branch
BranchTrueFalseTotal
if -e $try 2 7 9
28  
29 7 $try = File::Spec->catfile( $ENV{HOME}, '.fritzrc' );
30 7 ▶ return $try if -e $try;
Branch: 1 branch
BranchTrueFalseTotal
if -e $try 2 5 7
31  
32 5 return $original;
33 }
34  
35  
36 12 ▶has configfile => ( is => 'ro' , coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
37  
38 12 my $configfile = shift;
39  
40 # expand empty filename to default ~/.fritzrc
41 12 ▶ if (! $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($configfile) 4 8 12
42 4 $configfile = _find_default_configfile($configfile);
43 }
44  
45 # expand ~ to $HOME
46 12 $configfile =~ s/^~/$ENV{HOME}/;
47  
48 12 return $configfile;
49  
50 6 } );
51  
52  
53  
54
55 6 has configuration => ( is => 'lazy' );
56  
57 6 ▶sub _build_configuration {
Subroutine _build_configuration: called CC=3 SCAR=11.0
58  
59 6 my $self = shift;
60  
61 6 ▶ return {} unless $self->configfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->configfile 1 5 6
62  
63 5 my $app_config = AppConfig->new();
64 5 $app_config->define('upnp_url=s');
65 5 $app_config->define('trdesc_path=s');
66 5 $app_config->define('username=s');
67 5 $app_config->define('password=s');
68  
69 5 $app_config->file($self->configfile);
70  
71 5 my %config_vars = $app_config->varlist('^');
72  
73 # remove all missing configuration variables
74 5 delete $config_vars{$_} foreach grep {!defined $config_vars{$_}} keys %config_vars;
75  
76 5 return \%config_vars;
77 };
78  
79  
80 # prepend 'configfile => ' when called without hash
81 # (when called with uneven list)
82 13 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=6.9
Pod undocumented
83 13 my ( $class, @args ) = @_;
84
85 13 ▶ unshift @args, "configfile" if @args % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 11 2 13
86
87 13 return { @args };
88 };
89  
90  
91 6 1;
92  
93 __END__
« Fritz/Device.pm ↑ Summary Fritz/Action.pm »