| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package Config::Role; | ||||||||||
| 5 | { | ||||||||||
| 6 | 2 | $Config::Role::VERSION = '0.1.1'; | |||||||||
| 7 | } | ||||||||||
| 8 | 2 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: Moose config attribute loaded from file in home dir | ||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use File::HomeDir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use Path::Class::Dir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use Path::Class::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 2 | ▶ | use Config::Any; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 2 | ▶ | use MooseX::Types::Moose qw(ArrayRef HashRef Str Object); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | use MooseX::Types -declare => [qw( Dir File ArrayRefOfFile )]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | subtype Dir, | ||||||||||
| 21 | as Object, | ||||||||||
| 22 | 4 | ▶ | where { $_->isa('Path::Class::Dir') }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 23 | coerce Dir, | ||||||||||
| 24 | from Str, | ||||||||||
| 25 | 0 | ▶ | via { Path::Class::Dir->new($_) }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||
| 26 | subtype File, | ||||||||||
| 27 | as Object, | ||||||||||
| 28 | 15 | ▶ | where { $_->isa('Path::Class::File') }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 29 | coerce File, | ||||||||||
| 30 | from Str, | ||||||||||
| 31 | 3 | ▶ | via { Path::Class::File->new($_) }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 2 | subtype ArrayRefOfFile, | |||||||||
| 33 | as ArrayRef[File]; | ||||||||||
| 34 | coerce ArrayRefOfFile, | ||||||||||
| 35 | from ArrayRef[Str], | ||||||||||
| 36 | 1 | ▶ | via { [ map { to_File($_) } @$_ ] }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | 2 | has 'config_dir' => ( | |||||||||
| 41 | is => 'ro', | ||||||||||
| 42 | isa => Dir, | ||||||||||
| 43 | coerce => 1, | ||||||||||
| 44 | lazy_build => 1, | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | 2 | ▶ | sub _build_config_dir { | ||||||||
|
Subroutine
_build_config_dir: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 2 | my ($self) = @_; | |||||||||
| 49 | 2 | return Path::Class::Dir->new(File::HomeDir->my_data); | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 2 | has 'config_file' => ( | |||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | isa => File, | ||||||||||
| 56 | coerce => 1, | ||||||||||
| 57 | lazy_build => 1, | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | 2 | ▶ | sub _build_config_file { | ||||||||
|
Subroutine
_build_config_file: called CC=2 SCAR=6.9
| |||||||||||
| 61 | 2 | my ($self) = @_; | |||||||||
| 62 | 2 | my $config_filename = ""; | |||||||||
| 63 | 2 | ▶ | if ( $self->can('config_filename') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | $config_filename = $self->config_filename; | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | # Method taken from Catalyst::Utils->appprefix() | ||||||||||
| 68 | 1 | $config_filename = lc( $self->meta->name ); | |||||||||
| 69 | 1 | $config_filename =~ s/::/_/g; | |||||||||
| 70 | 1 | $config_filename = ".${config_filename}.ini"; | |||||||||
| 71 | } | ||||||||||
| 72 | 2 | return $self->config_dir->file( | |||||||||
| 73 | $config_filename | ||||||||||
| 74 | ); | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | |||||||||||
| 78 | 2 | has 'config_files' => ( | |||||||||
| 79 | is => 'ro', | ||||||||||
| 80 | isa => ArrayRefOfFile, | ||||||||||
| 81 | coerce => 1, | ||||||||||
| 82 | lazy_build => 1, | ||||||||||
| 83 | ); | ||||||||||
| 84 | 3 | ▶ | sub _build_config_files { | ||||||||
|
Subroutine
_build_config_files: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 3 | my ($self) = @_; | |||||||||
| 86 | 3 | return [ $self->config_file ]; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | 2 | has 'config' => ( | |||||||||
| 91 | is => 'ro', | ||||||||||
| 92 | isa => HashRef, | ||||||||||
| 93 | lazy_build => 1, | ||||||||||
| 94 | ); | ||||||||||
| 95 | |||||||||||
| 96 | 4 | ▶ | sub _build_config { | ||||||||
|
Subroutine
_build_config: called CC=2 SCAR=6.9
| |||||||||||
| 97 | 4 | my ($self) = @_; | |||||||||
| 98 | 4 | my $cfg = Config::Any->load_files({ | |||||||||
| 99 | use_ext => 1, | ||||||||||
| 100 | files => $self->config_files, | ||||||||||
| 101 | }); | ||||||||||
| 102 | 4 | foreach my $config_entry ( @{ $cfg } ) { | |||||||||
| 103 | 3 | my ($filename, $config) = %{ $config_entry }; | |||||||||
| 104 | 3 | return $config; | |||||||||
| 105 | } | ||||||||||
| 106 | 1 | return {}; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 2 | 1; | |||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | =pod | ||||||||||
| 114 | |||||||||||
| 115 | =encoding utf-8 | ||||||||||
| 116 | |||||||||||
| 117 | =head1 NAME | ||||||||||
| 118 | |||||||||||
| 119 | Config::Role - Moose config attribute loaded from file in home dir | ||||||||||
| 120 | |||||||||||
| 121 | =head1 VERSION | ||||||||||
| 122 | |||||||||||
| 123 | version 0.1.1 | ||||||||||
| 124 | |||||||||||
| 125 | =head1 SYNOPSIS | ||||||||||
| 126 | |||||||||||
| 127 | package My::Class; | ||||||||||
| 128 | use Moose; | ||||||||||
| 129 | with 'Config::Role'; | ||||||||||
| 130 | |||||||||||
| 131 | # Read configuration from ~/.my_class.ini, available in $self->config | ||||||||||
| 132 | # This is optional if you like this particular naming of the file | ||||||||||
| 133 | has 'config_filename' => ( is => 'ro', isa => 'Str', lazy_build => 1 ); | ||||||||||
| 134 | sub _build_config_filename { '.my_class.ini' } | ||||||||||
| 135 | |||||||||||
| 136 | # Fetch a value from the configuration, allow constructor override | ||||||||||
| 137 | has 'username' => ( is => 'ro', isa => 'Str', lazy_build => 1 ); | ||||||||||
| 138 | sub _build_username { return (shift)->config->{'username'}; } | ||||||||||
| 139 | |||||||||||
| 140 | sub make_request { | ||||||||||
| 141 | my ($self) = @_; | ||||||||||
| 142 | my $response = My::Class::Request->make( | ||||||||||
| 143 | username => $self->username, | ||||||||||
| 144 | ... | ||||||||||
| 145 | ); | ||||||||||
| 146 | ... | ||||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | =head1 RATIONALE | ||||||||||
| 150 | |||||||||||
| 151 | This is the problem Config::Role was created to solve: Give me a config | ||||||||||
| 152 | attribute (hashref) which is read from a file in my home directory to give | ||||||||||
| 153 | other attributes default values, with configurability to choose the file's | ||||||||||
| 154 | location and name. | ||||||||||
| 155 | |||||||||||
| 156 | =head1 DESCRIPTION | ||||||||||
| 157 | |||||||||||
| 158 | Config::Role is a very basic role you can add to your Moose class that | ||||||||||
| 159 | allows it to take configuration data from a file located in your home | ||||||||||
| 160 | directory instead of always requiring parameters to be specified in the | ||||||||||
| 161 | constructor. | ||||||||||
| 162 | |||||||||||
| 163 | The synopsis shows how you can read the value of C<username> from the file | ||||||||||
| 164 | C<.my_class.ini> located in the home directory of the current user. The | ||||||||||
| 165 | location of the file is determined by whatever C<< File::HomeDir->my_data >> | ||||||||||
| 166 | returns for your particular platform. | ||||||||||
| 167 | |||||||||||
| 168 | The config file is loaded by using L<Config::Any>'s C<load_files()> method. | ||||||||||
| 169 | It will load the files specified in the C<config_files> attribute. By | ||||||||||
| 170 | default this is an array reference that contains the filename from the | ||||||||||
| 171 | C<config_file> attribute. If you specify multiple files which both contain | ||||||||||
| 172 | the same configuration key, the value is loaded from the first file. That | ||||||||||
| 173 | is, the most significant file should be first in the array. | ||||||||||
| 174 | |||||||||||
| 175 | The C<< Config::Any->load_files() >> flag C<use_ext> is set to a true value, so you | ||||||||||
| 176 | can use any configuration file format supported by L<Config::Any> by just | ||||||||||
| 177 | specifying the common filename extension for the format. | ||||||||||
| 178 | |||||||||||
| 179 | =head1 ATTRIBUTES | ||||||||||
| 180 | |||||||||||
| 181 | =head2 config_dir | ||||||||||
| 182 | |||||||||||
| 183 | The directory where the configuration file is located. A Path::Class::Dir | ||||||||||
| 184 | object. Defaults to C<< File::HomeDir->my_data >>. Allows coercion from | ||||||||||
| 185 | Str. | ||||||||||
| 186 | |||||||||||
| 187 | =head2 config_file | ||||||||||
| 188 | |||||||||||
| 189 | The filename the configuration is read from. A Path::Class::File object. | ||||||||||
| 190 | Allows coercion from Str. Default is calculated based on the composing | ||||||||||
| 191 | class name. If your composing class is called C<My::Class> it will be | ||||||||||
| 192 | C<.my_class.ini>. Remember that if you sub-class the composing class, the | ||||||||||
| 193 | default will be the name of the sub-class, not the super-class. | ||||||||||
| 194 | |||||||||||
| 195 | =head2 config_files | ||||||||||
| 196 | |||||||||||
| 197 | The collection of filenames the configuration is read from. Array reference | ||||||||||
| 198 | of L<Path::Class::File> objects. Allows coercion from an array reference of | ||||||||||
| 199 | strings. | ||||||||||
| 200 | |||||||||||
| 201 | =head2 config | ||||||||||
| 202 | |||||||||||
| 203 | A hash reference that holds the compiled configuration read from the | ||||||||||
| 204 | specified files. | ||||||||||
| 205 | |||||||||||
| 206 | =head1 METHODS | ||||||||||
| 207 | |||||||||||
| 208 | =head2 config_filename | ||||||||||
| 209 | |||||||||||
| 210 | Optional attribute or method on the composing class. Should return a string | ||||||||||
| 211 | with the name of the configuration file name. See C<config_file> for | ||||||||||
| 212 | how the default is calculated if this method is not available. | ||||||||||
| 213 | |||||||||||
| 214 | =head1 COMPARISON TO L<MooseX::ConfigFromFile> | ||||||||||
| 215 | |||||||||||
| 216 | Config::Role doesn't require you to use anything else than C<< $class->new() >> to | ||||||||||
| 217 | actually get the benefit of automatic config loading. Someone might see this as | ||||||||||
| 218 | negative, as it gives a minor performance penalty even if the config file is | ||||||||||
| 219 | not present. | ||||||||||
| 220 | |||||||||||
| 221 | Config::Role uses L<File::HomeDir> to default to a known location, so you | ||||||||||
| 222 | only need to specify the file name you use, not a full path. This should | ||||||||||
| 223 | give better cross-platform compatibility, together with the use of | ||||||||||
| 224 | Path::Class for all file system manipulation. | ||||||||||
| 225 | |||||||||||
| 226 | Also, with Config::Role you must explicitly specify in the builder of an | ||||||||||
| 227 | attribute that you want to use values from the config file. | ||||||||||
| 228 | MooseX::ConfigFromFile seems to do that for you. You also get the benefit | ||||||||||
| 229 | that the configuration file keys and the class attribute names does not need | ||||||||||
| 230 | to map 1-to-1 (someone will probably see that as a bad thing). | ||||||||||
| 231 | |||||||||||
| 232 | Otherwise they are pretty similar in terms of what they do. | ||||||||||
| 233 | |||||||||||
| 234 | =head1 TODO | ||||||||||
| 235 | |||||||||||
| 236 | =over 4 | ||||||||||
| 237 | |||||||||||
| 238 | =item * | ||||||||||
| 239 | |||||||||||
| 240 | A nicely named sugar function could be exported to allow less boilerplate | ||||||||||
| 241 | in generating attributes that default to config values. | ||||||||||
| 242 | |||||||||||
| 243 | =back | ||||||||||
| 244 | |||||||||||
| 245 | =head1 SEMANTIC VERSIONING | ||||||||||
| 246 | |||||||||||
| 247 | This module uses semantic versioning concepts from L<http://semver.org/>. | ||||||||||
| 248 | |||||||||||
| 249 | =head1 SEE ALSO | ||||||||||
| 250 | |||||||||||
| 251 | =over 4 | ||||||||||
| 252 | |||||||||||
| 253 | =item * | ||||||||||
| 254 | |||||||||||
| 255 | L<Moose> | ||||||||||
| 256 | |||||||||||
| 257 | =item * | ||||||||||
| 258 | |||||||||||
| 259 | L<File::HomeDir> | ||||||||||
| 260 | |||||||||||
| 261 | =item * | ||||||||||
| 262 | |||||||||||
| 263 | L<Config::Any> | ||||||||||
| 264 | |||||||||||
| 265 | =item * | ||||||||||
| 266 | |||||||||||
| 267 | L<Path::Class::File> | ||||||||||
| 268 | |||||||||||
| 269 | =item * | ||||||||||
| 270 | |||||||||||
| 271 | L<MooseX::ConfigFromFile> | ||||||||||
| 272 | |||||||||||
| 273 | =back | ||||||||||
| 274 | |||||||||||
| 275 | =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders | ||||||||||
| 276 | |||||||||||
| 277 | =head1 SUPPORT | ||||||||||
| 278 | |||||||||||
| 279 | =head2 Perldoc | ||||||||||
| 280 | |||||||||||
| 281 | You can find documentation for this module with the perldoc command. | ||||||||||
| 282 | |||||||||||
| 283 | perldoc Config::Role | ||||||||||
| 284 | |||||||||||
| 285 | =head2 Websites | ||||||||||
| 286 | |||||||||||
| 287 | The following websites have more information about this module, and may be of help to you. As always, | ||||||||||
| 288 | in addition to those websites please use your favorite search engine to discover more resources. | ||||||||||
| 289 | |||||||||||
| 290 | =over 4 | ||||||||||
| 291 | |||||||||||
| 292 | =item * | ||||||||||
| 293 | |||||||||||
| 294 | Search CPAN | ||||||||||
| 295 | |||||||||||
| 296 | The default CPAN search engine, useful to view POD in HTML format. | ||||||||||
| 297 | |||||||||||
| 298 | L<http://search.cpan.org/dist/Config-Role> | ||||||||||
| 299 | |||||||||||
| 300 | =item * | ||||||||||
| 301 | |||||||||||
| 302 | RT: CPAN's Bug Tracker | ||||||||||
| 303 | |||||||||||
| 304 | The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. | ||||||||||
| 305 | |||||||||||
| 306 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Config-Role> | ||||||||||
| 307 | |||||||||||
| 308 | =item * | ||||||||||
| 309 | |||||||||||
| 310 | AnnoCPAN | ||||||||||
| 311 | |||||||||||
| 312 | The AnnoCPAN is a website that allows community annonations of Perl module documentation. | ||||||||||
| 313 | |||||||||||
| 314 | L<http://annocpan.org/dist/Config-Role> | ||||||||||
| 315 | |||||||||||
| 316 | =item * | ||||||||||
| 317 | |||||||||||
| 318 | CPAN Ratings | ||||||||||
| 319 | |||||||||||
| 320 | The CPAN Ratings is a website that allows community ratings and reviews of Perl modules. | ||||||||||
| 321 | |||||||||||
| 322 | L<http://cpanratings.perl.org/d/Config-Role> | ||||||||||
| 323 | |||||||||||
| 324 | =item * | ||||||||||
| 325 | |||||||||||
| 326 | CPAN Forum | ||||||||||
| 327 | |||||||||||
| 328 | The CPAN Forum is a web forum for discussing Perl modules. | ||||||||||
| 329 | |||||||||||
| 330 | L<http://cpanforum.com/dist/Config-Role> | ||||||||||
| 331 | |||||||||||
| 332 | =item * | ||||||||||
| 333 | |||||||||||
| 334 | CPANTS | ||||||||||
| 335 | |||||||||||
| 336 | The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. | ||||||||||
| 337 | |||||||||||
| 338 | L<http://cpants.perl.org/dist/overview/Config-Role> | ||||||||||
| 339 | |||||||||||
| 340 | =item * | ||||||||||
| 341 | |||||||||||
| 342 | CPAN Testers | ||||||||||
| 343 | |||||||||||
| 344 | The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions. | ||||||||||
| 345 | |||||||||||
| 346 | L<http://www.cpantesters.org/distro/C/Config-Role> | ||||||||||
| 347 | |||||||||||
| 348 | =item * | ||||||||||
| 349 | |||||||||||
| 350 | CPAN Testers Matrix | ||||||||||
| 351 | |||||||||||
| 352 | The CPAN Testers Matrix is a website that provides a visual way to determine what Perls/platforms PASSed for a distribution. | ||||||||||
| 353 | |||||||||||
| 354 | L<http://matrix.cpantesters.org/?dist=Config-Role> | ||||||||||
| 355 | |||||||||||
| 356 | =item * | ||||||||||
| 357 | |||||||||||
| 358 | CPAN Testers Dependencies | ||||||||||
| 359 | |||||||||||
| 360 | The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. | ||||||||||
| 361 | |||||||||||
| 362 | L<http://deps.cpantesters.org/?module=Config::Role> | ||||||||||
| 363 | |||||||||||
| 364 | =back | ||||||||||
| 365 | |||||||||||
| 366 | =head2 Bugs / Feature Requests | ||||||||||
| 367 | |||||||||||
| 368 | Please report any bugs or feature requests by email to C<bug-config-role at rt.cpan.org>, or through | ||||||||||
| 369 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Role>. You will be automatically notified of any | ||||||||||
| 370 | progress on the request by the system. | ||||||||||
| 371 | |||||||||||
| 372 | =head2 Source Code | ||||||||||
| 373 | |||||||||||
| 374 | The code is open to the world, and available for you to hack on. Please feel free to browse it and play | ||||||||||
| 375 | with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull | ||||||||||
| 376 | from your repository :) | ||||||||||
| 377 | |||||||||||
| 378 | L<http://github.com/robinsmidsrod/Config-Role> | ||||||||||
| 379 | |||||||||||
| 380 | git clone git://github.com/robinsmidsrod/Config-Role.git | ||||||||||
| 381 | |||||||||||
| 382 | =head1 AUTHOR | ||||||||||
| 383 | |||||||||||
| 384 | Robin Smidsrød <robin@smidsrod.no> | ||||||||||
| 385 | |||||||||||
| 386 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 387 | |||||||||||
| 388 | This software is copyright (c) 2012 by Robin Smidsrød. | ||||||||||
| 389 | |||||||||||
| 390 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 391 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 392 | |||||||||||
| 393 | =cut | ||||||||||
| 394 | |||||||||||
| 395 | |||||||||||
| 396 | __END__ | ||||||||||