blib/lib/App/Open/Config.pm

filter: Statementstmt 88.9% 121 / 136 Branchbran 76.6% 23 / 30 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 34 / 34 Podpod 100% 8 / 8 totaltotal 89.2% 200 / 224 CC 24 SCAR 14.1
CC
24
Cov
87%
load_backends
11.5
load_backend
6.3
load_config
5.0
CRAP 4.1
« blib/lib/App/Open/Backend/YAML.pm ↑ Summary blib/lib/App/Open/Backend/MailCap.pm »
1 #===============================================================================
2 #
3 # FILE: Config.pm
4 #
5 # DESCRIPTION: App:;Open::Config - basic configuration interface to App::Open
6 #
7 # FILES: ---
8 # BUGS: ---
9 # NOTES: ---
10 # AUTHOR: Erik Hollensbe (), <erik@hollensbe.org>
11 # COMPANY:
12 # VERSION: 1.0
13 # CREATED: 06/02/2008 02:27:27 AM PDT
14 # REVISION: ---
15 #===============================================================================
16  
17 package App::Open::Config;
18  
19 =head2 METHODS
20  
21 =over 4
22  
23 =cut
24  
25 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 12 ▶use YAML::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 $YAML::Syck::ImplicitTyping = 1;
30  
31 =item new($config_file)
32  
33 Constructor, optionally takes the name of a config file; calls load_config()
34 automatically.
35  
36 =cut
37  
38 31 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
39 31 my ( $class, $config_file ) = @_;
40  
41 31 ▶ my $self = bless { config_file => ( $config_file || "" ) }, $class;
Condition100%2/2
$config_file || ''
l!l
265
MC/DC100%1/1
$config_file || ''
$config_file
Truth table100%2/2
A
A= $config_file
Aresult
11
00
42  
43 31 $self->load_config;
44  
45 29 return $self;
46 }
47  
48 =item load_config()
49  
50 Loads the configuration (or resets it). If there is trouble reading the
51 configuration, it will supply a default empty configuration.
52  
53 This call will die if the configuration is available, not undefined and does
54 not evaluate to a hash.
55  
56 =cut
57  
58 31 ▶sub load_config {
Subroutine load_config: called CC=5 SCAR=16.1
Pod documented
59 31 my $self = shift;
60  
61 31 my $config;
62  
63 31 eval { $config = YAML::Syck::LoadFile( $self->{config_file} ) };
64  
65 # load a default configuration if Syck fails us
66 31 ▶ $self->{config} = $@ ? {} : ( $config || {} );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 6 25 31
Condition100%2/2
$config or {}
l!l
241
MC/DC100%1/1
$config or {}
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
67  
68 31 ▶ if ( !ref( $self->{config} ) || ref( $self->{config} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self->{"config"} or ref $self->{"config"} ne "HASH") 2 29 31
Condition100%3/3
not ref $self->{"config"} or ref $self->{"config"} ne "HASH"
l!l&&r!l&&!r
1129
MC/DC100%2/2
not ref $self->{"config"} or ref $self->{"config"} ne "HASH"
not ref $self->{"config"} ref $self->{"config"} ne "HASH"
Truth table100%3/3
not(A) or B
A= not ref $self->{"config"}B= ref $self->{"config"} ne "HASH"
ABresult
1X1
011
000
69 2 die "INVALID_CONFIGURATION";
70 }
71  
72 29 return;
73 }
74  
75 =item load_backends(@backends)
76  
77 A frontend to load_backend(). Takes a list of backends to be processed in priority.
78  
79 =cut
80  
81 27 ▶sub load_backends {
Subroutine load_backends: called CC=11 SCAR=24.4
Pod documented
82 27 my ( $self, @backends ) = @_;
83  
84 #
85 # This is a lot more complex than I'd like, but it keeps the end-user
86 # configuration tolerable.
87 #
88 # Basically, if the backend value is a hash, it passes the key to
89 # load_backend() which will ferret the arguments out. The upside of this is
90 # that it's trivial to configure one backend, but multiple backends cannot
91 # guarantee ordering. The value associated with this key must be an array,
92 # and will be used as the arguments for the backend.
93 #
94 # If the value is an array, it expects each array element to be a hash,
95 # with the keys `name` and `args`, which represent the backend name and
96 # arguments respectively. The `args` must be an array. The whole top-level
97 # hash for the backend (the array element) is passed to load_backend().
98 #
99  
100 27 ▶ if ( exists( $self->config->{backend} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->config->{"backend"} and defined $self->config->{"backend"}) 25 2 27
Condition66%2/3
exists $self->config->{"backend"} and defined $self->config->{"backend"}
!ll&&!rl&&r
2025
MC/DC50%1/2
exists $self->config->{"backend"} and defined $self->config->{"backend"}
exists $self->config->{"backend"} defined $self->config->{"backend"}
Truth table66%2/3
A and B
A= exists $self->config->{"backend"}B= defined $self->config->{"backend"}
ABresult
0X0
100
111
101 and defined( $self->config->{backend} ) )
102 {
103 25 ▶ if (ref( $self->config->{backend} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->config->{'backend'}) { } 25 0 25
104 25 ▶ if (ref($self->config->{backend}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->config->{'backend'} eq 'HASH') { } 24 1 25
elsif (ref $self->config->{'backend'} eq 'ARRAY') { } 1 0 1
105 24 foreach my $backend ( keys %{ $self->config->{backend} } ) {
106 25 $self->load_backend($backend);
107 }
108 } elsif (ref($self->config->{backend}) eq 'ARRAY') {
109 1 foreach my $backend (@{$self->config->{backend}}) {
110 2 ▶ if (ref($backend) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $backend eq "HASH") 2 0 2
111 2 $self->load_backend($backend);
112 }
113 }
114 }
115 }
116 else {
117 0 $self->load_backend( $self->config->{backend} );
118 }
119 }
120  
121 27 ▶ if (@backends) {
Branch: 1 branch
BranchTrueFalseTotal
if (@backends) 5 22 27
122 5 foreach my $backend (@backends) {
123 6 $self->load_backend($backend);
124 }
125 }
126  
127 27 return;
128 }
129  
130 =item load_backend($backend)
131  
132 Gets the parameters for the backend, name and arguments. Requires the module
133 for the backend via require_backend() and on success, constructs an object from
134 that module with the supplied arguments and stores it in the backend list.
135  
136 The $backend argument can either be a hashref or string, this is detailed in
137 some comments in load_backends().
138  
139 If the backend supplied cannot be loaded, it will die with NO_BACKEND_FOUND.
140  
141 =cut
142  
143 35 ▶sub load_backend {
Subroutine load_backend: called CC=6 SCAR=18.3
Pod documented
144 35 my ( $self, $backend ) = @_;
145  
146 35 ▶ if (ref($backend) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $backend eq 'HASH') { } 2 33 35
elsif (not ref $backend) { } 33 0 33
147 2 ▶ if ($backend->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backend->{"name"}) 2 0 2
148 2 my $module = $self->require_backend($backend->{name});
149 2 ▶ if ($module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module) 2 0 2
150 2 my $obj = $module->new($backend->{args});
151 2 push @{ $self->backend_order }, $obj;
152 2 return $module;
153 }
154 }
155 } elsif (!ref($backend)) {
156 33 my $module = $self->require_backend($backend);
157 33 ▶ if ($module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module) 33 0 33
158 33 my $obj = $module->new( $self->config->{backend}{$backend} );
159 33 push @{ $self->backend_order }, $obj;
160 33 return $module;
161 }
162 }
163  
164 0 die "NO_BACKEND_FOUND $backend";
165 }
166  
167 =item require_backend($backend)
168  
169 Attempts to use the module that corresponds to the backend name. This will try
170 a couple of namespaces to load a backend:
171  
172 =over 4
173  
174 =item App::Open::Backend::
175  
176 =item "" (root namespace)
177  
178 =back
179  
180 On success, it will return the module name used. Otherwise, undef.
181  
182 =cut
183  
184 35 ▶sub require_backend {
Subroutine require_backend: called CC=3 SCAR=11.1
Pod documented
185 35 my ($self, $backend) = @_;
186  
187 35 foreach my $backend_try ( "App::Open::Backend::", "" ) {
188 44 my $module = "$backend_try$backend";
189  
190 0 ▶ eval "use $module";
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
191  
192 44 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 35 9 44
193 35 return $module;
194 }
195 }
196  
197 0 return undef;
198 }
199  
200 =item config()
201  
202 Convenience call to access the config hash.
203  
204 =cut
205  
206 171 ▶sub config { $_[0]->{config} }
Subroutine config: called CC=1 SCAR=0.0
Pod documented
207  
208 =item config_file()
209  
210 Convenience call to access the config filename.
211  
212 =cut
213  
214 2 ▶sub config_file { $_[0]->{config_file} }
Subroutine config_file: called CC=1 SCAR=0.0
Pod documented
215  
216 =item backend_order()
217  
218 Returns the lookup order of the various MIME backends as arrayref.
219  
220 In the instance that this does not already exist when it is called, a new,
221 empty arrayref will be created and returned.
222  
223 =cut
224  
225 60 ▶sub backend_order {
Subroutine backend_order: called CC=2 SCAR=6.9
Pod documented
226 60 my $self = shift;
227  
228 60 ▶ return $self->{backend_order} if ( $self->{backend_order} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"backend_order"} 36 24 60
229  
230 24 return $self->{backend_order} = [];
231 }
232  
233 =back
234  
235 =cut
236  
237 12 1;
« blib/lib/App/Open/Backend/YAML.pm ↑ Summary blib/lib/App/Open/Backend/MailCap.pm »