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 { $class->configfile };
31 $configfile ||= $cfmeta->default if $cfmeta->has_default;
34 if (defined $configfile) {
35 my $hash = $class->get_config_from_file($configfile);
37 no warnings 'uninitialized';
38 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
39 unless ref $hash eq 'HASH';
41 %opts = (%$hash, %opts);
55 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
60 ## A real role based on this abstract role:
63 package MooseX::SomeSpecificConfigRole;
66 with 'MooseX::ConfigFromFile';
68 use Some::ConfigFile::Loader ();
70 sub get_config_from_file {
71 my ($class, $file) = @_;
73 my $options_hashref = Some::ConfigFile::Loader->load($file);
75 return $options_hashref;
80 ## A class that uses it:
84 with 'MooseX::SomeSpecificConfigRole';
86 # optionally, default the configfile:
87 sub configfile { '/tmp/foo.yaml' }
89 # ... insert your stuff here ...
92 ## A script that uses the class with a configfile
95 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
99 This is an abstract role which provides an alternate constructor for creating
100 objects using parameters passed in from a configuration file. The
101 actual implementation of reading the configuration file is left to
104 It declares an attribute C<configfile> and a class method C<new_with_config>,
105 and requires that concrete roles derived from it implement the class method
106 C<get_config_from_file>.
108 Attributes specified directly as arguments to C<new_with_config> supercede those
111 L<MooseX::Getopt> knows about this abstract role, and will use it if available
112 to load attributes from the file specified by the commandline flag C<--configfile>
113 during its normal C<new_with_options>.
119 This is a L<Path::Class::File> object which can be coerced from a regular pathname
120 string. This is the file your attributes are loaded from. You can add a default
121 configfile in the class using the role and it will be honored at the appropriate time:
123 has +configfile ( default => '/etc/myapp.yaml' );
127 =head2 new_with_config
129 This is an alternate constructor, which knows to look for the C<configfile> option
130 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
131 C<new_with_options>. Example:
133 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
135 Explicit arguments will overide anything set by the configfile.
137 =head2 get_config_from_file
139 This class method is not implemented in this role, but it is required of all subroles.
140 Its two arguments are the classname and the configfile, and it is expected to return
141 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
145 The Moose meta stuff, included here because otherwise pod tests fail sometimes
151 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
155 This library is free software; you can redistribute it and/or modify
156 it under the same terms as Perl itself.