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