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.05';
-
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']) } },
# 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 $cfmeta->has_default;
+ $configfile = $cfmeta->default if not defined $configfile and $cfmeta->has_default;
if (ref $configfile eq 'CODE') {
$configfile = $configfile->($class);
package MooseX::SomeSpecificConfigRole;
use Moose::Role;
-
+
with 'MooseX::ConfigFromFile';
-
+
use Some::ConfigFile::Loader ();
sub get_config_from_file {
=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<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
+Attributes specified directly as arguments to C<new_with_config> supersede 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>
+to load attributes from the file specified by the command line flag C<--configfile>
during its normal C<new_with_options>.
=head1 Attributes
=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. You can add a default
+This is a L<Path::Tiny> 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)
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<new()> which are sourced from the configfile.
=head1 COPYRIGHT