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