Skip to content

Commit

Permalink
Added ->found functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
robertkrimen committed May 14, 2009
1 parent 1f46707 commit bbe1119
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
- Fixed t/14-loader-env-lookup.t to ignore external ENV (rt45624)
- Require Data::Visitor 0.24 (rt45624)
- Changed how the 'file' option to work how the user actually expects it to (and how it is in the SYNOPSIS)
- Added ->found functionality

0.05 Wednesday March 18 12:36:57 PDT 2009:
- Modified tests to pass on Windows using Path::Class (thanks Dan Dascalescu)
Expand Down
24 changes: 15 additions & 9 deletions lib/Config/JFDI.pm
Expand Up @@ -86,7 +86,7 @@ use Clone qw//;

has package => qw/is ro isa Str/;

has reader => qw/is ro/, handles => [qw/ driver local_suffix no_env env_lookup path /];
has source => qw/is ro/, handles => [qw/ driver local_suffix no_env env_lookup path found /];

#has driver => qw/is ro lazy_build 1/;
#sub _build_driver {
Expand Down Expand Up @@ -173,13 +173,13 @@ sub BUILD {

$self->{package} = $given->{name} if defined $given->{name} && ! defined $self->{package} && ! ref $given->{name};

my ($reader, %reader);
my ($source, %source);
if ($given->{file}) {
carp "The behavior of the 'file' option has changed, pass in 'quiet_deprecation' or 'no_06_warning' to disable this warning"
unless $given->{quiet_deprecation} || $given->{no_06_warning};
carp "Warning, overriding path setting with file (\"$given->{file}\" instead of \"$given->{path}\")" if $given->{path};
$given->{path} = $given->{file};
$reader{path_is_file} = 1;
$source{path_is_file} = 1;
}

{
Expand All @@ -195,17 +195,17 @@ sub BUILD {
env_lookup
/) {
$reader{$_} = $given->{$_} if exists $given->{$_};
$source{$_} = $given->{$_} if exists $given->{$_};
}

carp "Warning, 'local_suffix' will be ignored if 'file' is given, use 'path' instead" if exists $reader{local_suffix};
carp "Warning, 'local_suffix' will be ignored if 'file' is given, use 'path' instead" if exists $source{local_suffix};

$reader{local_suffix} = $given->{config_local_suffix} if $given->{config_local_suffix};
$source{local_suffix} = $given->{config_local_suffix} if $given->{config_local_suffix};

$reader = Config::JFDI::Source::Loader->new( %reader );
$source = Config::JFDI::Source::Loader->new( %source );
}

$self->{reader} = $reader;
$self->{source} = $source;

for (qw/substitute substitutes substitutions substitution/) {
if ($given->{$_}) {
Expand Down Expand Up @@ -237,6 +237,12 @@ Load a config as specified by ->new(...) and ENV and return a hash
These will only load the configuration once, so it's safe to call them multiple times without incurring any loading-time penalty
=head2 $config->found
Returns a list of files found
If the list is empty, then no files were loaded/read
=cut

sub get {
Expand Down Expand Up @@ -264,7 +270,7 @@ sub load {
$self->_config($self->default);

{
my @read = $self->reader->read;
my @read = $self->source->read;

$self->_load($_) for @read;
}
Expand Down
17 changes: 16 additions & 1 deletion lib/Config/JFDI/Source/Loader.pm
@@ -1,6 +1,7 @@
package Config::JFDI::Source::Loader;

use Moose;
use MooseX::AttributeHelpers;

use Config::Any;
use Carp;
Expand All @@ -25,6 +26,10 @@ has env_lookup => qw/is ro/, default => sub { [] };

has path_is_file => qw/is ro default 0/;

has _found => qw/metaclass Collection::Array is rw isa ArrayRef/, provides => {qw/
elements found
/};

sub _env(@) {
my $key = uc join "_", @_;
$key =~ s/::/_/g;
Expand Down Expand Up @@ -59,6 +64,7 @@ sub read {
my @files = $self->_find_files;
my $cfg_files = $self->_load_files(\@files);
my %cfg_files = map { (%$_)[0] => $_ } reverse @$cfg_files;
$self->_found( [ map { (%$_)[0] } @$cfg_files ] );

my (@cfg, @local_cfg);
{
Expand All @@ -80,6 +86,15 @@ sub read {
return $self->no_local ? @cfg : (@cfg, @local_cfg);
}

around found => sub {
my $inner = shift;
my $self = shift;

$self->read unless $self->{_found};

return $inner->( $self, @_ );
};

sub _load_files {
my $self = shift;
my $files = shift;
Expand All @@ -90,7 +105,7 @@ sub _load_files {
});
}

sub _find_files {
sub _find_files { # Doesn't really find files...hurm...
my $self = shift;

if ($self->path_is_file) {
Expand Down
47 changes: 47 additions & 0 deletions t/17-found.t
@@ -0,0 +1,47 @@
use strict;
use warnings;

use Test::Most;

plan qw/no_plan/;

use Config::JFDI;

sub has_Config_General {
return eval "require Config::General;";
}

{
my $config = Config::JFDI->new( file => 't/assets/some_random_file.pl', quiet_deprecation => 1 );

ok( $config->get );
ok( keys %{ $config->get } );
ok( $config->found );
cmp_deeply( [ $config->found ], [ 't/assets/some_random_file.pl' ] );
}

{
my $config = Config::JFDI->new( qw{ name xyzzy path t/assets } );
ok( $config->get );
ok( keys %{ $config->get } );
ok( $config->found );
cmp_deeply( [ $config->found ], [ 't/assets/xyzzy.pl', 't/assets/xyzzy_local.pl' ] );
}

{
my $config = Config::JFDI->new( file => 't/assets/missing-file.pl', quiet_deprecation => 1 );

ok( $config->get );
cmp_deeply( $config->get, {} );
ok( !$config->found );
}

{
my $config = Config::JFDI->new( file => 't/assets/some_random_file.pl', quiet_deprecation => 1 );

ok( $config->found ); # Do ->read via ->found
ok( $config->get );
ok( keys %{ $config->get } );
cmp_deeply( [ $config->found ], [ 't/assets/some_random_file.pl' ] );
}

0 comments on commit bbe1119

Please sign in to comment.