File Coverage

blib/lib/App/GitHooks/Config.pm
Criterion Covered Total %
statement 12 31 38.7
branch 0 14 0.0
condition 0 9 0.0
subroutine 4 7 57.1
pod 3 3 100.0
total 19 64 29.6


line stmt bran cond sub pod time code
1             package App::GitHooks::Config;
2              
3 23     23   19835 use strict;
  23         32  
  23         773  
4 23     23   93 use warnings;
  23         35  
  23         717  
5              
6             # External dependencies.
7 23     23   93 use Carp;
  23         30  
  23         1311  
8 23     23   784 use Config::Tiny;
  23         1305  
  23         7472  
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             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 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 0     0 1   my ( $class, %args ) = @_;
65 0           my $file = delete( $args{'file'} );
66 0           my $source = delete( $args{'source'} );
67              
68 0 0         my $self = defined( $file )
69             ? Config::Tiny->read( $file )
70             : Config::Tiny->new();
71              
72 0           bless( $self, $class );
73              
74             # Store meta-information for future reference.
75 0           $self->{'__source'} = $source;
76 0           $self->{'__path'} = $file;
77              
78 0           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 configuration files are organized by sections,
89             with the main (default) section being '_'.
90              
91             =cut
92              
93             sub get
94             {
95 0     0 1   my ( $self, $section, $name ) = @_;
96              
97 0 0 0       croak 'A section name is required as first argument'
98             if !defined( $section ) || ( $section eq '' );
99 0 0 0       croak 'A key name is required as second argument'
100             if !defined( $name ) || ( $name eq '' );
101              
102 0 0         return defined( $self->{ $section } )
103             ? $self->{ $section }->{ $name }
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, 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 0     0 1   my ( $self, $section, $name ) = @_;
127              
128 0           my $value = $self->get( $section, $name );
129             return undef
130 0 0 0       if !defined( $value ) || $value eq '';
131              
132 0           my ( $regex ) = $value =~ /^\s*\/(.*?)\/\s*$/;
133 0 0         croak "The key $name in the section $section is not a regex, use /.../ to delimit your expression"
134             if !defined( $regex );
135 0 0         croak "The key $name in the section $section does not specify a valid regex, it has unescaped '/' delimiters inside it"
136             if $regex =~ /(?
137              
138 0           return $regex;
139             }
140              
141              
142             =head1 BUGS
143              
144             Please report any bugs or feature requests through the web interface at
145             L.
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
164              
165             =item * AnnoCPAN: Annotated CPAN documentation
166              
167             L
168              
169             =item * CPAN Ratings
170              
171             L
172              
173             =item * MetaCPAN
174              
175             L
176              
177             =back
178              
179              
180             =head1 AUTHOR
181              
182             L,
183             C<< >>.
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             1;