X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FConfigFromFile.pm;h=8d50c6a58b66862cf3f454e516b82cc9280f0d1d;hb=233548ddae7e1b8af9ccdf3943c4973bd653d2fe;hp=f1b5f21bda292c26951731f486afe1004fa846e9;hpb=f1040206706524d0d2f9ad2d8b2be983c6ef2971;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index f1b5f21..8d50c6a 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -1,20 +1,19 @@ 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.04'; - 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 +25,18 @@ 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 }; - $configfile ||= $cfmeta->default if $cfmeta->has_default; + + # 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 not defined $configfile and $cfmeta->has_default; + if (ref $configfile eq 'CODE') { - $configfile = &$configfile($class); + $configfile = $configfile->($class); } } @@ -65,9 +71,9 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co package MooseX::SomeSpecificConfigRole; use Moose::Role; - + with 'MooseX::ConfigFromFile'; - + use Some::ConfigFile::Loader (); sub get_config_from_file { @@ -87,7 +93,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 ... @@ -99,36 +105,43 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co =head1 DESCRIPTION -This is an abstract role which provides an alternate constructor for creating +This is an abstract role which provides an alternate constructor for creating objects using parameters passed in from a configuration file. The actual implementation of reading the configuration file is left to -concrete subroles. +concrete sub-roles. It declares an attribute C and a class method C, and requires that concrete roles derived from it implement the class method C. -Attributes specified directly as arguments to C supercede those +Attributes specified directly as arguments to C supersede those in the configfile. L knows about this abstract role, and will use it if available -to load attributes from the file specified by the commandline flag C<--configfile> +to load attributes from the file specified by the command line flag C<--configfile> during its normal C. =head1 Attributes =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 path +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 @@ -139,17 +152,18 @@ C. Example: my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml'); -Explicit arguments will overide anything set by the configfile. +Explicit arguments will override anything set by the configfile. =head2 get_config_from_file -This class method is not implemented in this role, but it is required of all subroles. -Its two arguments are the classname and the configfile, and it is expected to return +This class method is not implemented in this role, but it is required of all +classes or roles that consume this role. +Its two arguments are the class name and the configfile, and it is expected to return 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 +173,7 @@ Brandon L. Black, Eblblack@gmail.comE =over -=item Tomas Doran C<< >> (current maintainer). +=item Tomas Doran =item Karen Etheridge