blib/lib/YAML/AppConfig.pm

filter: Statementstmt 94.9% 132 / 139 Branchbran 88.4% 46 / 52 Conditioncond 66.6% 12 / 18 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 96.1% 25 / 26 Podpod 100% 8 / 8 totaltotal 89.4% 228 / 255 CC 45 SCAR 15.7
CC
45
Cov
91%
_resolve_scalar
9.0
_resolve_refs
7.8
_load_yaml_class
7.0
CRAP 4.8
↑ Summary
1 package YAML::AppConfig;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Storable qw(dclone); # For Deep Copy
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 ####################
8 # Global Variables
9 ####################
10 6 our $VERSION = '0.19';
11 6 our @YAML_PREFS = qw(YAML::Syck YAML);
12  
13 #########################
14 # Class Methods: Public
15 #########################
16 23 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
17 23 my ($class, %args) = @_;
18 23 ▶ my $self = bless( \%args, ref($class) || $class );
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
1220
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
19  
20 # Load a YAML parser.
21 23 $self->{yaml_class} = $self->_load_yaml_class();
22  
23 # Load config from file, string, or object.
24 23 ▶ if ( exists $self->{file} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->{'file'}) { } 9 14 23
elsif (exists $self->{'string'}) { } 12 2 14
elsif (exists $self->{'object'}) { } 1 1 2
25 9 my $load_file = eval "\\&$self->{yaml_class}::LoadFile";
26 9 $self->{config} = $load_file->( $self->{file} );
27 } elsif ( exists $self->{string} ) {
28 12 my $load = eval "\\&$self->{yaml_class}::Load";
29 12 $self->{config} = $load->( $self->{string} );
30 } elsif ( exists $self->{object} ) {
31 1 $self->{config} = dclone( $self->{object}->{config} );
32 } else {
33 1 $self->{config} = {};
34 }
35  
36 # Initialize internal state
37 23 $self->_install_accessors(); # Install convenience accessors.
38 23 $self->{seen} = {}; # For finding circular references.
39 23 $self->{scope_stack} = []; # For implementing dynamic variables.
40  
41 23 return $self;
42 }
43  
44 #############################
45 # Instance Methods: Public
46 #############################
47 653 ▶sub config {
Subroutine config: called CC=1 SCAR=0.0
Pod documented
48 653 my $self = shift;
49 653 return $self->{config};
50 }
51  
52 25 ▶sub config_keys {
Subroutine config_keys: called CC=1 SCAR=0.0
Pod documented
53 25 my $self = shift;
54 25 return sort keys %{$self->config};
55 }
56  
57 106 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
58 106 my $self = shift;
59 106 $self->{seen} = {}; # Don't know if we exited cleanly, so clean up.
60 106 return $self->_get(@_);
61 }
62  
63 # Inner get so we can clear the seen hash above. Listed here for readability.
64 242 ▶sub _get {
Subroutine _get: called CC=5 SCAR=16.1
65 242 my ( $self, $key, $no_resolve ) = @_;
66 242 ▶ return unless $self->_scope_has($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_scope_has($key) 0 242 242
67 242 ▶ return $self->config->{$key} if $self->{no_resolve} or $no_resolve;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"no_resolve"} or $no_resolve 13 229 242
Condition100%3/3
$self->{"no_resolve"} or $no_resolve
l!l&&r!l&&!r
58229
MC/DC100%2/2
$self->{"no_resolve"} or $no_resolve
$self->{"no_resolve"} $no_resolve
Truth table100%3/3
A or B
A= $self->{"no_resolve"}B= $no_resolve
ABresult
1X1
011
000
68 229 ▶ croak "Circular reference in $key." if exists $self->{seen}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"seen"}{$key} 11 218 229
69 218 $self->{seen}->{$key} = 1;
70 218 my $value = $self->_resolve_refs($self->_get_from_scope($key));
71 187 delete $self->{seen}->{$key};
72 187 return $value;
73 }
74  
75 9 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
76 9 my ($self, $key, $value) = @_;
77 9 return $self->config->{$key} = $value;
78 }
79  
80 1 ▶sub merge {
Subroutine merge: called CC=2 SCAR=6.9
Pod documented
81 1 my ( $self, %args ) = @_;
82 1 my $other_conf = $self->new( %args );
83 1 for my $key ( $other_conf->config_keys ) {
84 2 $self->set( $key, $other_conf->get( $key, 'no vars' ) );
85 }
86 }
87  
88 4 ▶sub resolve {
Subroutine resolve: called CC=1 SCAR=0.0
Pod documented
89 4 my ( $self, $thing ) = @_;
90 4 $self->{seen} = {}; # Can't be sure this is empty, could've croaked.
91 4 return $self->_resolve_refs($thing);
92 }
93  
94 2 ▶sub dump {
Subroutine dump: called CC=4 SCAR=13.9
Pod documented
95 2 my ( $self, $file ) = @_;
96 2 ▶ my $func = eval "\\&$self->{yaml_class}::" . ($file ? 'DumpFile' : 'Dump');
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 1 1 2
97 2 ▶ die "Could not find $func: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
98 2 ▶ $func->($file ? ($file) : (), $self->config);
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 1 1 2
99 }
100  
101 ##############################
102 # Instance Methods: Private
103 ##############################
104  
105 # void _resolve_refs(Scalar $value)
106 #
107 # Recurses on $value until a non-reference scalar is found, in which case we
108 # defer to _resolve_scalar. In this manner things like hashes and arrays are
109 # traversed depth-first.
110 358 ▶sub _resolve_refs {
Subroutine _resolve_refs: called CC=7 SCAR=20.5
111 358 my ( $self, $value ) = @_;
112 358 ▶ if ( not ref $value ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not ref $value) { } 283 75 358
elsif (ref $value eq 'HASH') { } 38 37 75
elsif (ref $value eq 'ARRAY') { } 37 0 37
elsif (ref $value eq 'SCALAR') { } 0 0 0
113 283 $value = $self->_resolve_scalar($value);
114 }
115 elsif (ref $value eq 'HASH' ) {
116 38 $value = dclone($value);
117 38 my @hidden = $self->_push_scope($value);
118 38 for my $key ( keys %$value ) {
119 66 $value->{$key} = $self->_resolve_refs( $value->{$key} );
120 }
121 29 $self->_pop_scope(@hidden);
122 29 return $value;
123 }
124 elsif (ref $value eq 'ARRAY' ) {
125 37 $value = dclone($value);
126 37 for my $item (@$value) {
127 70 $item = $self->_resolve_refs( $item );
128 }
129 }
130 elsif (ref $value eq 'SCALAR' ) {
131 0 $value = $self->_resolve_scalar($$value);
132 }
133 else {
134 0 my ($class, $type) = map ref, ($self, $value);
135 0 die "${class}::_resolve_refs() can't handle $type references.";
136 }
137  
138 285 return $value;
139 }
140  
141 # List _push_scope(HashRef scope)
142 #
143 # Pushes a new scope onto the stack. Variables in this scope are hidden from
144 # the seen stack. This allows us to reference variables in the current scope
145 # even if they have the same name as a variable higher up in chain. The
146 # hidden variables are returned.
147 38 ▶sub _push_scope {
Subroutine _push_scope: called CC=3 SCAR=11.0
148 38 my ( $self, $scope ) = @_;
149 38 unshift @{ $self->{scope_stack} }, dclone($scope);
150 38 my @hidden;
151 38 for my $key ( keys %$scope ) {
152 68 ▶ if ( exists $self->{seen}->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"seen"}{$key}) 3 65 68
153 3 push @hidden, $key;
154 3 delete $self->{seen}->{$key};
155 }
156 }
157 38 return @hidden;
158 }
159  
160 # void _pop_scope(@hidden)
161 #
162 # Removes the currently active scope from the stack and unhides any variables
163 # passed in via @hidden, which is usually returned from _push_scope.
164 29 ▶sub _pop_scope {
Subroutine _pop_scope: called CC=2 SCAR=6.9
165 29 my ( $self, @hidden ) = @_;
166 29 shift @{$self->{scope_stack}};
167 29 for my $key ( @hidden ) {
168 1 $self->{seen}->{$key} = 1; # Unhide
169 }
170 }
171  
172 # void _resolve_scalar(String $value)
173 #
174 # This function should only be called with strings (or numbers), not
175 # references. $value is treated as a string and is searched for $foo type
176 # variables, which are then resolved. The new string with variables resolved
177 # is returned.
178 283 ▶sub _resolve_scalar {
Subroutine _resolve_scalar: called CC=9 SCAR=22.0
179 283 my ( $self, $value ) = @_;
180 283 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 279 283
181 279 my @parts = grep length, # Empty strings are useless, discard them
182 split /((?<!\\)\$(?:{\w+}|\w+))/, $value;
183 279 for my $part (@parts) {
184 364 ▶ if ( $part =~ /^(?<!\\)\$(?:{(\w+)}|(\w+))$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part =~ /^(?<!\\)\$(?:{(\w+)}|(\w+))$/) { } 143 221 364
185 143 ▶ my $name = $1 || $2;
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
41390
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
186 143 ▶ $part = $self->_get($name) if $self->_scope_has($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_scope_has($name) 136 7 143
187 } else {
188 # Unescape slashes. Example: \\\$foo -> \\$foo, ditto with ${foo}
189 221 $part =~ s/(\\*)\\(\$(?:{(\w+)}|(\w+)))/$1$2/g;
190 }
191 }
192 247 ▶ return $parts[0] if @parts == 1 and ref $parts[0]; # Preserve references
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 and ref $parts[0] 8 239 247
Condition100%3/3
@parts == 1 and ref $parts[0]
!ll&&!rl&&r
571828
MC/DC100%2/2
@parts == 1 and ref $parts[0]
@parts == 1 ref $parts[0]
Truth table100%3/3
A and B
A= @parts == 1B= ref $parts[0]
ABresult
0X0
100
111
193 239 ▶ return join "", map { defined($_) ? $_ : "" } @parts;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 322 2 324
194 }
195  
196 # HashRef _scope(void)
197 #
198 # Returns the current scope. There is always a currenty defined scope, even
199 # if it's just the global scope.
200 0 ▶sub _scope {
Subroutine _scope: not called CC=2 SCAR=17.9
201 0 my $self = shift;
202 0 ▶ return $self->{scope_stack}->[0] || $self->config;
Condition0%0/3
$self->{'scope_stack'}[0] || $self->config
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'scope_stack'}[0] || $self->config
$self->{'scope_stack'}[0] $self->config
Truth table0%0/3
A || B
A= $self->{'scope_stack'}[0]B= $self->config
ABresult
1X1
011
000
203 }
204  
205 # List _scope_stack(void)
206 #
207 # Returns the list of currently active scopes. The list is ordered from inner
208 # most scope to outer most scope. The global scope is always the last scope
209 # in the list.
210 603 ▶sub _scope_stack {
Subroutine _scope_stack: called CC=1 SCAR=0.0
211 603 my $self = shift;
212 603 return ( @{ $self->{scope_stack} }, $self->config );
213 }
214  
215 # Boolean _get_from_scope(String key)
216 #
217 # This method returns true if the key is in any scope enclosing the current
218 # scope or in the current scope. False otherwise.
219 385 ▶sub _scope_has {
Subroutine _scope_has: called CC=3 SCAR=11.0
220 385 my ( $self, $name ) = @_;
221 385 for my $scope ( $self->_scope_stack ) {
222 635 ▶ return 1 if exists $scope->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scope->{$name} 378 257 635
223 }
224 7 return 0;
225 }
226  
227 # Scalar _get_from_scope(String key)
228 #
229 # Given a key this method returns its value as it's defined in the inner most
230 # enclosing scope containing the key. That is to say, this method implements
231 # the dyanmic scoping lookup for key.
232 218 ▶sub _get_from_scope {
Subroutine _get_from_scope: called CC=3 SCAR=11.1
233 218 my ( $self, $key ) = @_;
234 218 for my $scope ( $self->_scope_stack ) {
235 337 ▶ return $scope->{$key} if exists $scope->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scope->{$key} 218 119 337
236 }
237 0 return undef;
238 }
239  
240 # void _load_yaml_class
241 #
242 # Attempts to load a YAML class that can parse YAML for us. We prefer the
243 # yaml_class attribute over everything, then fall back to a previously loaded
244 # YAML parser from @YAML_PREFS, and failing that try to load a parser from
245 # @YAML_PREFS.
246 23 ▶sub _load_yaml_class {
Subroutine _load_yaml_class: called CC=7 SCAR=19.5
247 23 my $self = shift;
248  
249 # Always use what we were given.
250 23 ▶ if (defined $self->{yaml_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"yaml_class"}) 2 21 23
251 2 eval "require $self->{yaml_class}; 0;";
252 2 ▶ croak "$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
253 2 return $self->{yaml_class};
254 }
255  
256 # Use what's already been loaded.
257 21 for my $module (@YAML_PREFS) {
258 42 my $filename = $module . ".pm";
259 42 $filename =~ s{::}{/};
260 42 ▶ return $self->{yaml_class} = $module if exists $INC{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{$filename} 16 26 42
261 }
262  
263 # Finally, try and load something.
264 5 for my $module (@YAML_PREFS) {
265 10 eval "require $module; 0;";
266 10 ▶ return $self->{yaml_class} = $module unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 5 5 10
267 }
268  
269 0 die "Could not load: " . join(" or ", @YAML_PREFS);
270 }
271  
272 # void _install_accessors(void)
273 #
274 # Installs convienence methods for getting and setting configuration values.
275 # These methods are just curryed versions of get() and set().
276 23 ▶sub _install_accessors {
Subroutine _install_accessors: called CC=5 SCAR=16.1
277 23 my $self = shift;
278 23 for my $key ($self->config_keys) {
279 115 ▶ next unless $key and $key =~ /^[a-zA-Z_]\w*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $key =~ /^[a-zA-Z_]\w*$/ 8 107 115
Condition66%2/3
$key and $key =~ /^[a-zA-Z_]\w*$/
!ll&&!rl&&r
08107
MC/DC50%1/2
$key and $key =~ /^[a-zA-Z_]\w*$/
$key $key =~ /^[a-zA-Z_]\w*$/
Truth table66%2/3
A and B
A= $keyB= $key =~ /^[a-zA-Z_]\w*$/
ABresult
0X0
100
111
280 107 for my $method (qw(get set)) {
281 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
282 6 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
283 214 my $method_name = ref($self) . "::${method}_$key";
284 214 ▶ *{$method_name} = sub { $_[0]->$method($key, $_[1]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
285 }
286 }
287 }
288  
289 6 1;
290 __END__
↑ Summary