X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FConfigFromFile.pm;h=43168637aa40a3736eb0b2edc3092ee11e35f9f8;hb=75faf71e9f906a22899b2728fee90d3906b1c0f6;hp=5557c53ad9d4484b372ede675fa8b32e96d9c6e2;hpb=56e4351b360e0c6e8f24d83c1bf2af08b1fdb17e;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 5557c53..4316863 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -1,20 +1,21 @@ package MooseX::ConfigFromFile; use Moose::Role; -use MooseX::Types::Path::Class qw( File ); +use MooseX::Types::Path::Tiny 'Path'; use Try::Tiny qw/ try /; use Carp qw(croak); use namespace::autoclean; -our $VERSION = '0.03'; +our $VERSION = '0.06'; requires 'get_config_from_file'; has configfile => ( is => 'ro', - isa => File, + isa => Path, coerce => 1, predicate => 'has_configfile', + do { try { require MooseX::Getopt; (traits => ['Getopt']) } }, ); sub new_with_config { @@ -26,11 +27,18 @@ sub new_with_config { $configfile = $opts{configfile} } else { + # 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 = try { to_File($class->configfile) }; $configfile ||= $cfmeta->default if $cfmeta->has_default; + if (ref $configfile eq 'CODE') { - $configfile = &$configfile($class); + $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 ... @@ -119,16 +127,23 @@ during its normal C. =head2 configfile -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: +This is a L object which can be coerced from a regular pathname +string or any object that supports stringification. +This is the file your attributes are loaded from. You can add a default +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 @@ -149,7 +164,7 @@ a hashref of arguments to pass to C which are sourced from the configfile =head1 COPYRIGHT -Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below. +Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below. =head1 AUTHOR @@ -159,7 +174,7 @@ Brandon L. Black, Eblblack@gmail.comE =over -=item Tomas Doran C<< >> (current maintainer). +=item Tomas Doran =item Karen Etheridge