Add way to pass parameters to Config::Any
lestrrat [Wed, 4 Nov 2009 08:25:53 +0000 (17:25 +0900)]
lib/MooseX/SimpleConfig.pm
t/12config_any_args.t [new file with mode: 0644]
t/lib/MXDriverArgsConfigTest.pm [new file with mode: 0644]

index e12b104..e3ad86c 100644 (file)
@@ -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 (file)
index 0000000..64522e9
--- /dev/null
@@ -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 <<EOM;
+Direct_Attr 123
+Inherited_Ro_Attr asdf
+Req_Attr foo
+EOM
+    close($test_conf);
+
+    my $foo = eval {
+        MXDriverArgsConfigTest->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 (file)
index 0000000..d069fd6
--- /dev/null
@@ -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;