requires 'Moose' => '0.35';
requires 'MooseX::Types::Path::Class' => '0.04';
+requires 'Try::Tiny';
# Rebuild README for maintainers
system("pod2text lib/MooseX/ConfigFromFile.pm >README") if $Module::Install::AUTHOR;
use Moose::Role;
use MooseX::Types::Path::Class qw( File );
+use Try::Tiny qw/ try /;
our $VERSION = '0.02';
}
else {
my $cfmeta = $class->meta->find_attribute_by_name('configfile');
- $configfile = $cfmeta->default if $cfmeta->has_default;
+ $configfile = try { $class->configfile };
+ $configfile ||= $cfmeta->default if $cfmeta->has_default;
}
- if(defined $configfile) {
+ if (defined $configfile) {
my $hash = $class->get_config_from_file($configfile);
no warnings 'uninitialized';
with 'MooseX::SomeSpecificConfigRole';
# optionally, default the configfile:
- has +configfile ( default => '/tmp/foo.yaml' );
+ sub configfile { '/tmp/foo.yaml' }
# ... insert your stuff here ...
--- /dev/null
+#!/usr/bin/env perl
+use strict;
+use Test::More;
+use Test::Fatal;
+{
+ package A;
+ use Moose;
+ with qw(MooseX::ConfigFromFile);
+
+ sub configfile { 'moo' }
+
+ sub get_config_from_file { {} }
+}
+
+{
+ package B;
+ use Moose;
+ extends qw(A);
+
+ sub configfile { die; }
+ has configfile => ( is => 'bare', default => 'bar' );
+
+}
+
+is(exception { A->new_with_config() }, undef, 'A->new_with_config lives');
+is(exception { B->new_with_config() }, undef, 'B->new_with_config lives');
+
+done_testing();