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