| 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
Condition100%3/3
$self->has_directory and $self->has_files
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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$self->has_directory or $self->has_files
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | 4 | my $dir = $self->directory; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | unless(-d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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/$_"
MC/DC100%2/2
!($_ =~ /^\./) && -f "$dir/$_"
!($_ =~ /^\./)
-f "$dir/$_"
Truth table100%3/3
not(A) && B
A= !($_ =~ /^\./)B= -f "$dir/$_"
| |||||||||||||||||||||||||||||
| 88 | 4 | closedir($dh); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | $files = \@files; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 9 | ▶ | if(!defined($files) || scalar(@{ $files }) < 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $files or scalar @{$files;} < 1
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 104 | 20 | ▶ | next unless defined $anyconf->{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 20 | $config = $merge->merge($config, $anyconf->{$file}); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 9 | ▶ | if(defined($config)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 135 | 37 | $conf = $conf->{$piece}; | |||||||||||||||||||||||||||
| 136 | ▶ | } elsif(ref($conf) eq 'ARRAY' && $piece =~ /\d+/) { | |||||||||||||||||||||||||||
|
Condition66%2/3
ref $conf eq "ARRAY" and $piece =~ /\d+/
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+/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 22 | ▶ | if ( $self->convert_empty_to_undef ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 20 | ▶ | if ( ref $conf eq 'HASH' and not keys %$conf ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $conf eq "HASH" and not keys %$conf
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||