blib/lib/App/GitHooks/Config.pm

filter: Statementstmt 100% 33 / 33 Branchbran 78.5% 11 / 14 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 84.7% 61 / 72 CC 11 SCAR 15.5
CC
11
Cov
88%
get
7.9
get_regex
5.0
new
2.0
CRAP 4.7
« blib/lib/App/GitHooks/Hook/PreCommit.pm ↑ Summary /home/cpancover/.cpan/build/App-GitHooks-v1.7.3-0/bin/githooks »
1 package App::GitHooks::Config;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # External dependencies.
7 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 =head1 NAME
12  
13 App::GitHooks::Config - Configuration manager for App::GitHooks.
14  
15  
16 =head1 VERSION
17  
18 Version 1.7.3
19  
20 =cut
21  
22 30 our $VERSION = '1.7.3';
23  
24  
25 =head1 SYNOPSIS
26  
27 my $config = App::GitHooks::Config->new();
28  
29 my $config = App::GitHooks::Config->new(
30 file => '...',
31 );
32  
33 my $value = $config->get( $section, $name );
34  
35  
36 =head1 METHODS
37  
38 =head2 new()
39  
40 Return a new C<App::GitHooks::Config> object.
41  
42 my $config = App::GitHooks::Config->new(
43 file => $file,
44 );
45  
46 Arguments:
47  
48 =over 4
49  
50 =item * file I<(optional)>
51  
52 A path to a config file to load into the object.
53  
54 =item * source I<(optional)>
55  
56 How the path of the config file to use was determined.
57  
58 =back
59  
60 =cut
61  
62 sub new
63 {
64 43 ▶ my ( $class, %args ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
65 43 my $file = delete( $args{'file'} );
66 43 my $source = delete( $args{'source'} );
67  
68 43 ▶ my $self = defined( $file )
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 43 0 43
69 ? Config::Tiny->read( $file )
70 : Config::Tiny->new();
71  
72 43 bless( $self, $class );
73  
74 # Store meta-information for future reference.
75 43 $self->{'__source'} = $source;
76 43 $self->{'__path'} = $file;
77  
78 43 return $self;
79 }
80  
81  
82 =head2 get()
83  
84 Retrieve the value for a given section and key name.
85  
86 my $value = $config->get( $section, $name );
87  
88 Note that the C<App::GitHooks> configuration files are organized by sections,
89 with the main (default) section being '_'.
90  
91 =cut
92  
93 sub get
94 {
95 190 ▶ my ( $self, $section, $name ) = @_;
Subroutine get: called CC=6 SCAR=20.7
Pod documented
96  
97 190 ▶ croak 'A section name is required as first argument'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $section or $section eq "" 0 190 190
Condition33%1/3
not defined $section or $section eq ""
l!l&&r!l&&!r
00190
MC/DC0%0/2
not defined $section or $section eq ""
not defined $section $section eq ""
Truth table33%1/3
not(A) or B
A= not defined $sectionB= $section eq ""
ABresult
1X1
011
000
98 if !defined( $section ) || ( $section eq '' );
99 190 ▶ croak 'A key name is required as second argument'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 190 190
Condition33%1/3
not defined $name or $name eq ""
l!l&&r!l&&!r
00190
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
100 if !defined( $name ) || ( $name eq '' );
101  
102 return defined( $self->{ $section } )
103 190 ▶ ? $self->{ $section }->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$section} ? : 133 57 190
104 : undef;
105 }
106  
107  
108 =head2 get_regex()
109  
110 Retrieve the specified regex for a given section and key name.
111  
112 my $regex = $config->get_regex( $section, $name );
113  
114 Note that this is very much like C<get()>, except that it will treat the value as a regex and strip out outer '/' symbols so that the result is suitable for inclusion in a regex. For example:
115  
116 my $regex = $config->get_regex( $section, $name );
117 if ( $variable =~ /$regex/ )
118 {
119 ...
120 }
121  
122 =cut
123  
124 sub get_regex
125 {
126 25 ▶ my ( $self, $section, $name ) = @_;
Subroutine get_regex: called CC=5 SCAR=16.1
Pod documented
127  
128 25 my $value = $self->get( $section, $name );
129 return undef
130 25 ▶ if !defined( $value ) || $value eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq "" 18 7 25
Condition100%3/3
not defined $value or $value eq ""
l!l&&r!l&&!r
1717
MC/DC100%2/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
131  
132 7 my ( $regex ) = $value =~ /^\s*\/(.*?)\/\s*$/;
133 7 ▶ croak "The key $name in the section $section is not a regex, use /.../ to delimit your expression"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $regex 1 6 7
134 if !defined( $regex );
135 6 ▶ croak "The key $name in the section $section does not specify a valid regex, it has unescaped '/' delimiters inside it"
Branch: 1 branch
BranchTrueFalseTotal
if $regex =~ m[(?<!\\)/] 1 5 6
136 if $regex =~ /(?<!\\)\//;
137  
138 5 return $regex;
139 }
140  
141  
142 =head1 BUGS
143  
144 Please report any bugs or feature requests through the web interface at
145 L<https://github.com/guillaumeaubert/App-GitHooks/issues/new>.
146 I will be notified, and then you'll automatically be notified of progress on
147 your bug as I make changes.
148  
149  
150 =head1 SUPPORT
151  
152 You can find documentation for this module with the perldoc command.
153  
154 perldoc App::GitHooks::Config
155  
156  
157 You can also look for information at:
158  
159 =over
160  
161 =item * GitHub's request tracker
162  
163 L<https://github.com/guillaumeaubert/App-GitHooks/issues>
164  
165 =item * AnnoCPAN: Annotated CPAN documentation
166  
167 L<http://annocpan.org/dist/app-githooks>
168  
169 =item * CPAN Ratings
170  
171 L<http://cpanratings.perl.org/d/app-githooks>
172  
173 =item * MetaCPAN
174  
175 L<https://metacpan.org/release/App-GitHooks>
176  
177 =back
178  
179  
180 =head1 AUTHOR
181  
182 L<Guillaume Aubert|https://metacpan.org/author/AUBERTG>,
183 C<< <aubertg at cpan.org> >>.
184  
185  
186 =head1 COPYRIGHT & LICENSE
187  
188 Copyright 2013-2015 Guillaume Aubert.
189  
190 This program is free software: you can redistribute it and/or modify it under
191 the terms of the GNU General Public License version 3 as published by the Free
192 Software Foundation.
193  
194 This program is distributed in the hope that it will be useful, but WITHOUT ANY
195 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
196 PARTICULAR PURPOSE. See the GNU General Public License for more details.
197  
198 You should have received a copy of the GNU General Public License along with
199 this program. If not, see http://www.gnu.org/licenses/
200  
201 =cut
202  
203 30 1;
« blib/lib/App/GitHooks/Hook/PreCommit.pm ↑ Summary /home/cpancover/.cpan/build/App-GitHooks-v1.7.3-0/bin/githooks »