blib/lib/Config/Entities.pm

filter: Statementstmt 95.0% 135 / 142 Branchbran 77.5% 62 / 80 Conditioncond 76.1% 16 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 21 / 21 Podpod 100% 4 / 4 totaltotal 87.2% 246 / 282 CC 57 SCAR 19.1
CC
57
Cov
88%
_init
10.0
fill
9.1
__ANON__
9.1
CRAP 6.7
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Config::Entities;
5 1 $Config::Entities::VERSION = '1.07';
6 # ABSTRACT: An multi-level overridable perl based configuration module
7 # PODNAME: Config::Entities
8  
9 1 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 my $logger = Log::Any->get_logger();
16  
17 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 13 my ( $class, @args ) = @_;
19 13 return bless( {}, $class )->_init(@args);
20 }
21  
22 2 ▶sub as_hashref {
Subroutine as_hashref: called CC=1 SCAR=0.0
Pod documented
23 2 return _copy(@_);
24 }
25  
26 38 ▶sub _copy {
Subroutine _copy: called CC=6 SCAR=21.4
27 38 my ($value) = @_;
28  
29 38 my $ref = ref($value);
30 38 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 14 24 38
31 14 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 14 14
elsif ($ref eq 'HASH' or $value->isa('Config::Entities')) { } 14 0 14
elsif ($ref eq 'SCALAR') { } 0 0 0
32 0 return [ map { _copy($_) } @$value ];
33 }
34 ▶ elsif ( $ref eq 'HASH' || $value->isa('Config::Entities') ) {
Condition66%2/3
$ref eq "HASH" or $value->isa("Config::Entities")
l!l&&r!l&&!r
1220
MC/DC0%0/2
$ref eq "HASH" or $value->isa("Config::Entities")
$ref eq "HASH" $value->isa("Config::Entities")
Truth table66%2/3
A or B
A= $ref eq "HASH"B= $value->isa("Config::Entities")
ABresult
1X1
011
000
35 14 return { map { $_ => _copy( $value->{$_} ) } keys(%$value) };
36 }
37 elsif ( $ref eq 'SCALAR' ) {
38 0 return $value;
39 }
40 else {
41 0 croak("unsupported type '$ref'");
42 }
43 }
44 else {
45 24 return $value;
46 }
47 }
48  
49 13 ▶sub _add_properties {
Subroutine _add_properties: called CC=2 SCAR=6.9
50 13 my ( $self, $properties, $more_properties ) = @_;
51  
52 13 foreach my $key ( keys( %{$more_properties} ) ) {
53 20 $properties->{$key} = $more_properties->{$key};
54 }
55 }
56  
57 5 ▶sub fill {
Subroutine fill: called CC=9 SCAR=22.1
Pod documented
58 5 my ( $self, $coordinate, $hashref, %options ) = @_;
59  
60 5 my @entity = $self->get_entity( $coordinate, %options );
61 5 foreach my $key ( keys(%$hashref) ) {
62 11 ▶ if ( ref( $entity[0] ) eq 'HASH' && exists( $entity[0]->{$key} ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $entity[0] eq 'HASH' and exists $entity[0]{$key}) { } 5 6 11
elsif ($hashref->{$key} and $hashref->{$key} eq 'Config::Entities::entity') { } 1 5 6
elsif ($options{'ancestry'}) { } 5 0 5
Condition100%3/3
ref $entity[0] eq "HASH" and exists $entity[0]{$key}
!ll&&!rl&&r
425
MC/DC100%2/2
ref $entity[0] eq "HASH" and exists $entity[0]{$key}
ref $entity[0] eq "HASH" exists $entity[0]{$key}
Truth table100%3/3
A and B
A= ref $entity[0] eq "HASH"B= exists $entity[0]{$key}
ABresult
0X0
100
111
63 5 $hashref->{$key} = $entity[0]->{$key};
64 }
65 ▶ elsif ( $hashref->{$key} && $hashref->{$key} eq 'Config::Entities::entity' ) {
Condition66%2/3
$hashref->{$key} and $hashref->{$key} eq "Config::Entities::entity"
!ll&&!rl&&r
501
MC/DC50%1/2
$hashref->{$key} and $hashref->{$key} eq "Config::Entities::entity"
$hashref->{$key} $hashref->{$key} eq "Config::Entities::entity"
Truth table66%2/3
A and B
A= $hashref->{$key}B= $hashref->{$key} eq "Config::Entities::entity"
ABresult
0X0
100
111
66 1 $hashref->{$key} = $entity[0];
67 }
68 elsif ( $options{ancestry} ) {
69 5 ▶ for ( my $index = 1; $index < scalar(@entity); $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < scalar @entity) 14 0 14
70 14 ▶ if ( defined( $entity[$index]->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $entity[$index]{$key}) 5 9 14
71 5 $hashref->{$key} = $entity[$index]->{$key};
72 5 last;
73 }
74 }
75 }
76 }
77  
78 5 return $hashref;
79 }
80  
81 9 ▶sub get_entity {
Subroutine get_entity: called CC=5 SCAR=16.2
Pod documented
82 9 my ( $self, $coordinate, %options ) = @_;
83  
84 9 my @result = ($self);
85 9 ▶ if ($coordinate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coordinate) 9 0 9
86 9 foreach my $coordinate_part ( split( /\./, $coordinate ) ) {
87 25 my $child = $result[0]->{$coordinate_part};
88 25 ▶ return if ( !defined($child) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child 0 25 25
89 25 unshift( @result, $child );
90 }
91 }
92 9 ▶ return $options{ancestry} ? @result : shift(@result);
Branch: 1 branch
BranchTrueFalseTotal
$options{'ancestry'} ? : 5 4 9
93 }
94  
95 13 ▶sub _init {
Subroutine _init: called CC=10 SCAR=23.0
96 13 my ( $self, @args ) = @_;
97  
98 # if last arg is a hash, it is an options hash
99 13 ▶ my $options =
Branch: 1 branch
BranchTrueFalseTotal
ref $args[$#args] eq 'HASH' ? : 11 2 13
100 ref( $args[$#args] ) eq 'HASH'
101 ? pop(@args)
102 : {};
103  
104 # all other args are entities roots
105 13 my @entities_roots = @args;
106  
107 13 my $properties = {};
108 13 ▶ if ( $options->{properties_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"properties_file"}) 6 7 13
109  
110 # merge in properties from files
111 my @properties_files =
112 ref( $options->{properties_file} ) eq 'ARRAY'
113 1 ? @{ $options->{properties_file} }
114 6 ▶ : ( $options->{properties_file} );
Branch: 1 branch
BranchTrueFalseTotal
ref $options->{'properties_file'} eq 'ARRAY' ? : 1 5 6
115  
116 6 foreach my $properties_file (@properties_files) {
117 7 $self->_add_properties( $properties, do($properties_file) );
118 }
119 }
120 13 ▶ if ( $options->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"properties"}) 6 7 13
121  
122 # merge in direct properties
123 6 $self->_add_properties( $properties, $options->{properties} );
124 }
125  
126 13 ▶ if ( $options->{entity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"entity"}) 4 9 13
127 4 foreach my $key ( keys( %{ $options->{entity} } ) ) {
128 7 _merge( $self, $key, $options->{entity}{$key} );
129 }
130 }
131  
132 13 ▶ if ( scalar(@entities_roots) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entities_roots) 8 5 13
133 find(
134 52 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
135 52 ▶ if ( $_ =~ /^(.*)\.pmc?$/ && -f $File::Find::name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^(.*)\.pmc?$/ and -f $File::Find::name) 32 20 52
Condition66%2/3
$_ =~ /^(.*)\.pmc?$/ and -f $File::Find::name
!ll&&!rl&&r
20032
MC/DC50%1/2
$_ =~ /^(.*)\.pmc?$/ and -f $File::Find::name
$_ =~ /^(.*)\.pmc?$/ -f $File::Find::name
Truth table66%2/3
A and B
A= $_ =~ /^(.*)\.pmc?$/B= -f $File::Find::name
ABresult
0X0
100
111
136 32 my $key = $1;
137  
138 32 my $hashref = $self;
139 32 my @directories = File::Spec->splitdir(
140 substr( $File::Find::dir, length($File::Find::topdir) ) );
141 32 ▶ if ( scalar(@directories) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @directories) 16 16 32
142 16 ▶ shift(@directories) while ( !$directories[0] );
Branch: 1 branch
BranchTrueFalseTotal
$directories[0] or do { shift @directories } 16 16 32
143 16 foreach my $dir (@directories) {
144 24 ▶ if ( !defined( $hashref->{$dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hashref->{$dir}) 4 20 24
145 4 $hashref->{$dir} = {};
146 }
147 24 $hashref = $hashref->{$dir};
148 }
149 }
150  
151 32 my $entity;
152 {
153 # export %properties to the entity file
154 32 local $Config::Entities::properties = $properties;
155 ## no critic (ProhibitNoStrict)
156 1 ▶ no strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
157 32 ▶ local %properties = $properties ? %$properties : ();
Branch: 1 branch
BranchTrueFalseTotal
$properties ? : 32 0 32
158 32 $entity = do($File::Find::name);
159 ## use critic
160 }
161 32 ▶ $logger->warn( 'unable to compile ', $File::Find::name, ': ', $@, "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 32 32
162 if ($@);
163 32 _merge( $hashref, $key, $entity );
164 }
165 },
166 8 map { Cwd::abs_path($_) } @entities_roots
167 );
168 }
169  
170 13 &$_($self) foreach $self->_inherit( undef, $self );
171  
172 13 return $self;
173 }
174  
175 134 ▶sub _inherit {
Subroutine _inherit: called CC=7 SCAR=19.5
176 134 my ( $self, $parent, $child ) = @_;
177  
178 134 my @after_inherit = ();
179 134 ▶ if ($child) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child) 123 11 134
180 123 my $ref = ref($child);
181 123 ▶ if ( $ref eq 'HASH' || $ref eq 'Config::Entities' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH" or $ref eq "Config::Entities") 54 69 123
Condition100%3/3
$ref eq "HASH" or $ref eq "Config::Entities"
l!l&&r!l&&!r
411369
MC/DC100%2/2
$ref eq "HASH" or $ref eq "Config::Entities"
$ref eq "HASH" $ref eq "Config::Entities"
Truth table100%3/3
A or B
A= $ref eq "HASH"B= $ref eq "Config::Entities"
ABresult
1X1
011
000
182 54 ▶ if ( $parent && $child->{'Config::Entities::inherit'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $child->{"Config::Entities::inherit"}) 6 48 54
Condition100%3/3
$parent and $child->{"Config::Entities::inherit"}
!ll&&!rl&&r
13356
MC/DC100%2/2
$parent and $child->{"Config::Entities::inherit"}
$parent $child->{"Config::Entities::inherit"}
Truth table100%3/3
A and B
A= $parentB= $child->{"Config::Entities::inherit"}
ABresult
0X0
100
111
183 push(
184 @after_inherit,
185 $self->_inherit_each(
186 6 delete( $child->{'Config::Entities::inherit'} ),
187 $parent, $child
188 )
189 );
190 }
191 54 push( @after_inherit, $self->_inherit( $child, $child->{$_} ) ) foreach keys(%$child);
192 }
193 }
194 134 return @after_inherit;
195 }
196  
197 6 ▶sub _inherit_each {
Subroutine _inherit_each: called CC=7 SCAR=20.4
198 6 my ( $self, $inherit, $parent, $child ) = @_;
199  
200 6 my @after_inherit = ();
201 6 ▶ if ( ref($inherit) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inherit eq "ARRAY") 6 0 6
202 6 foreach my $spec (@$inherit) {
203 8 my $spec_ref = ref($spec);
204 8 ▶ if ($spec_ref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec_ref) { } 2 6 8
elsif (defined $parent->{$spec}) { } 6 0 6
205 2 ▶ if ( $spec_ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec_ref eq 'HASH') { } 2 0 2
206 2 push( @after_inherit, $self->_inherit_spec( $spec, $parent, $child ) );
207 }
208 else {
209 0 croak('invalid inherit');
210 }
211 }
212 elsif ( defined( $parent->{$spec} ) ) {
213 $child->{$spec} = $parent->{$spec}
214 6 ▶ unless ( defined( $child->{$spec} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->{$spec} 6 0 6
215 }
216 }
217 }
218 6 return @after_inherit;
219 }
220  
221 2 ▶sub _inherit_spec {
Subroutine _inherit_spec: called CC=6 SCAR=19.5
222 2 my ( $self, $spec, $parent, $child ) = @_;
223  
224 2 ▶ if ( $spec->{name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec->{'name'}) { } 1 1 2
elsif ($spec->{'coordinate'}) { } 1 0 1
225 1 ▶ my $as = $spec->{as} || $spec->{name};
Condition33%1/3
$spec->{'as'} || $spec->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$spec->{'as'} || $spec->{'name'}
$spec->{'as'} $spec->{'name'}
Truth table33%1/3
A || B
A= $spec->{'as'}B= $spec->{'name'}
ABresult
1X1
011
000
226 $child->{$as} = $parent->{ $spec->{name} }
227 1 ▶ unless ( defined( $child->{$as} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child->{$as} 1 0 1
228 }
229 elsif ( $spec->{coordinate} ) {
230 1 my $as = $spec->{as};
231 1 ▶ unless ($as) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($as) 0 1 1
232 0 $as = $spec->{coordinate};
233 0 $as =~ s/^.*\.//;
234 }
235  
236 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
237 1 my ($entities) = @_;
238 1 $child->{$as} = _copy( $entities->get_entity( $spec->{coordinate} ) );
239 1 ▶ _merge( $child, $as, $spec->{using} ) if ( $spec->{using} );
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"using"} 1 0 1
240 1 };
241 }
242 1 return;
243 }
244  
245 128 ▶sub _merge {
Subroutine _merge: called CC=4 SCAR=13.9
246 128 my ( $hashref, $key, $value ) = @_;
247  
248 128 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 43 85 128
249  
250 # transfer key/value pairs from hashref
251 # will merge rather than replace...
252 43 ▶ if ( !defined( $hashref->{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hashref->{$key}) 37 6 43
253 37 $hashref->{$key} = {};
254 }
255 43 $hashref = $hashref->{$key};
256  
257 43 ▶ while ( my ( $sub_key, $sub_value ) = each(%$value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$value) 88 43 131
258 88 _merge( $hashref, $sub_key, $sub_value );
259 }
260 }
261 else {
262 # anything not a hashref will replace
263 85 $hashref->{$key} = $value;
264 }
265 }
266  
267 1 1;
268  
269 __END__
↑ Summary