prep for release
[gitmo/MooseX-ConfigFromFile.git] / lib / MooseX / ConfigFromFile.pm
1 package MooseX::ConfigFromFile;
2
3 use Moose::Role;
4 use MooseX::Types::Path::Tiny 'Path';
5 use Try::Tiny qw/ try /;
6 use Carp qw(croak);
7 use namespace::autoclean;
8
9 our $VERSION = '0.07';
10
11 requires 'get_config_from_file';
12
13 has configfile => (
14     is => 'ro',
15     isa => Path,
16     coerce => 1,
17     predicate => 'has_configfile',
18     do { try { require MooseX::Getopt; (traits => ['Getopt']) } },
19 );
20
21 sub new_with_config {
22     my ($class, %opts) = @_;
23
24     my $configfile;
25
26     if(defined $opts{configfile}) {
27         $configfile = $opts{configfile}
28     }
29     else {
30         # This would only succeed if the consumer had defined a new configfile
31         # sub to override the generated reader
32         $configfile = try { $class->configfile };
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');
38         $configfile ||= $cfmeta->default if $cfmeta->has_default;
39
40         if (ref $configfile eq 'CODE') {
41             $configfile = $configfile->($class);
42         }
43     }
44
45     if (defined $configfile) {
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);
53     }
54
55     $class->new(%opts);
56 }
57
58 no Moose::Role; 1;
59
60 __END__
61
62 =pod
63
64 =head1 NAME
65
66 MooseX::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
97   # optionally, default the configfile:
98   around configfile => sub { '/tmp/foo.yaml' };
99
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
110 This is an abstract role which provides an alternate constructor for creating 
111 objects using parameters passed in from a configuration file.  The
112 actual implementation of reading the configuration file is left to
113 concrete subroles.
114
115 It declares an attribute C<configfile> and a class method C<new_with_config>,
116 and requires that concrete roles derived from it implement the class method
117 C<get_config_from_file>.
118
119 Attributes specified directly as arguments to C<new_with_config> supercede those
120 in the configfile.
121
122 L<MooseX::Getopt> knows about this abstract role, and will use it if available
123 to load attributes from the file specified by the commandline flag C<--configfile>
124 during its normal C<new_with_options>.
125
126 =head1 Attributes
127
128 =head2 configfile
129
130 This is a L<Path::Tiny> object which can be coerced from a regular pathname
131 string or any object that supports stringification.
132 This is the file your attributes are loaded from.  You can add a default
133 configfile in the consuming class and it will be honored at the appropriate time
134 (note that a simple sub declaration is not sufficient, as there is already a
135 sub by that name being added by Moose as the attribute reader)
136
137   around configfile => sub { '/etc/myapp.yaml' };
138
139 Note that you can alternately just provide a C<configfile> method which returns
140 the config file when called - this will be used in preference to the default of
141 the attribute.
142
143 If you have L<MooseX::Getopt> installed, this attribute will also have the
144 C<Getopt> trait supplied, so you can also set the configfile from the
145 command line.
146
147 =head1 Class Methods
148
149 =head2 new_with_config
150
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:
154
155   my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
156
157 Explicit arguments will overide anything set by the configfile.
158
159 =head2 get_config_from_file
160
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.
164
165 =head1 COPYRIGHT
166
167 Copyright (c) - the MooseX::ConfigFromFile "AUTHOR" and "CONTRIBUTORS" as listed below.
168
169 =head1 AUTHOR
170
171 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
172
173 =head1 CONTRIBUTORS
174
175 =over
176
177 =item Tomas Doran
178
179 =item Karen Etheridge
180
181 =item Chris Prather
182
183 =item Zbigniew Lukasiak
184
185 =back
186
187 =head1 LICENSE
188
189 This library is free software; you can redistribute it and/or modify
190 it under the same terms as Perl itself.
191
192 =cut