Commit | Line | Data |
c35b639e |
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 | # ... insert your stuff here ... |
34 | |
35 | ######## |
36 | ## A script that uses the class with a configfile |
37 | ######## |
38 | |
39 | my $obj = Foo->new_with_config(configfile => '/etc/foo.yaml', other_opt => 'foo'); |
40 | |
41 | DESCRIPTION |
42 | This is an abstract role which provides an alternate constructor for |
43 | creating objects using parameters passed in from a configuration file. |
44 | The actual implementation of reading the configuration file is left to |
45 | concrete subroles. |
46 | |
47 | It declares an attribute "configfile" and a class method |
48 | "new_with_config", and requires that concrete roles derived from it |
49 | implement the class method "get_config_from_file". |
50 | |
51 | MooseX::Getopt knows about this abstract role, and will use it if |
52 | available to load attributes from the file specified by the commandline |
53 | flag "--configfile" during its normal "new_with_options". |
54 | |
55 | Attributes |
56 | configfile |
57 | This is a Path::Class::File object which can be coerced from a regular |
58 | pathname string. This is the file your attributes are loaded from. |
59 | |
60 | Class Methods |
61 | new_with_config |
62 | This is an alternate constructor, which knows to look for the |
63 | "configfile" option in its arguments and use that to set attributes. It |
64 | is much like MooseX::Getopts's "new_with_options". Example: |
65 | |
66 | my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml'); |
67 | |
68 | Explicit arguments will overide anything set by the configfile. |
69 | |
70 | get_config_from_file |
71 | This class method is not implemented in this role, but it is required of |
72 | all subroles. Its two arguments are the classname and the configfile, |
73 | and it is expected to return a hashref of arguments to pass to "new()". |
74 | |
75 | meta |
76 | The Moose meta stuff, included here because otherwise pod tests fail |
77 | sometimes |
78 | |
79 | BUGS |
80 | AUTHOR |
81 | Brandon L. Black, <blblack@gmail.com> |
82 | |
83 | LICENSE |
84 | This library is free software; you can redistribute it and/or modify it |
85 | under the same terms as Perl itself. |
86 | |