1 package MooseX::ConfigFromFile;
4 use MooseX::Types::Path::Tiny 'Path';
5 use MooseX::Types::Moose 'Undef';
8 use namespace::autoclean;
10 requires 'get_config_from_file';
16 predicate => 'has_configfile',
17 do { try { require MooseX::Getopt; (traits => ['Getopt']) } },
19 # it sucks that we have to do this rather than using a builder, but some old code
20 # simply swaps in a new default sub into the attr definition
23 $class->_get_default_configfile if $class->can('_get_default_configfile');
28 my ($class, %opts) = @_;
32 if(defined $opts{configfile}) {
33 $configfile = $opts{configfile}
36 # This would only succeed if the consumer had defined a new configfile
37 # sub to override the generated reader - as suggested in old
38 # documentation -- or if $class is an instance not a class name
39 $configfile = try { $class->configfile };
41 # this is gross, but since a lot of users have swapped in their own
42 # default subs, we have to keep calling it rather than calling a
43 # builder sub directly - and it might not even be a coderef either
44 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
45 $configfile = $cfmeta->default if not defined $configfile and $cfmeta->has_default;
47 if (ref $configfile eq 'CODE') {
48 $configfile = $configfile->($class);
51 my $init_arg = $cfmeta->init_arg;
52 $opts{$init_arg} = $configfile if defined $configfile and defined $init_arg;
55 if (defined $configfile) {
56 my $hash = $class->get_config_from_file($configfile);
58 no warnings 'uninitialized';
59 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
60 unless ref $hash eq 'HASH';
62 %opts = (%$hash, %opts);
76 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
81 ## A real role based on this abstract role:
84 package MooseX::SomeSpecificConfigRole;
87 with 'MooseX::ConfigFromFile';
89 use Some::ConfigFile::Loader ();
91 sub get_config_from_file {
92 my ($class, $file) = @_;
94 my $options_hashref = Some::ConfigFile::Loader->load($file);
96 return $options_hashref;
101 ## A class that uses it:
105 with 'MooseX::SomeSpecificConfigRole';
107 # optionally, default the configfile:
108 sub _get_default_configfile { '/tmp/foo.yaml' }
110 # ... insert your stuff here ...
113 ## A script that uses the class with a configfile
116 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
120 This is an abstract role which provides an alternate constructor for creating
121 objects using parameters passed in from a configuration file. The
122 actual implementation of reading the configuration file is left to
125 It declares an attribute C<configfile> and a class method C<new_with_config>,
126 and requires that concrete roles derived from it implement the class method
127 C<get_config_from_file>.
129 Attributes specified directly as arguments to C<new_with_config> supersede those
132 L<MooseX::Getopt> knows about this abstract role, and will use it if available
133 to load attributes from the file specified by the command line flag C<--configfile>
134 during its normal C<new_with_options>.
140 This is a L<Path::Tiny> object which can be coerced from a regular path
141 string or any object that supports stringification.
142 This is the file your attributes are loaded from. You can add a default
143 configfile in the consuming class and it will be honored at the appropriate
144 time; see below at L</_get_default_configfile>.
146 If you have L<MooseX::Getopt> installed, this attribute will also have the
147 C<Getopt> trait supplied, so you can also set the configfile from the
152 =head2 new_with_config
154 This is an alternate constructor, which knows to look for the C<configfile> option
155 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
156 C<new_with_options>. Example:
158 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
160 Explicit arguments will override anything set by the configfile.
162 =head2 get_config_from_file
164 This class method is not implemented in this role, but it is required of all
165 classes or roles that consume this role.
166 Its two arguments are the class name and the configfile, and it is expected to return
167 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
169 =head2 _get_default_configfile
171 This class method is not implemented in this role, but can and should be defined
172 in a consuming class or role to return the default value of the configfile (if not
173 passed into the constructor explicitly).
177 Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
181 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
189 =item Karen Etheridge
193 =item Zbigniew Lukasiak
199 This library is free software; you can redistribute it and/or modify
200 it under the same terms as Perl itself.