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',
21 my ($class, %opts) = @_;
25 if(defined $opts{configfile}) {
26 $configfile = $opts{configfile}
29 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
30 $configfile = try { to_File($class->configfile) };
31 $configfile ||= $cfmeta->default if $cfmeta->has_default;
32 if (ref $configfile eq 'CODE') {
33 $configfile = &$configfile($class);
37 if (defined $configfile) {
38 my $hash = $class->get_config_from_file($configfile);
40 no warnings 'uninitialized';
41 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
42 unless ref $hash eq 'HASH';
44 %opts = (%$hash, %opts);
58 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
63 ## A real role based on this abstract role:
66 package MooseX::SomeSpecificConfigRole;
69 with 'MooseX::ConfigFromFile';
71 use Some::ConfigFile::Loader ();
73 sub get_config_from_file {
74 my ($class, $file) = @_;
76 my $options_hashref = Some::ConfigFile::Loader->load($file);
78 return $options_hashref;
83 ## A class that uses it:
87 with 'MooseX::SomeSpecificConfigRole';
89 # optionally, default the configfile:
90 sub configfile { '/tmp/foo.yaml' }
92 # ... insert your stuff here ...
95 ## A script that uses the class with a configfile
98 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
102 This is an abstract role which provides an alternate constructor for creating
103 objects using parameters passed in from a configuration file. The
104 actual implementation of reading the configuration file is left to
107 It declares an attribute C<configfile> and a class method C<new_with_config>,
108 and requires that concrete roles derived from it implement the class method
109 C<get_config_from_file>.
111 Attributes specified directly as arguments to C<new_with_config> supercede those
114 L<MooseX::Getopt> knows about this abstract role, and will use it if available
115 to load attributes from the file specified by the commandline flag C<--configfile>
116 during its normal C<new_with_options>.
122 This is a L<Path::Class::File> object which can be coerced from a regular pathname
123 string. This is the file your attributes are loaded from. You can add a default
124 configfile in the class using the role and it will be honored at the appropriate time:
126 has +configfile ( default => '/etc/myapp.yaml' );
128 Note that you can alternately just provide a C<configfile> method which returns
129 the config file when called - this will be used in preference to the default of
134 =head2 new_with_config
136 This is an alternate constructor, which knows to look for the C<configfile> option
137 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
138 C<new_with_options>. Example:
140 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
142 Explicit arguments will overide anything set by the configfile.
144 =head2 get_config_from_file
146 This class method is not implemented in this role, but it is required of all subroles.
147 Its two arguments are the classname and the configfile, and it is expected to return
148 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
152 Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
156 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
162 =item Tomas Doran C<< <bobtfish@bobtfish.net> >> (current maintainer).
164 =item Karen Etheridge
168 =item Zbigniew Lukasiak
174 This library is free software; you can redistribute it and/or modify
175 it under the same terms as Perl itself.