From: Karen Etheridge Date: Fri, 8 Feb 2013 00:34:14 +0000 (-0800) Subject: pass configfile value through to new() X-Git-Tag: v0.08~1^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8b4a3e7640107c8fa677673024b83ba3315393f9;hp=233548ddae7e1b8af9ccdf3943c4973bd653d2fe;p=gitmo%2FMooseX-ConfigFromFile.git pass configfile value through to new() --- diff --git a/Changes b/Changes index 980e335..bc58688 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for {{$dist->name}} {{$NEXT}} - allow configfiles called "0" + - configfile value now passed through to new() 0.07 2013-02-04 (Karen Etheridge) - fixed tests to not load optional dependencies diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 8d50c6a..481839e 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -38,6 +38,9 @@ sub new_with_config { if (ref $configfile eq 'CODE') { $configfile = $configfile->($class); } + + my $init_arg = $cfmeta->init_arg; + $opts{$init_arg} = $configfile if defined $configfile and defined $init_arg; } if (defined $configfile) { diff --git a/t/05_default_sub.t b/t/05_default_sub.t index cfe826b..6c41191 100644 --- a/t/05_default_sub.t +++ b/t/05_default_sub.t @@ -2,14 +2,14 @@ use strict; use warnings FATAL => 'all'; use Test::Requires 'MooseX::SimpleConfig'; # skip all if not reuqired -use Test::More tests => 11; +use Test::More tests => 19; use Test::Fatal; use Test::Deep '!blessed'; use Test::NoWarnings 1.04 ':early'; use Scalar::Util 'blessed'; my %loaded_file; -my %default_sub; +my %constructor_args; # nothing special going on here @@ -23,6 +23,16 @@ my %default_sub; $loaded_file{$file}++; +{} } + around BUILDARGS => sub { + my ($orig, $class) = (shift, shift); + my $args = $class->$orig(@_); + $constructor_args{$class} = $args; + }; + sub __my_configfile + { + my $class = blessed($_[0]) || $_[0]; + $class . ' file' + } } is( @@ -30,6 +40,11 @@ is( my $obj = Generic->new_with_config; is($obj->configfile, undef, 'no configfile set'); cmp_deeply(\%loaded_file, {}, 'no files loaded'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { }, + 'correct constructor args passed', + ); }, undef, 'no exceptions', @@ -57,24 +72,44 @@ is( 'no exceptions', ); +# legacy usecase, and configfile init_arg has been changed +{ + package OverriddenDefaultAndChangedName; + use Moose; + extends 'Generic'; + has '+configfile' => ( + init_arg => 'my_configfile', + default => 'OverriddenDefaultAndChangedName file', + ); +} + +is( + exception { + my $obj = OverriddenDefaultAndChangedName->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden default'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { my_configfile => blessed($obj) . ' file' }, + 'correct constructor args passed', + ); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); # "reader" method is overridden to provide for configfile default { package OverriddenMethod; use Moose; extends 'Generic'; - around configfile => sub { - my $class = blessed($_[1]) || $_[1]; - $default_sub{$class}++; - $class . ' file' - }; + around configfile => sub { my $orig = shift; shift->__my_configfile }; } is( exception { my $obj = OverriddenMethod->new_with_config; is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub'); - ok($default_sub{blessed($obj)}, 'default sub was called'); is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); }, undef, @@ -82,3 +117,29 @@ is( ); +# overridable method for configfile default, and configfile init_arg is changed +{ + package OverriddenMethodAndChangedName; + use Moose; + extends 'Generic'; + has '+configfile' => ( + init_arg => 'my_configfile', + ); + around configfile => sub { my $orig = shift; shift->__my_configfile }; +} + +is( + exception { + my $obj = OverriddenMethodAndChangedName->new_with_config; + is($obj->configfile, blessed($obj) . ' file', 'configfile set via overridden sub'); + cmp_deeply( + $constructor_args{blessed($obj)}, + { my_configfile => blessed($obj) . ' file' }, + 'correct constructor args passed', + ); + is($loaded_file{blessed($obj) . ' file'}, 1, 'correct file was loaded from'); + }, + undef, + 'no exceptions', +); +