From: Karen Etheridge Date: Sat, 2 Feb 2013 23:34:45 +0000 (-0800) Subject: apply Getopt trait if available X-Git-Tag: 0.05~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=742b859c0f89bf4dc325980079eb770771c1c709;p=gitmo%2FMooseX-ConfigFromFile.git apply Getopt trait if available --- diff --git a/ChangeLog b/ChangeLog index c145019..2f52e4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,8 @@ Revision history for Perl extension MooseX::ConfigFromFile - documentation corrected to demostrate how to properly override the configfile method to provide a default from the consuming class, without having to redefine the attribute itself + - if MooseX::Getopt is installed, the configfile attribute has the Getopt + trait applied (Karen Etheridge) 0.04 - Dec 17, 2011 - Call the configfile attribute default sub if it is a sub, not just a diff --git a/Makefile.PL b/Makefile.PL index 2b2ecb9..71bd50c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -6,6 +6,9 @@ all_from 'lib/MooseX/ConfigFromFile.pm'; test_requires 'Test::More' => '0.42'; test_requires 'Test::Fatal'; +test_requires 'Test::Requires'; +test_requires 'Test::NoWarnings' => '1.04'; +test_requires 'Test::Without::Module'; requires 'Moose' => '0.35'; requires 'MooseX::Types::Path::Class' => '0.04'; diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 2de2688..5bdc8c3 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -15,6 +15,7 @@ has configfile => ( isa => File, coerce => 1, predicate => 'has_configfile', + do { try { require MooseX::Getopt; (traits => ['Getopt']) } }, ); sub new_with_config { @@ -138,6 +139,10 @@ Note that you can alternately just provide a C method which returns the config file when called - this will be used in preference to the default of the attribute. +If you have L installed, this attribute will also have the +C trait supplied, so you can also set the configfile from the +command line. + =head1 Class Methods =head2 new_with_config diff --git a/t/04a_getopt.t b/t/04a_getopt.t new file mode 100644 index 0000000..2677f50 --- /dev/null +++ b/t/04a_getopt.t @@ -0,0 +1,21 @@ +use strict; +use warnings; + +use Test::More tests => 2; +use Test::NoWarnings 1.04 ':early'; + +use Test::Without::Module 'MooseX::Getopt'; + +{ + package Foo::NoOptions; + + use Moose; + with qw(MooseX::ConfigFromFile); + sub get_config_from_file { } +} + +ok( + !Foo::NoOptions->meta->find_attribute_by_name('configfile')->does('MooseX::Getopt::Meta::Attribute::Trait'), + 'the Getopt attr trait is not added if not installed', +); + diff --git a/t/04b_getopt.t b/t/04b_getopt.t new file mode 100644 index 0000000..4db87d3 --- /dev/null +++ b/t/04b_getopt.t @@ -0,0 +1,33 @@ +use strict; +use warnings; + +use Test::Requires 'MooseX::Getopt'; # skip all if not installed +use Test::More tests => 3; +use Test::NoWarnings 1.04 ':early'; + +{ + package Foo::Options; + + use Moose; + with qw(MooseX::Getopt MooseX::ConfigFromFile); + sub get_config_from_file { } +} + +{ + package Foo::NoOptions; + + use Moose; + with qw(MooseX::ConfigFromFile); + sub get_config_from_file { } +} + +ok( + Foo::Options->meta->find_attribute_by_name('configfile')->does('MooseX::Getopt::Meta::Attribute::Trait'), + 'classes with MooseX::Getopt have the Getopt attr trait added', +); + +ok( + Foo::NoOptions->meta->find_attribute_by_name('configfile')->does('MooseX::Getopt::Meta::Attribute::Trait'), + 'when MooseX::Getopt is loaded, the Getopt attr trait is still added', +); +