1 package MooseX::ConfigFromFile;
4 use MooseX::Types::Path::Class qw( File );
5 use Try::Tiny qw/ try /;
7 use namespace::autoclean;
11 requires 'get_config_from_file';
17 predicate => 'has_configfile',
20 # overridable in consuming class or role to provide a default value
21 # called before instantiation, so must be a class method!
22 sub _get_default_configfile { }
25 my ($class, %opts) = @_;
29 if(defined $opts{configfile}) {
30 $configfile = $opts{configfile}
33 # This would only succeed if the consumer had changed the name of the
34 # reader method, and defined a new configfile sub in its place
35 $configfile = try { to_File($class->configfile) };
37 # this is gross, but since a lot of users have swapped in their own
38 # default subs, we have to keep calling it rather than calling a
39 # builder sub directly - and it might not even be a coderef either
40 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
41 $configfile ||= $cfmeta->default;
43 if (ref $configfile eq 'CODE') {
44 $configfile = $configfile->($class);
47 $configfile ||= $class->_get_default_configfile;
49 $opts{$cfmeta->init_arg} = $configfile if defined $configfile;
52 if (defined $configfile) {
53 my $hash = $class->get_config_from_file($configfile);
55 no warnings 'uninitialized';
56 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
57 unless ref $hash eq 'HASH';
59 %opts = (%$hash, %opts);
73 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
78 ## A real role based on this abstract role:
81 package MooseX::SomeSpecificConfigRole;
84 with 'MooseX::ConfigFromFile';
86 use Some::ConfigFile::Loader ();
88 sub get_config_from_file {
89 my ($class, $file) = @_;
91 my $options_hashref = Some::ConfigFile::Loader->load($file);
93 return $options_hashref;
98 ## A class that uses it:
102 with 'MooseX::SomeSpecificConfigRole';
104 # optionally, default the configfile:
105 sub configfile { '/tmp/foo.yaml' }
107 # ... insert your stuff here ...
110 ## A script that uses the class with a configfile
113 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
117 This is an abstract role which provides an alternate constructor for creating
118 objects using parameters passed in from a configuration file. The
119 actual implementation of reading the configuration file is left to
122 It declares an attribute C<configfile> and a class method C<new_with_config>,
123 and requires that concrete roles derived from it implement the class method
124 C<get_config_from_file>.
126 Attributes specified directly as arguments to C<new_with_config> supercede those
129 L<MooseX::Getopt> knows about this abstract role, and will use it if available
130 to load attributes from the file specified by the commandline flag C<--configfile>
131 during its normal C<new_with_options>.
137 This is a L<Path::Class::File> object which can be coerced from a regular pathname
138 string. This is the file your attributes are loaded from. You can add a default
139 configfile in the class using the role and it will be honored at the appropriate time:
141 has +configfile ( default => '/etc/myapp.yaml' );
143 Note that you can alternately just provide a C<configfile> method which returns
144 the config file when called - this will be used in preference to the default of
149 =head2 new_with_config
151 This is an alternate constructor, which knows to look for the C<configfile> option
152 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
153 C<new_with_options>. Example:
155 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
157 Explicit arguments will overide anything set by the configfile.
159 =head2 get_config_from_file
161 This class method is not implemented in this role, but it is required of all subroles.
162 Its two arguments are the classname and the configfile, and it is expected to return
163 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
167 Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
171 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
177 =item Tomas Doran C<< <bobtfish@bobtfish.net> >> (current maintainer).
179 =item Karen Etheridge
183 =item Zbigniew Lukasiak
189 This library is free software; you can redistribute it and/or modify
190 it under the same terms as Perl itself.