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