add make_variant_package_name optional method
Matt S Trout [Fri, 3 Jul 2015 19:04:48 +0000 (19:04 +0000)]
Changes
lib/Package/Variant.pm
t/01simple.t

diff --git a/Changes b/Changes
index ce9d1db..24dc98d 100644 (file)
--- 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
 
index a242d93..06cf0bf 100644 (file)
@@ -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</subs> 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<You may optionally provide this method.> 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;
index 3d3813d..6e8348c 100644 (file)
@@ -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;