WebFetch/Data/Config.pm

filter: Statementstmt 93.6% 44 / 47 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 85.7% 66 / 77 CC 8 SCAR 8.0
CC
8
Cov
90%
_class_or_obj
4.5
del
2.1
contains
2.0
CRAP 2.2
« WebFetch/Output/Capture.pm ↑ Summary WebFetch/Output/Dump.pm »
1 # WebFetch::Data::Config
2 # ABSTRACT: WebFetch configuration data management
3 #
4 # Copyright (c) 2022 Ian Kluft. This program is free software; you can
5 # redistribute it and/or modify it under the terms of the GNU General Public
6 # License Version 3. See https://www.gnu.org/licenses/gpl-3.0-standalone.html
7 #
8 # WebFetch::Data::Config contains configuration data for the WebFetch class hierarchy.
9 # It was made to replace older-generation code which depended on subclasses defining
10 # package variables. That is no longer considered good practice. This uses the singleton
11 # design pattern to maintain a simple key/value store for configuration data.
12  
13 # pragmas to silence some warnings from Perl::Critic
14 ## no critic (Modules::RequireExplicitPackage)
15 # This solves a catch-22 where parts of Perl::Critic want both package and use-strict to be first
16 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 ## use critic (Modules::RequireExplicitPackage)
20  
21 package WebFetch::Data::Config;
22 6 $WebFetch::Data::Config::VERSION = '0.15.9';
23 6 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use base 'Class::Singleton';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # helper function to allow methods to get the singleton instance whether called as a class or instance method
27 sub _class_or_obj
28 2378 ▶{
Subroutine _class_or_obj: called CC=4 SCAR=15.0
29 2378 my $coo = shift; # coo = class or object
30  
31 # safety net: all-stop if we received an undef
32 2378 ▶ if ( not defined $coo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $coo) 0 2378 2378
33 0 confess "coo got undef from:" . ( join "|", caller 1 );
34 }
35  
36 # safety net: the class or object must be WebFetch::Data::Config or a derivative
37 2378 ▶ if ( not $coo->isa("WebFetch::Data::Config") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($coo->isa("WebFetch::Data::Config")) 0 2378 2378
38 0 confess "incompatible class $coo from:" . ( join "|", caller 1 );
39 }
40  
41 # instance method if it got an object reference
42 2378 ▶ return $coo if ref $coo;
Branch: 1 branch
BranchTrueFalseTotal
if ref $coo 1570 808 2378
43  
44 # class method: return the instance via the instance() class method
45 # if the singleton object wasn't already instantiated, this will take care of it
46 # assumption: it must be string name of class WebFetch::Data::Config or subclass of it - so it has instance()
47 808 return $coo->instance();
48 }
49  
50 # check for existence of a config entry
51 sub contains
52 802 ▶{
Subroutine contains: called CC=2 SCAR=6.9
Pod undocumented
53 802 my ( $class_or_obj, $key ) = @_;
54 802 my $instance = _class_or_obj($class_or_obj);
55 802 ▶ return exists $instance->{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $instance->{$key} ? : 761 41 802
56 }
57  
58 # configuration read accessor
59 sub read_accessor
60 752 ▶{
Subroutine read_accessor: called CC=2 SCAR=6.9
Pod undocumented
61 752 my ( $class_or_obj, $key ) = @_;
62 752 my $instance = _class_or_obj($class_or_obj);
63 752 ▶ if ( $instance->contains($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->contains($key)) 735 17 752
64 735 return $instance->{$key};
65 }
66 17 return;
67 }
68  
69 # configuration write accessor
70 sub write_accessor
71 30 ▶{
Subroutine write_accessor: called CC=1 SCAR=0.0
Pod undocumented
72 30 my ( $class_or_obj, $key, $value ) = @_;
73 30 my $instance = _class_or_obj($class_or_obj);
74 30 $instance->{$key} = $value;
75 30 return;
76 }
77  
78 # configuration read/write accessor
79 # WebFetch's config() method calls here
80 sub accessor
81 782 ▶{
Subroutine accessor: called CC=2 SCAR=6.9
Pod undocumented
82 782 my ( $class_or_obj, $key, $value ) = @_;
83 782 my $instance = _class_or_obj($class_or_obj);
84  
85 # if no value is provided, use read accessor
86 782 ▶ if ( not defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 752 30 782
87 752 return $instance->read_accessor($key);
88 }
89  
90 # otherwise use write accessor
91 30 $instance->write_accessor( $key, $value );
92 30 return;
93 }
94  
95 # delete configuration item
96 sub del
97 12 ▶{
Subroutine del: called CC=2 SCAR=7.4
Pod undocumented
98 12 my ( $class_or_obj, $key ) = @_;
99 12 my $instance = _class_or_obj($class_or_obj);
100 12 ▶ if ( $instance->contains($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance->contains($key)) 12 0 12
101 12 return delete $instance->{$key};
102 }
103 0 return;
104 }
105  
106 6 1;
107  
108 __END__
« WebFetch/Output/Capture.pm ↑ Summary WebFetch/Output/Dump.pm »