support for _get_default_configfile - RT#79746
[gitmo/MooseX-ConfigFromFile.git] / README
CommitLineData
c35b639e 1NAME
2 MooseX::ConfigFromFile - An abstract Moose role for setting attributes
3 from a configfile
4
5SYNOPSIS
6 ########
7 ## A real role based on this abstract role:
8 ########
9
10 package MooseX::SomeSpecificConfigRole;
11 use Moose::Role;
20611f8e 12
13 with 'MooseX::ConfigFromFile';
14
15 use Some::ConfigFile::Loader ();
c35b639e 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
fc44be6f 33 # optionally, default the configfile:
f028e0e1 34 sub configfile { '/tmp/foo.yaml' }
fc44be6f 35
c35b639e 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
44DESCRIPTION
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
fc44be6f 54 Attributes specified directly as arguments to "new_with_config"
55 supercede those in the configfile.
56
c35b639e 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
61Attributes
62 configfile
63 This is a Path::Class::File object which can be coerced from a regular
fc44be6f 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' );
c35b639e 69
f028e0e1 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
c35b639e 74Class 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,
fc44be6f 87 and it is expected to return a hashref of arguments to pass to "new()"
88 which are sourced from the configfile.
c35b639e 89
f028e0e1 90COPYRIGHT
91 Copyright (c) 2007 - 2009 the MooseX::ConfigFromFile "AUTHOR" and
92 "CONTRIBUTORS" as listed below.
c35b639e 93
c35b639e 94AUTHOR
95 Brandon L. Black, <blblack@gmail.com>
96
f028e0e1 97CONTRIBUTORS
98 Tomas Doran "<bobtfish@bobtfish.net>" (current maintainer).
99 Karen Etheridge
100 Chris Prather
101 Zbigniew Lukasiak
102
c35b639e 103LICENSE
104 This library is free software; you can redistribute it and/or modify it
105 under the same terms as Perl itself.
106