From: Brandon L Black Date: Wed, 23 Jan 2008 05:58:02 +0000 (+0000) Subject: 0.02, use default configfile X-Git-Tag: 0.02^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fc44be6f0832752b4dd69c09ba826d7964b64f3e;p=gitmo%2FMooseX-ConfigFromFile.git 0.02, use default configfile --- diff --git a/ChangeLog b/ChangeLog index 167db3d..ca765f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ Revision history for Perl extension MooseX::ConfigFromFile +0.02 - Jan 23, 2008 + - Honor any default supplied for the configfile attribute + 0.01 - Dec 18, 2007 - Initial release diff --git a/Makefile.PL b/Makefile.PL index 3316d0a..feff8f4 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -6,8 +6,8 @@ all_from 'lib/MooseX/ConfigFromFile.pm'; test_requires 'Test::More' => '0.42'; -requires 'Moose' => '0'; -requires 'MooseX::Types::Path::Class' => '0'; +requires 'Moose' => '0.35'; +requires 'MooseX::Types::Path::Class' => '0.04'; # Rebuild README for maintainers system("pod2text lib/MooseX/ConfigFromFile.pm >README") if -e 'MANIFEST.SKIP'; diff --git a/README b/README index 6f251de..9635b7d 100644 --- a/README +++ b/README @@ -30,6 +30,9 @@ SYNOPSIS use Moose; with 'MooseX::SomeSpecificConfigRole'; + # optionally, default the configfile: + has +configfile ( default => '/tmp/foo.yaml' ); + # ... insert your stuff here ... ######## @@ -48,6 +51,9 @@ DESCRIPTION "new_with_config", and requires that concrete roles derived from it implement the class method "get_config_from_file". + Attributes specified directly as arguments to "new_with_config" + supercede those in the configfile. + MooseX::Getopt knows about this abstract role, and will use it if available to load attributes from the file specified by the commandline flag "--configfile" during its normal "new_with_options". @@ -55,7 +61,11 @@ DESCRIPTION Attributes configfile This is a Path::Class::File object which can be coerced from a regular - pathname string. This is the file your attributes are loaded from. + 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: + + has +configfile ( default => '/etc/myapp.yaml' ); Class Methods new_with_config @@ -70,7 +80,8 @@ Class Methods 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 a hashref of arguments to pass to "new()". + and it is expected to return a hashref of arguments to pass to "new()" + which are sourced from the configfile. meta The Moose meta stuff, included here because otherwise pod tests fail diff --git a/lib/MooseX/ConfigFromFile.pm b/lib/MooseX/ConfigFromFile.pm index 2759f57..57e6f5b 100644 --- a/lib/MooseX/ConfigFromFile.pm +++ b/lib/MooseX/ConfigFromFile.pm @@ -3,7 +3,7 @@ package MooseX::ConfigFromFile; use Moose::Role; use MooseX::Types::Path::Class qw( File ); -our $VERSION = '0.01'; +our $VERSION = '0.02'; requires 'get_config_from_file'; @@ -17,9 +17,20 @@ has configfile => ( sub new_with_config { my ($class, %opts) = @_; - if($opts{configfile}) { - %opts = (%{$class->get_config_from_file($opts{configfile})}, %opts); + my $configfile; + + if(defined $opts{configfile}) { + $configfile = $opts{configfile} + } + else { + my $cfmeta = $class->meta->get_attribute('configfile'); + $configfile = $cfmeta->default if $cfmeta->has_default; + } + + if(defined $configfile) { + %opts = (%{$class->get_config_from_file($configfile)}, %opts); } + $class->new(%opts); } @@ -62,6 +73,9 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co use Moose; with 'MooseX::SomeSpecificConfigRole'; + # optionally, default the configfile: + has +configfile ( default => '/tmp/foo.yaml' ); + # ... insert your stuff here ... ######## @@ -81,6 +95,9 @@ 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 +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> during its normal C. @@ -90,7 +107,10 @@ 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. +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: + + has +configfile ( default => '/etc/myapp.yaml' ); =head1 Class Methods @@ -108,7 +128,7 @@ Explicit arguments will overide anything set by the configfile. 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 -a hashref of arguments to pass to C. +a hashref of arguments to pass to C which are sourced from the configfile. =head2 meta