Version 0.03
[gitmo/MooseX-ConfigFromFile.git] / lib / MooseX / ConfigFromFile.pm
index 2759f57..2137247 100644 (file)
@@ -2,8 +2,11 @@ package MooseX::ConfigFromFile;
 
 use Moose::Role;
 use MooseX::Types::Path::Class qw( File );
+use Try::Tiny qw/ try /;
+use Carp qw(croak);
+use namespace::autoclean;
 
-our $VERSION   = '0.01';
+our $VERSION = '0.03';
 
 requires 'get_config_from_file';
 
@@ -17,9 +20,27 @@ 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->find_attribute_by_name('configfile');
+        $configfile = try { to_File($class->configfile) };
+        $configfile ||= $cfmeta->default if $cfmeta->has_default;
+    }
+
+    if (defined $configfile) {
+        my $hash = $class->get_config_from_file($configfile);
+
+        no warnings 'uninitialized';
+        croak "get_config_from_file($configfile) did not return a hash (got $hash)"
+            unless ref $hash eq 'HASH';
+
+        %opts = (%$hash, %opts);
     }
+
     $class->new(%opts);
 }
 
@@ -62,6 +83,9 @@ MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a co
   use Moose;
   with 'MooseX::SomeSpecificConfigRole';
 
+  # optionally, default the configfile:
+  sub configfile { '/tmp/foo.yaml' }
+
   # ... insert your stuff here ...
 
   ########
@@ -81,6 +105,9 @@ It declares an attribute C<configfile> and a class method C<new_with_config>,
 and requires that concrete roles derived from it implement the class method
 C<get_config_from_file>.
 
+Attributes specified directly as arguments to C<new_with_config> supercede those
+in the configfile.
+
 L<MooseX::Getopt> 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<new_with_options>.
@@ -90,7 +117,14 @@ during its normal C<new_with_options>.
 =head2 configfile
 
 This is a L<Path::Class::File> 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' );
+
+Note that you can alternately just provide a C<configfile> method which returns
+the config file when called - this will be used in preference to the default of
+the attribute.
 
 =head1 Class Methods
 
@@ -108,18 +142,30 @@ 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<new()>.
-
-=head2 meta
+a hashref of arguments to pass to C<new()> which are sourced from the configfile.
 
-The Moose meta stuff, included here because otherwise pod tests fail sometimes
+=head1 COPYRIGHT
 
-=head1 BUGS
+Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
 
 =head1 AUTHOR
 
 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
 
+=head1 CONTRIBUTORS
+
+=over
+
+=item Tomas Doran C<< <bobtfish@bobtfish.net> >> (current maintainer).
+
+=item Karen Etheridge
+
+=item Chris Prather
+
+=item Zbigniew Lukasiak
+
+=back
+
 =head1 LICENSE
 
 This library is free software; you can redistribute it and/or modify