support for _get_default_configfile - RT#79746
[gitmo/MooseX-ConfigFromFile.git] / README
1 NAME
2     MooseX::ConfigFromFile - An abstract Moose role for setting attributes
3     from a configfile
4
5 SYNOPSIS
6       ########
7       ## A real role based on this abstract role:
8       ########
9
10       package MooseX::SomeSpecificConfigRole;
11       use Moose::Role;
12   
13       with 'MooseX::ConfigFromFile';
14   
15       use Some::ConfigFile::Loader ();
16
17       sub get_config_from_file {
18         my ($class, $file) = @_;
19
20         my $options_hashref = Some::ConfigFile::Loader->load($file);
21
22         return $options_hashref;
23       }
24
25
26       ########
27       ## A class that uses it:
28       ########
29       package Foo;
30       use Moose;
31       with 'MooseX::SomeSpecificConfigRole';
32
33       # optionally, default the configfile:
34       sub configfile { '/tmp/foo.yaml' }
35
36       # ... insert your stuff here ...
37
38       ########
39       ## A script that uses the class with a configfile
40       ########
41
42       my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo');
43
44 DESCRIPTION
45     This is an abstract role which provides an alternate constructor for
46     creating objects using parameters passed in from a configuration file.
47     The actual implementation of reading the configuration file is left to
48     concrete subroles.
49
50     It declares an attribute "configfile" and a class method
51     "new_with_config", and requires that concrete roles derived from it
52     implement the class method "get_config_from_file".
53
54     Attributes specified directly as arguments to "new_with_config"
55     supercede those in the configfile.
56
57     MooseX::Getopt knows about this abstract role, and will use it if
58     available to load attributes from the file specified by the commandline
59     flag "--configfile" during its normal "new_with_options".
60
61 Attributes
62   configfile
63     This is a Path::Class::File object which can be coerced from a regular
64     pathname string. This is the file your attributes are loaded from. You
65     can add a default configfile in the class using the role and it will be
66     honored at the appropriate time:
67
68       has +configfile ( default => '/etc/myapp.yaml' );
69
70     Note that you can alternately just provide a "configfile" method which
71     returns the config file when called - this will be used in preference to
72     the default of the attribute.
73
74 Class Methods
75   new_with_config
76     This is an alternate constructor, which knows to look for the
77     "configfile" option in its arguments and use that to set attributes. It
78     is much like MooseX::Getopts's "new_with_options". Example:
79
80       my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml');
81
82     Explicit arguments will overide anything set by the configfile.
83
84   get_config_from_file
85     This class method is not implemented in this role, but it is required of
86     all subroles. Its two arguments are the classname and the configfile,
87     and it is expected to return a hashref of arguments to pass to "new()"
88     which are sourced from the configfile.
89
90 COPYRIGHT
91     Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and
92     "CONTRIBUTORS" as listed below.
93
94 AUTHOR
95     Brandon L. Black, <blblack@gmail.com>
96
97 CONTRIBUTORS
98     Tomas Doran "<bobtfish@bobtfish.net>" (current maintainer).
99     Karen Etheridge
100     Chris Prather
101     Zbigniew Lukasiak
102
103 LICENSE
104     This library is free software; you can redistribute it and/or modify it
105     under the same terms as Perl itself.
106