0.02, use default configfile
[gitmo/MooseX-ConfigFromFile.git] / lib / MooseX / ConfigFromFile.pm
CommitLineData
c35b639e 1package MooseX::ConfigFromFile;
2
3use Moose::Role;
4use MooseX::Types::Path::Class qw( File );
5
fc44be6f 6our $VERSION = '0.02';
c35b639e 7
8requires 'get_config_from_file';
9
10has configfile => (
11 is => 'ro',
12 isa => File,
13 coerce => 1,
14 predicate => 'has_configfile',
15);
16
17sub new_with_config {
18 my ($class, %opts) = @_;
19
fc44be6f 20 my $configfile;
21
22 if(defined $opts{configfile}) {
23 $configfile = $opts{configfile}
24 }
25 else {
26 my $cfmeta = $class->meta->get_attribute('configfile');
27 $configfile = $cfmeta->default if $cfmeta->has_default;
28 }
29
30 if(defined $configfile) {
31 %opts = (%{$class->get_config_from_file($configfile)}, %opts);
c35b639e 32 }
fc44be6f 33
c35b639e 34 $class->new(%opts);
35}
36
37no Moose::Role; 1;
38
39__END__
40
41=pod
42
43=head1 NAME
44
45MooseX::ConfigFromFile - An abstract Moose role for setting attributes from a configfile
46
47=head1 SYNOPSIS
48
49 ########
50 ## A real role based on this abstract role:
51 ########
52
53 package MooseX::SomeSpecificConfigRole;
54 use Moose::Role;
55
56 with 'MooseX::ConfigFromFile';
57
58 use Some::ConfigFile::Loader ();
59
60 sub get_config_from_file {
61 my ($class, $file) = @_;
62
63 my $options_hashref = Some::ConfigFile::Loader->load($file);
64
65 return $options_hashref;
66 }
67
68
69 ########
70 ## A class that uses it:
71 ########
72 package Foo;
73 use Moose;
74 with 'MooseX::SomeSpecificConfigRole';
75
fc44be6f 76 # optionally, default the configfile:
77 has +configfile ( default => '/tmp/foo.yaml' );
78
c35b639e 79 # ... insert your stuff here ...
80
81 ########
82 ## A script that uses the class with a configfile
83 ########
84
85 my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
86
87=head1 DESCRIPTION
88
89This is an abstract role which provides an alternate constructor for creating
90objects using parameters passed in from a configuration file. The
91actual implementation of reading the configuration file is left to
92concrete subroles.
93
94It declares an attribute C<configfile> and a class method C<new_with_config>,
95and requires that concrete roles derived from it implement the class method
96C<get_config_from_file>.
97
fc44be6f 98Attributes specified directly as arguments to C<new_with_config> supercede those
99in the configfile.
100
c35b639e 101L<MooseX::Getopt> knows about this abstract role, and will use it if available
102to load attributes from the file specified by the commandline flag C<--configfile>
103during its normal C<new_with_options>.
104
105=head1 Attributes
106
107=head2 configfile
108
109This is a L<Path::Class::File> object which can be coerced from a regular pathname
fc44be6f 110string. This is the file your attributes are loaded from. You can add a default
111configfile in the class using the role and it will be honored at the appropriate time:
112
113 has +configfile ( default => '/etc/myapp.yaml' );
c35b639e 114
115=head1 Class Methods
116
117=head2 new_with_config
118
119This is an alternate constructor, which knows to look for the C<configfile> option
120in its arguments and use that to set attributes. It is much like L<MooseX::Getopts>'s
121C<new_with_options>. Example:
122
123 my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
124
125Explicit arguments will overide anything set by the configfile.
126
127=head2 get_config_from_file
128
129This class method is not implemented in this role, but it is required of all subroles.
130Its two arguments are the classname and the configfile, and it is expected to return
fc44be6f 131a hashref of arguments to pass to C<new()> which are sourced from the configfile.
c35b639e 132
133=head2 meta
134
135The Moose meta stuff, included here because otherwise pod tests fail sometimes
136
137=head1 BUGS
138
139=head1 AUTHOR
140
141Brandon L. Black, E<lt>blblack@gmail.comE<gt>
142
143=head1 LICENSE
144
145This library is free software; you can redistribute it and/or modify
146it under the same terms as Perl itself.
147
148=cut