1 package MooseX::ConfigFromFile;
4 use MooseX::Types::Path::Class qw( File );
10 requires 'get_config_from_file';
16 predicate => 'has_configfile',
20 my ($class, %opts) = @_;
24 if(defined $opts{configfile}) {
25 $configfile = $opts{configfile}
28 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
29 $configfile = $cfmeta->default if $cfmeta->has_default;
32 if(defined $configfile) {
33 my $hash = $class->get_config_from_file($configfile);
35 no warnings 'uninitialized';
36 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
37 unless ref $hash eq 'HASH';
39 %opts = (%$hash, %opts);
53 MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
58 ## A real role based on this abstract role:
61 package MooseX::SomeSpecificConfigRole;
64 with 'MooseX::ConfigFromFile';
66 use Some::ConfigFile::Loader ();
68 sub get_config_from_file {
69 my ($class, $file) = @_;
71 my $options_hashref = Some::ConfigFile::Loader->load($file);
73 return $options_hashref;
78 ## A class that uses it:
82 with 'MooseX::SomeSpecificConfigRole';
84 # optionally, default the configfile:
85 has +configfile ( default => '/tmp/foo.yaml' );
87 # ... insert your stuff here ...
90 ## A script that uses the class with a configfile
93 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
97 This is an abstract role which provides an alternate constructor for creating
98 objects using parameters passed in from a configuration file. The
99 actual implementation of reading the configuration file is left to
102 It declares an attribute C<configfile> and a class method C<new_with_config>,
103 and requires that concrete roles derived from it implement the class method
104 C<get_config_from_file>.
106 Attributes specified directly as arguments to C<new_with_config> supercede those
109 L<MooseX::Getopt> knows about this abstract role, and will use it if available
110 to load attributes from the file specified by the commandline flag C<--configfile>
111 during its normal C<new_with_options>.
117 This is a L<Path::Class::File> object which can be coerced from a regular pathname
118 string. This is the file your attributes are loaded from. You can add a default
119 configfile in the class using the role and it will be honored at the appropriate time:
121 has +configfile ( default => '/etc/myapp.yaml' );
125 =head2 new_with_config
127 This is an alternate constructor, which knows to look for the C<configfile> option
128 in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
129 C<new_with_options>. Example:
131 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
133 Explicit arguments will overide anything set by the configfile.
135 =head2 get_config_from_file
137 This class method is not implemented in this role, but it is required of all subroles.
138 Its two arguments are the classname and the configfile, and it is expected to return
139 a hashref of arguments to pass to C<new()> which are sourced from the configfile.
143 The Moose meta stuff, included here because otherwise pod tests fail sometimes
149 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
153 This library is free software; you can redistribute it and/or modify
154 it under the same terms as Perl itself.