| 1 | package MooseX::Configuration; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $MooseX::Configuration::VERSION = '0.02'; | |
| 4 | } | ||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use Moose::Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | Moose::Exporter->setup_import_methods( | |
| 12 | class_metaroles => { | ||
| 13 | attribute => ['MooseX::Configuration::Trait::Attribute'], | ||
| 14 | }, | ||
| 15 | base_class_roles => ['MooseX::Configuration::Trait::Object'], | ||
| 16 | ); | ||
| 17 | |||
| 18 | 1 | 1; | |
| 19 | |||
| 20 | # ABSTRACT: Define attributes which come from configuration files | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | =pod | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | MooseX::Configuration - Define attributes which come from configuration files | ||
| 29 | |||
| 30 | =head1 VERSION | ||
| 31 | |||
| 32 | version 0.02 | ||
| 33 | |||
| 34 | =head1 SYNOPSIS | ||
| 35 | |||
| 36 | package MyApp::Config; | ||
| 37 | |||
| 38 | use Moose; | ||
| 39 | use MooseX::Configuration; | ||
| 40 | |||
| 41 | has database_name => ( | ||
| 42 | is => 'ro', | ||
| 43 | isa => 'Str', | ||
| 44 | default => 'MyApp', | ||
| 45 | section => 'database', | ||
| 46 | key => 'name', | ||
| 47 | documentation => | ||
| 48 | 'The name of the database.', | ||
| 49 | ); | ||
| 50 | |||
| 51 | has database_username => ( | ||
| 52 | is => 'ro', | ||
| 53 | isa => Str, | ||
| 54 | default => q{}, | ||
| 55 | section => 'database', | ||
| 56 | key => 'username', | ||
| 57 | documentation => | ||
| 58 | 'The username to use when connecting to the database. By default, this is empty.', | ||
| 59 | ); | ||
| 60 | |||
| 61 | my $config = MyApp::Config->new( config_file => '/path/to/config/myapp.ini' ); | ||
| 62 | $config->write_file( ... ); | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This module lets you define attributes which can come from a configuration | ||
| 67 | file. It also adds a role to your class which allows you to write a | ||
| 68 | configuration file. | ||
| 69 | |||
| 70 | It is based on using a simple INI-style configuration file, which contains | ||
| 71 | sections and keys: | ||
| 72 | |||
| 73 | key1 = value | ||
| 74 | key2 = 42 | ||
| 75 | |||
| 76 | [section] | ||
| 77 | key3 = 2 | ||
| 78 | |||
| 79 | =head1 ATTRIBUTE API | ||
| 80 | |||
| 81 | Simply using this module in your class changes your class's attribute | ||
| 82 | metaclass to add support for defining attributes as configuration items. | ||
| 83 | |||
| 84 | There are two new parameters you can pass when defining an attribute, | ||
| 85 | C<section> and C<key>. These tell the module how to find the attribute's value | ||
| 86 | in the configuration file. The C<section> parameter is optional. If you don't | ||
| 87 | set it, but I<do> provide a key, then the section defaults to C<_>, which is | ||
| 88 | the main section of the config file. | ||
| 89 | |||
| 90 | If you pass a C<section> you must also pass a C<key>. | ||
| 91 | |||
| 92 | Defining an attribute as a configuration item has several effects. First, it | ||
| 93 | changes the default value for the attribute. Before looking at a C<default> or | ||
| 94 | C<builder> you define, the attribute will first look in the config file for a | ||
| 95 | corresponding value. If one exists, it will use that, otherwise it will fall | ||
| 96 | back to using a default you supply. | ||
| 97 | |||
| 98 | If you do supply a default, it must be a string (or number), not a reference | ||
| 99 | or undefined value. | ||
| 100 | |||
| 101 | All configuration attributes are lazy. This is necessary because the | ||
| 102 | configuration file needs to be loaded and parsed before looking up values. | ||
| 103 | |||
| 104 | The C<documentation> string is used when generating a configuration file. See | ||
| 105 | below for details. | ||
| 106 | |||
| 107 | =head1 CLASS API | ||
| 108 | |||
| 109 | Your config class will do the L<MooseX::Configuration::Trait::Object> | ||
| 110 | role. This adds several attributes and methods to your class. | ||
| 111 | |||
| 112 | =head2 config_file attribute | ||
| 113 | |||
| 114 | The C<config_file> attribute defines the location of the configuration | ||
| 115 | file. The role supplies a builder method that you can replace, | ||
| 116 | C<_build_config_file>. It should return a string or L<Path::Class::File> | ||
| 117 | object pointing to the configuration file. It can also return C<undef>. | ||
| 118 | |||
| 119 | If you I<don't> provide your own builder, then the C<config_file> will default | ||
| 120 | to C<undef>. | ||
| 121 | |||
| 122 | =head2 $config->_raw_config() | ||
| 123 | |||
| 124 | This returns the raw hash reference as read by L<Config::INI::Reader>. If no | ||
| 125 | config file was defined, then this simply returns an empty hash reference. | ||
| 126 | |||
| 127 | =head2 $config->write_config_file( ... ) | ||
| 128 | |||
| 129 | This method can be used to write a configuration file. It accepts several | ||
| 130 | parameters: | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item * file | ||
| 135 | |||
| 136 | This can be either a path or an open filehandle. The configuration text will | ||
| 137 | be written to this file. This defaults to the value of C<< | ||
| 138 | $self->config_file() >>. If no file is provided or already set in the object, | ||
| 139 | this method will die. | ||
| 140 | |||
| 141 | =item * generated_by | ||
| 142 | |||
| 143 | If this parameter is passed, it will be included as a comment at the top of | ||
| 144 | the generated file. | ||
| 145 | |||
| 146 | =item * values | ||
| 147 | |||
| 148 | This should be a hash reference of attribute names and values to write to the | ||
| 149 | config file. It is optional. | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | When writing the configuration file, any configuration item that was set in | ||
| 154 | the configuration file originally will be set in the new file, as will any | ||
| 155 | value passed in the C<values> key. An attribute value set in the constructor | ||
| 156 | or by a default will I<not> be included in the generated file. | ||
| 157 | |||
| 158 | Keys without a value will still be included in the file as a comment. | ||
| 159 | |||
| 160 | If an attribute includes a documentation string, that string will appear as a | ||
| 161 | comment above the key. If the attribute defines a simple scalar default, that | ||
| 162 | will also be included in the comment, unless the default is the empty | ||
| 163 | string. Finally, if the attribute is required, that is also mentioned in the | ||
| 164 | comment. | ||
| 165 | |||
| 166 | =head1 DONATIONS | ||
| 167 | |||
| 168 | If you'd like to thank me for the work I've done on this module, please | ||
| 169 | consider making a "donation" to me via PayPal. I spend a lot of free time | ||
| 170 | creating free software, and would appreciate any support you'd care to offer. | ||
| 171 | |||
| 172 | Please note that B<I am not suggesting that you must do this> in order for me | ||
| 173 | to continue working on this particular software. I will continue to do so, | ||
| 174 | inasmuch as I have in the past, for as long as it interests me. | ||
| 175 | |||
| 176 | Similarly, a donation made in this way will probably not make me work on this | ||
| 177 | software much more, unless I get so many donations that I can consider working | ||
| 178 | on free software full time, which seems unlikely at best. | ||
| 179 | |||
| 180 | To donate, log into PayPal and send money to autarch@urth.org or use the | ||
| 181 | button on this page: L<http://www.urth.org/~autarch/fs-donation.html> | ||
| 182 | |||
| 183 | =head1 BUGS | ||
| 184 | |||
| 185 | Please report any bugs or feature requests to | ||
| 186 | C<bug-moosex-configuration@rt.cpan.org>, or through the web interface at | ||
| 187 | L<http://rt.cpan.org>. I will be notified, and then you'll automatically be | ||
| 188 | notified of progress on your bug as I make changes. | ||
| 189 | |||
| 190 | =head1 AUTHOR | ||
| 191 | |||
| 192 | Dave Rolsky <autarch@urth.org> | ||
| 193 | |||
| 194 | =head1 COPYRIGHT AND LICENSE | ||
| 195 | |||
| 196 | This software is Copyright (c) 2010 by Dave Rolsky. | ||
| 197 | |||
| 198 | This is free software, licensed under: | ||
| 199 | |||
| 200 | The Artistic License 2.0 | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | |||
| 205 | __END__ | ||