From: lestrrat Date: Wed, 4 Nov 2009 08:25:53 +0000 (+0900) Subject: Add way to pass parameters to Config::Any X-Git-Tag: 0.04~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=392e82e0852e5968030e979790001e9c1b9f1bf5;p=gitmo%2FMooseX-SimpleConfig.git Add way to pass parameters to Config::Any --- diff --git a/lib/MooseX/SimpleConfig.pm b/lib/MooseX/SimpleConfig.pm index e12b104..e3ad86c 100644 --- a/lib/MooseX/SimpleConfig.pm +++ b/lib/MooseX/SimpleConfig.pm @@ -10,10 +10,12 @@ use Config::Any (); sub get_config_from_file { my ($class, $file) = @_; - my $raw_cfany = Config::Any->load_files({ - files => [ $file ], - use_ext => 1, - }); + my $can_config_any_args = $class->can('config_any_args'); + my %args = $can_config_any_args ? + ( %{$can_config_any_args->($class, $file)}, files => [ $file ], use_ext => 1 ) : + ( files => [ $file ], use_ext => 1 ) + ; + my $raw_cfany = Config::Any->load_files(\%args); die q{Specified configfile '} . $file . q{' does not exist, is empty, or is not readable} diff --git a/t/12config_any_args.t b/t/12config_any_args.t new file mode 100644 index 0000000..64522e9 --- /dev/null +++ b/t/12config_any_args.t @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use lib 't/lib'; +use lib '../t/lib'; + +BEGIN { + use Test::More; + + eval "use Config::General ()"; + if($@) { + plan skip_all => "Config::General required for this test"; + } + + plan tests => 6; + + use_ok('MXDriverArgsConfigTest'); +} + +# Does it work with no configfile and not barf? +{ + eval { MXDriverArgsConfigTest->new(req_attr => 'foo') }; + ok(!$@, 'Did not die with no configfile specified') + or diag $@; +} + +# Can it load a simple YAML file with the options +{ + open(my $test_conf, '>', 'test.conf') + or die "Cannot create test.conf: $!"; + print $test_conf <new_with_config(configfile => 'test.conf'); + }; + ok(!$@, 'Did not die with good General configfile') + or diag $@; + + is($foo->req_attr, 'foo', 'req_attr works'); + is($foo->direct_attr, 123, 'direct_attr works'); + is($foo->inherited_ro_attr, 'asdf', 'inherited_ro_attr works'); +} + +END { unlink('test.conf') } \ No newline at end of file diff --git a/t/lib/MXDriverArgsConfigTest.pm b/t/lib/MXDriverArgsConfigTest.pm new file mode 100644 index 0000000..d069fd6 --- /dev/null +++ b/t/lib/MXDriverArgsConfigTest.pm @@ -0,0 +1,29 @@ +package MXDriverArgsConfigTestBase; +use Moose; + +has 'inherited_ro_attr' => (is => 'ro', isa => 'Str'); + +no Moose; +1; + +package MXDriverArgsConfigTest; +use Moose; +extends 'MXDriverArgsConfigTestBase'; +with 'MooseX::SimpleConfig'; + +has 'direct_attr' => (is => 'ro', isa => 'Int'); + +has 'req_attr' => (is => 'rw', isa => 'Str', required => 1); + +sub config_any_args { + return +{ + driver_args => { + General => { + -LowerCaseNames => 1 + } + } + } +} + +no Moose; +1;