blib/lib/Config/Path.pm

filter: Statementstmt 95.7% 68 / 71 Branchbran 79.4% 27 / 34 Conditioncond 77.7% 14 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 9 / 9 Podpod 66.6% 2 / 3 totaltotal 87.0% 128 / 147 CC 26 SCAR 21.5
CC
26
Cov
89%
fetch
12.2
_build__config
11.2
BUILD
5.0
CRAP 8.6
↑ Summary
1 package Config::Path;
2 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 our $VERSION = '0.12';
5  
6 6 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10  
11 6 has '_config' => (
12 is => 'ro',
13 isa => 'HashRef',
14 lazy_build => 1,
15 clearer => 'reload'
16 );
17  
18  
19 has 'config_options' => (
20 is => 'ro',
21 isa => 'HashRef',
22 11 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 11 flatten_to_hash => 1,
24 use_ext => 1
25 } }
26 6 );
27  
28  
29 6 has 'directory' => (
30 is => 'ro',
31 isa => 'Str',
32 predicate => 'has_directory'
33 );
34  
35  
36 6 has 'files' => (
37 traits => [ qw(Array) ],
38 is => 'ro',
39 isa => 'ArrayRef',
40 predicate => 'has_files',
41 handles => {
42 add_file => 'push'
43 }
44 );
45  
46 6 has '_mask' => (
47 is => 'rw',
48 isa => 'HashRef',
49 predicate => 'has_mask',
50 clearer => 'clear_mask'
51 );
52  
53  
54 6 has 'convert_empty_to_undef' => (
55 is => 'ro',
56 isa => 'Bool',
57 default => 1
58 );
59  
60 11 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
61 11 my ($self) = @_;
62  
63 11 ▶ if($self->has_directory && $self->has_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_directory and $self->has_files) 1 10 11
Condition100%3/3
$self->has_directory and $self->has_files
!ll&&!rl&&r
551
MC/DC100%2/2
$self->has_directory and $self->has_files
$self->has_directory $self->has_files
Truth table100%3/3
A and B
A= $self->has_directoryB= $self->has_files
ABresult
0X0
100
111
64 1 die "directory and files are mutually exclusive, choose one"
65 }
66  
67 10 ▶ unless($self->has_directory || $self->has_files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_directory or $self->has_files) 1 9 10
Condition100%3/3
$self->has_directory or $self->has_files
l!l&&r!l&&!r
541
MC/DC100%2/2
$self->has_directory or $self->has_files
$self->has_directory $self->has_files
Truth table100%3/3
A or B
A= $self->has_directoryB= $self->has_files
ABresult
1X1
011
000
68 1 die "One of directory or files must be specified"
69 }
70 }
71  
72 9 ▶sub _build__config {
Subroutine _build__config: called CC=10 SCAR=24.1
73 9 my ($self) = @_;
74  
75 # This might be undef, but that's ok. We'll check later.
76 9 my $files = $self->files;
77  
78 # Check for a directory
79 9 ▶ if($self->has_directory) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_directory) 4 5 9
80 4 my $dir = $self->directory;
81  
82 4 ▶ unless(-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 4 4
83 0 die "Can't open directory: $dir";
84 }
85  
86 4 opendir(my $dh, $dir);
87 4 ▶ my @files = sort(map("$dir/$_", grep { $_ !~ /^\./ && -f "$dir/$_" } readdir($dh)));
Condition100%3/3
!($_ =~ /^\./) && -f "$dir/$_"
!ll&&!rl&&r
8210
MC/DC100%2/2
!($_ =~ /^\./) && -f "$dir/$_"
!($_ =~ /^\./) -f "$dir/$_"
Truth table100%3/3
not(A) && B
A= !($_ =~ /^\./)B= -f "$dir/$_"
ABresult
0X0
100
111
88 4 closedir($dh);
89  
90 4 $files = \@files;
91 }
92  
93 9 ▶ if(!defined($files) || scalar(@{ $files }) < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $files or scalar @{$files;} < 1) 0 9 9
Condition33%1/3
not defined $files or scalar @{$files;} < 1
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $files or scalar @{$files;} < 1
not defined $files scalar @{$files;} < 1
Truth table33%1/3
not(A) or B
A= not defined $filesB= scalar @{$files;} < 1
ABresult
1X1
011
000
94 0 warn "No files found.";
95 }
96  
97 9 my $anyconf = Config::Any->load_files({ %{ $self->config_options }, files => $files });
98  
99 9 my $config = ();
100 9 my $merge = Hash::Merge->new('RIGHT_PRECEDENT');
101 9 foreach my $file (@{ $files }) {
102 # Double check that it exists, as Config::Any might not have loaded it
103 20 ▶ next unless exists $anyconf->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $anyconf->{$file} 0 20 20
104 20 ▶ next unless defined $anyconf->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $anyconf->{$file} 0 20 20
105 20 $config = $merge->merge($config, $anyconf->{$file});
106 }
107 9 ▶ if(defined($config)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 9 0 9
108 9 return $config;
109 }
110  
111 0 return {};
112 }
113  
114  
115 27 ▶sub fetch {
Subroutine fetch: called CC=12 SCAR=25.0
Pod documented
116 27 my ($self, $path) = @_;
117  
118 # Check the mask first to see if the path we've been given has been
119 # overriden.
120 27 ▶ if($self->has_mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_mask) 2 25 27
121 # Use exists just in case they set the value to undef.
122 2 ▶ return $self->_mask->{$path} if exists($self->_mask->{$path});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_mask->{$path} 2 0 2
123 }
124  
125 25 my $conf = $self->_config;
126  
127 # you should be able to pass nothing and get a hashref back
128 25 ▶ if ( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) 25 0 25
129  
130 25 $path =~ s/^\///g; # Remove leading slashes, as they don't do anything
131 # and there's no reason to break over it.
132  
133 25 foreach my $piece (split(/\//, $path)) {
134 40 ▶ if(ref($conf) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $conf eq 'HASH') { } 37 3 40
elsif (ref $conf eq 'ARRAY' and $piece =~ /\d+/) { } 1 2 3
135 37 $conf = $conf->{$piece};
136 ▶ } elsif(ref($conf) eq 'ARRAY' && $piece =~ /\d+/) {
Condition66%2/3
ref $conf eq "ARRAY" and $piece =~ /\d+/
!ll&&!rl&&r
021
MC/DC50%1/2
ref $conf eq "ARRAY" and $piece =~ /\d+/
ref $conf eq "ARRAY" $piece =~ /\d+/
Truth table66%2/3
A and B
A= ref $conf eq "ARRAY"B= $piece =~ /\d+/
ABresult
0X0
100
111
137 1 $conf = $conf->[$piece];
138 } else {
139 # Not sure what they asked for, but it's not gonna work. Maybe a
140 # string member of an array?
141 2 $conf = undef;
142 }
143 40 ▶ return undef unless defined($conf);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf 3 37 40
144 }
145  
146 }
147  
148 22 ▶ if ( $self->convert_empty_to_undef ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->convert_empty_to_undef) 20 2 22
149 20 ▶ if ( ref $conf eq 'HASH' and not keys %$conf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf eq "HASH" and not keys %$conf) 1 19 20
Condition66%2/3
ref $conf eq "HASH" and not keys %$conf
!ll&&!rl&&r
1901
MC/DC50%1/2
ref $conf eq "HASH" and not keys %$conf
ref $conf eq "HASH" not keys %$conf
Truth table66%2/3
A and not(B)
A= ref $conf eq "HASH"B= not keys %$conf
ABresult
0X0
100
111
150 1 $conf = undef;
151 }
152 }
153  
154 22 return $conf;
155 }
156  
157  
158 2 ▶sub mask {
Subroutine mask: called CC=2 SCAR=6.9
Pod documented
159 2 my ($self, $path, $value) = @_;
160  
161 # Set the mask if there isn't one.
162 2 ▶ $self->_mask({}) unless $self->has_mask;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_mask 1 1 2
163  
164 # No reason to create a hierarchical setup here, just use the path as
165 # the key.
166 2 $self->_mask->{$path} = $value;
167 }
168  
169  
170 2 ▶after 'reload' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2 my $self = shift;
172 2 $self->clear_mask;
173 6 };
174  
175  
176 6 1;
177  
178 __END__
↑ Summary