avoid shipping my todo file
[gitmo/MooseX-ConfigFromFile.git] / lib / MooseX / ConfigFromFile.pm
CommitLineData
c35b639e 1package MooseX::ConfigFromFile;
2
3use Moose::Role;
4use MooseX::Types::Path::Class qw( File );
0e88ec88 5use Try::Tiny qw/ try /;
870afbfa 6use Carp qw(croak);
bc5ab785 7use namespace::autoclean;
8
22b28fb3 9our $VERSION = '0.04';
870afbfa 10
c35b639e 11requires 'get_config_from_file';
12
13has configfile => (
14 is => 'ro',
15 isa => File,
16 coerce => 1,
17 predicate => 'has_configfile',
742b859c 18 do { try { require MooseX::Getopt; (traits => ['Getopt']) } },
c35b639e 19);
20
21sub new_with_config {
22 my ($class, %opts) = @_;
23
fc44be6f 24 my $configfile;
25
26 if(defined $opts{configfile}) {
27 $configfile = $opts{configfile}
28 }
29 else {
7acf2fe9 30 # This would only succeed if the consumer had defined a new configfile
31 # sub to override the generated reader
f1040206 32 $configfile = try { $class->configfile };
7acf2fe9 33
34 # this is gross, but since a lot of users have swapped in their own
35 # default subs, we have to keep calling it rather than calling a
36 # builder sub directly - and it might not even be a coderef either
37 my $cfmeta = $class->meta->find_attribute_by_name('configfile');
0e88ec88 38 $configfile ||= $cfmeta->default if $cfmeta->has_default;
7acf2fe9 39
56e4351b 40 if (ref $configfile eq 'CODE') {
83f0ce54 41 $configfile = $configfile->($class);
56e4351b 42 }
fc44be6f 43 }
44
0e88ec88 45 if (defined $configfile) {
870afbfa 46 my $hash = $class->get_config_from_file($configfile);
47
48 no warnings 'uninitialized';
49 croak "get_config_from_file($configfile) did not return a hash (got $hash)"
50 unless ref $hash eq 'HASH';
51
52 %opts = (%$hash, %opts);
c35b639e 53 }
fc44be6f 54
c35b639e 55 $class->new(%opts);
56}
57
58no Moose::Role; 1;
59
60__END__
61
62=pod
63
64=head1 NAME
65
66MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
67
68=head1 SYNOPSIS
69
70 ########
71 ## A real role based on this abstract role:
72 ########
73
74 package MooseX::SomeSpecificConfigRole;
75 use Moose::Role;
76
77 with 'MooseX::ConfigFromFile';
78
79 use Some::ConfigFile::Loader ();
80
81 sub get_config_from_file {
82 my ($class, $file) = @_;
83
84 my $options_hashref = Some::ConfigFile::Loader->load($file);
85
86 return $options_hashref;
87 }
88
89
90 ########
91 ## A class that uses it:
92 ########
93 package Foo;
94 use Moose;
95 with 'MooseX::SomeSpecificConfigRole';
96
fc44be6f 97 # optionally, default the configfile:
7acf2fe9 98 around configfile => sub { '/tmp/foo.yaml' };
fc44be6f 99
c35b639e 100 # ... insert your stuff here ...
101
102 ########
103 ## A script that uses the class with a configfile
104 ########
105
106 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
107
108=head1 DESCRIPTION
109
110This is an abstract role which provides an alternate constructor for creating
111objects using parameters passed in from a configuration file. The
112actual implementation of reading the configuration file is left to
113concrete subroles.
114
115It declares an attribute C<configfile> and a class method C<new_with_config>,
116and requires that concrete roles derived from it implement the class method
117C<get_config_from_file>.
118
fc44be6f 119Attributes specified directly as arguments to C<new_with_config> supercede those
120in the configfile.
121
c35b639e 122L<MooseX::Getopt> knows about this abstract role, and will use it if available
123to load attributes from the file specified by the commandline flag C<--configfile>
124during its normal C<new_with_options>.
125
126=head1 Attributes
127
128=head2 configfile
129
130This is a L<Path::Class::File> object which can be coerced from a regular pathname
fc44be6f 131string. This is the file your attributes are loaded from. You can add a default
7acf2fe9 132configfile in the consuming class and it will be honored at the appropriate time
133(note that a simple sub declaration is not sufficient, as there is already a
134sub by that name being added by Moose as the attribute reader)
fc44be6f 135
7acf2fe9 136 around configfile => sub { '/etc/myapp.yaml' };
c35b639e 137
4d0a4f55 138Note that you can alternately just provide a C<configfile> method which returns
139the config file when called - this will be used in preference to the default of
140the attribute.
141
742b859c 142If you have L<MooseX::Getopt> installed, this attribute will also have the
143C<Getopt> trait supplied, so you can also set the configfile from the
144command line.
145
c35b639e 146=head1 Class Methods
147
148=head2 new_with_config
149
150This is an alternate constructor, which knows to look for the C<configfile> option
151in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
152C<new_with_options>. Example:
153
154 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
155
156Explicit arguments will overide anything set by the configfile.
157
158=head2 get_config_from_file
159
160This class method is not implemented in this role, but it is required of all subroles.
161Its two arguments are the classname and the configfile, and it is expected to return
fc44be6f 162a hashref of arguments to pass to C<new()> which are sourced from the configfile.
c35b639e 163
4d0a4f55 164=head1 COPYRIGHT
c35b639e 165
4d0a4f55 166Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
c35b639e 167
168=head1 AUTHOR
169
170Brandon L. Black, E<lt>blblack@gmail.comE<gt>
171
4d0a4f55 172=head1 CONTRIBUTORS
173
174=over
175
176=item Tomas Doran C<< <bobtfish@bobtfish.net> >> (current maintainer).
177
178=item Karen Etheridge
179
180=item Chris Prather
181
182=item Zbigniew Lukasiak
183
184=back
185
c35b639e 186=head1 LICENSE
187
188This library is free software; you can redistribute it and/or modify
189it under the same terms as Perl itself.
190
191=cut