support for _get_default_configfile - RT#79746
[gitmo/MooseX-ConfigFromFile.git] / lib / MooseX / ConfigFromFile.pm
index b08dde0..03b3227 100644 (file)
@@ -17,6 +17,10 @@ has configfile => (
     predicate => 'has_configfile',
 );
 
+# overridable in consuming class or role to provide a default value
+# called before instantiation, so must be a class method!
+sub _get_default_configfile { }
+
 sub new_with_config {
     my ($class, %opts) = @_;
 
@@ -26,12 +30,23 @@ sub new_with_config {
         $configfile = $opts{configfile}
     }
     else {
-        my $cfmeta = $class->meta->find_attribute_by_name('configfile');
+        # This would only succeed if the consumer had changed the name of the
+        # reader method, and defined a new configfile sub in its place
         $configfile = try { to_File($class->configfile) };
-        $configfile ||= $cfmeta->default if $cfmeta->has_default;
+
+        # this is gross, but since a lot of users have swapped in their own
+        # default subs, we have to keep calling it rather than calling a
+        # builder sub directly - and it might not even be a coderef either
+        my $cfmeta = $class->meta->find_attribute_by_name('configfile');
+        $configfile ||= $cfmeta->default;
+
         if (ref $configfile eq 'CODE') {
-            $configfile = &$configfile($class);
+            $configfile = $configfile->($class);
         }
+
+        $configfile ||= $class->_get_default_configfile;
+
+        $opts{$cfmeta->init_arg} = $configfile if defined $configfile;
     }
 
     if (defined $configfile) {