X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FConfigFromFile.pm;h=fb221668802aa91e4737fc7335eba0d780d73baf;hb=097fca8f8aa39c3111a01edf4d1f16cf0d660280;hp=a2d22111f7ea70c2480a087cbd79b59066877c7d;hpb=83f0ce5451073fa234ba80f4cfee5da45f60988a;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index a2d2211..fb22166 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 { @@ -26,9 +27,16 @@ 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 defined a new configfile + # sub to override the generated reader $configfile = try { $class->configfile }; + + # 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 $cfmeta->has_default; + if (ref $configfile eq 'CODE') { $configfile = $configfile->($class); } @@ -87,7 +95,7 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co with 'MooseX::SomeSpecificConfigRole'; # optionally, default the configfile: - sub configfile { '/tmp/foo.yaml' } + around configfile => sub { '/tmp/foo.yaml' }; # ... insert your stuff here ... @@ -121,14 +129,20 @@ during its normal C. This is a L object which can be coerced from a regular pathname string. This is the file your attributes are loaded from. You can add a default -configfile in the class using the role and it will be honored at the appropriate time: +configfile in the consuming class and it will be honored at the appropriate time +(note that a simple sub declaration is not sufficient, as there is already a +sub by that name being added by Moose as the attribute reader) - has +configfile ( default => '/etc/myapp.yaml' ); + around configfile => sub { '/etc/myapp.yaml' }; 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 @@ -159,7 +173,7 @@ Brandon L. Black, Eblblack@gmail.comE =over -=item Tomas Doran C<< >> (current maintainer). +=item Tomas Doran =item Karen Etheridge