From bbe111999da0bf009036a72e8c5ce8a8f7cf97b7 Mon Sep 17 00:00:00 2001 From: robertkrimen Date: Wed, 13 May 2009 18:44:21 -0700 Subject: [PATCH] Added ->found functionality --- Changes | 1 + lib/Config/JFDI.pm | 24 ++++++++++------ lib/Config/JFDI/Source/Loader.pm | 17 +++++++++++- t/17-found.t | 47 ++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 t/17-found.t diff --git a/Changes b/Changes index 35f5c8a..712bd54 100644 --- a/Changes +++ b/Changes @@ -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) diff --git a/lib/Config/JFDI.pm b/lib/Config/JFDI.pm index 69ff7ef..e8e324d 100644 --- a/lib/Config/JFDI.pm +++ b/lib/Config/JFDI.pm @@ -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 { @@ -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; } { @@ -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->{$_}) { @@ -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 { @@ -264,7 +270,7 @@ sub load { $self->_config($self->default); { - my @read = $self->reader->read; + my @read = $self->source->read; $self->_load($_) for @read; } diff --git a/lib/Config/JFDI/Source/Loader.pm b/lib/Config/JFDI/Source/Loader.pm index d719a03..3a36f9c 100644 --- a/lib/Config/JFDI/Source/Loader.pm +++ b/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; @@ -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; @@ -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); { @@ -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; @@ -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) { diff --git a/t/17-found.t b/t/17-found.t new file mode 100644 index 0000000..15250ea --- /dev/null +++ b/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' ] ); +} +