From: Tomas Doran Date: Thu, 16 Dec 2010 19:11:31 +0000 (+0000) Subject: RT#57023 X-Git-Tag: 0.03~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0e88ec887622e39111ce7997735b63c8e27ef354;p=gitmo%2FMooseX-ConfigFromFile.git RT#57023 --- diff --git a/Makefile.PL b/Makefile.PL index 82038f9..e63c957 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,6 +9,7 @@ test_requires 'Test::Fatal'; 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; diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 29e82c9..b1e5cbb 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -2,6 +2,7 @@ package MooseX::ConfigFromFile; use Moose::Role; use MooseX::Types::Path::Class qw( File ); +use Try::Tiny qw/ try /; our $VERSION = '0.02'; @@ -26,10 +27,11 @@ sub new_with_config { } 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'; @@ -82,7 +84,7 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co with 'MooseX::SomeSpecificConfigRole'; # optionally, default the configfile: - has +configfile ( default => '/tmp/foo.yaml' ); + sub configfile { '/tmp/foo.yaml' } # ... insert your stuff here ... diff --git a/t/02subclass.t b/t/02subclass.t index 9cf24a8..dc9aebd 100644 --- a/t/02subclass.t +++ b/t/02subclass.t @@ -6,7 +6,7 @@ use Test::Fatal; package A; use Moose; with qw(MooseX::ConfigFromFile); - + sub get_config_from_file { } } diff --git a/t/03configfile_method.t b/t/03configfile_method.t new file mode 100644 index 0000000..4c3f4f8 --- /dev/null +++ b/t/03configfile_method.t @@ -0,0 +1,28 @@ +#!/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();