package MooseX::ConfigFromFile;
+# ABSTRACT: An abstract Moose role for setting attributes from a configfile
use Moose::Role;
-use MooseX::Types::Path::Tiny 'Path';
-use Try::Tiny qw/ try /;
+use MooseX::Types::Path::Tiny 0.005 'Path';
+use MooseX::Types::Moose 'Undef';
+use Try::Tiny;
use Carp qw(croak);
use namespace::autoclean;
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 {
}
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
=pod
-=head1 NAME
-
-MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
-
=head1 SYNOPSIS
########
with 'MooseX::SomeSpecificConfigRole';
# optionally, default the configfile:
- around configfile => sub { '/tmp/foo.yaml' };
+ sub _get_default_configfile { '/tmp/foo.yaml' }
# ... insert your stuff here ...
and requires that concrete roles derived from it implement the class method
C<get_config_from_file>.
+=for stopwords configfile
+
Attributes specified directly as arguments to C<new_with_config> supersede those
in the configfile.
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)
-
- around configfile => sub { '/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.
+configfile in the consuming class and it will be honored at the appropriate
+time; see below at L</_get_default_configfile>.
If you have L<MooseX::Getopt> installed, this attribute will also have the
C<Getopt> trait supplied, so you can also set the configfile from the
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
-
-Copyright (c) - 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
-
-=item Karen Etheridge
-
-=item Chris Prather
-
-=item Zbigniew Lukasiak
-
-=back
-
-=head1 LICENSE
+=head2 _get_default_configfile
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+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).
=cut