From: Matt S Trout Date: Fri, 3 Jul 2015 19:04:48 +0000 (+0000) Subject: add make_variant_package_name optional method X-Git-Tag: v1.003000~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6779ecfd9c1a2a51cc5c26596ced7a36e27cf9dd;p=p5sagit%2FPackage-Variant.git add make_variant_package_name optional method --- diff --git a/Changes b/Changes index ce9d1db..24dc98d 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Package-Variant + - add make_variant_package_name optional method + 1.002002 - 2014-08-21 - fix license in metadata diff --git a/lib/Package/Variant.pm b/lib/Package/Variant.pm index a242d93..06cf0bf 100644 --- a/lib/Package/Variant.pm +++ b/lib/Package/Variant.pm @@ -94,9 +94,17 @@ sub import { }; } +sub build_variant_package_name { + my ($me, $variable, @args) = @_; + if ($variable->can('make_variant_package_name')) { + return $variable->make_variant_package_name(@args); + } + return "${variable}::_Variant_".++$Variable{$variable}{anon}; +} + sub build_variant_of { my ($me, $variable, @args) = @_; - my $variant_name = "${variable}::_Variant_".++$Variable{$variable}{anon}; + my $variant_name = $me->build_variant_package_name($variable, @args); foreach my $to_import (@{$Variable{$variable}{args}{importing}}) { my ($pkg, $args) = @$to_import; require_module $pkg; @@ -316,6 +324,19 @@ declared in L to customize the new variant package. This is a class method receiving the C<$target> package and the C<@arguments> defining the requested variant. +=head2 make_variant_package_name + + Some::Variant::Package->make_variant_package_name( @arguments ); + +B If present, this method will be +used to determine the package name for a particular variant being constructed. + +If you do not implement it, a unique package name something like + + Some::Variant::Package::_Variant_A003 + +will be created for you. + =head2 import use Some::Variant::Package; diff --git a/t/01simple.t b/t/01simple.t index 3d3813d..6e8348c 100644 --- a/t/01simple.t +++ b/t/01simple.t @@ -131,4 +131,23 @@ like exception { ); }, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list'; +BEGIN { + package TestOverrideName; + + use Package::Variant; + + sub make_variant_package_name { + my (undef, @args) = @_; + return $args[0]; + } + + sub make_variant { + install hey => sub { 'hey' }; + } +} + +is(TestOverrideName::->build_variant('hey'), 'hey'); + +is(hey->hey, 'hey', 'hey'); + done_testing;