X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FConfigFromFile.pm;h=cd6cfdf729897b7a8d3c458d8b195cdbec2b0a1e;hb=74e7e6279f4e747018a3b72a8c13ea21e3919395;hp=07468c4dca6b5f2ae2c6f92b8646b9eba90b195b;hpb=491a10839ac9a4bf14e46e04b44cb260f65dfa78;p=gitmo%2FMooseX-ConfigFromFile.git diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 07468c4..cd6cfdf 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -2,7 +2,8 @@ package MooseX::ConfigFromFile; use Moose::Role; use MooseX::Types::Path::Tiny 'Path'; -use Try::Tiny qw/ try /; +use MooseX::Types::Moose 'Undef'; +use Try::Tiny; use Carp qw(croak); use namespace::autoclean; @@ -10,10 +11,17 @@ requires 'get_config_from_file'; has configfile => ( is => 'ro', - isa => Path, + isa => Path|Undef, coerce => 1, predicate => 'has_configfile', do { try { require MooseX::Getopt; (traits => ['Getopt']) } }, + lazy => 1, + # it sucks that we have to do this rather than using a builder, but some old code + # simply swaps in a new default sub into the attr definition + default => sub { + my $class = shift; + $class->_get_default_configfile if $class->can('_get_default_configfile'); + }, ); sub new_with_config { @@ -26,7 +34,8 @@ sub new_with_config { } else { # This would only succeed if the consumer had defined a new configfile - # sub to override the generated reader + # sub to override the generated reader - as suggested in old + # documentation -- or if $class is an instance not a class name $configfile = try { $class->configfile }; # this is gross, but since a lot of users have swapped in their own @@ -38,6 +47,9 @@ sub new_with_config { if (ref $configfile eq 'CODE') { $configfile = $configfile->($class); } + + my $init_arg = $cfmeta->init_arg; + $opts{$init_arg} = $configfile if defined $configfile and defined $init_arg; } if (defined $configfile) { @@ -71,9 +83,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 { @@ -93,7 +105,7 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co with 'MooseX::SomeSpecificConfigRole'; # optionally, default the configfile: - around configfile => sub { '/tmp/foo.yaml' }; + sub _get_default_configfile { '/tmp/foo.yaml' } # ... insert your stuff here ... @@ -105,7 +117,7 @@ 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 sub-roles. @@ -128,15 +140,8 @@ during its normal C. 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) - - 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. +configfile in the consuming class and it will be honored at the appropriate +time; see below at L. If you have L installed, this attribute will also have the C trait supplied, so you can also set the configfile from the @@ -161,6 +166,12 @@ 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. +=head2 _get_default_configfile + +This class method is not implemented in this role, but can and should be defined +in a consuming class or role to return the default value of the configfile (if not +passed into the constructor explicitly). + =head1 COPYRIGHT Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.