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 | |
fc44be6f |
33 | # optionally, default the configfile: |
34 | has +configfile ( default => '/tmp/foo.yaml' ); |
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 | |
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 | |
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 | |
61 | Attributes |
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 | |
70 | Class Methods |
71 | new_with_config |
72 | This is an alternate constructor, which knows to look for the |
73 | "configfile" option in its arguments and use that to set attributes. It |
74 | is much like MooseX::Getopts's "new_with_options". Example: |
75 | |
76 | my $foo = SomeClass->new_with_config(configfile => '/etc/foo.yaml'); |
77 | |
78 | Explicit arguments will overide anything set by the configfile. |
79 | |
80 | get_config_from_file |
81 | This class method is not implemented in this role, but it is required of |
82 | all subroles. Its two arguments are the classname and the configfile, |
fc44be6f |
83 | and it is expected to return a hashref of arguments to pass to "new()" |
84 | which are sourced from the configfile. |
c35b639e |
85 | |
86 | meta |
87 | The Moose meta stuff, included here because otherwise pod tests fail |
88 | sometimes |
89 | |
90 | BUGS |
91 | AUTHOR |
92 | Brandon L. Black, <blblack@gmail.com> |
93 | |
94 | LICENSE |
95 | This library is free software; you can redistribute it and/or modify it |
96 | under the same terms as Perl itself. |
97 | |