Commit | Line | Data |
33defd43 |
1 | package MooseX::SimpleConfig; |
2 | |
3 | use Moose::Role; |
4 | with 'MooseX::ConfigFromFile'; |
5 | |
a96520dd |
6 | our $VERSION = '0.03'; |
33defd43 |
7 | |
8 | use Config::Any (); |
9 | |
10 | sub get_config_from_file { |
11 | my ($class, $file) = @_; |
12 | |
13 | my $raw_cfany = Config::Any->load_files({ |
14 | files => [ $file ], |
15 | use_ext => 1, |
16 | }); |
17 | |
18 | die q{Specified configfile '} . $file |
19 | . q{' does not exist, is empty, or is not readable} |
20 | unless $raw_cfany->[0] |
21 | && exists $raw_cfany->[0]->{$file}; |
22 | |
23 | my $raw_config = $raw_cfany->[0]->{$file}; |
24 | |
25 | die "configfile must represent a hash structure" |
26 | unless $raw_config && ref $raw_config && ref $raw_config eq 'HASH'; |
27 | |
28 | $raw_config; |
29 | } |
30 | |
b9b58042 |
31 | has extensions => ( |
32 | is => 'ro', |
33 | isa => 'ArrayRef', |
34 | lazy_build =>1, |
35 | ); |
36 | |
37 | sub _build_extensions { |
38 | return Config::Any->extensions(); |
39 | } |
40 | |
41 | has plugins => ( |
42 | is => 'ro', |
43 | isa => 'ArrayRef', |
44 | lazy_build => 1 |
45 | ); |
46 | |
47 | sub _build_plugins { |
48 | return [Config::Any->plugins()] ; |
49 | } |
50 | |
33defd43 |
51 | no Moose::Role; 1; |
52 | |
53 | __END__ |
54 | |
55 | =pod |
56 | |
57 | =head1 NAME |
58 | |
59 | MooseX::SimpleConfig - A Moose role for setting attributes from a simple configfile |
60 | |
61 | =head1 SYNOPSIS |
62 | |
63 | ## A YAML configfile named /etc/my_app.yaml: |
64 | foo: bar |
65 | baz: 123 |
66 | |
67 | ## In your class |
68 | package My::App; |
69 | use Moose; |
70 | |
71 | with 'MooseX::SimpleConfig'; |
72 | |
73 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
74 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
75 | |
76 | # ... rest of the class here |
77 | |
78 | ## in your script |
79 | #!/usr/bin/perl |
80 | |
81 | use My::App; |
82 | |
83 | my $app = My::App->new_with_config(configfile => '/etc/my_app.yaml'); |
84 | # ... rest of the script here |
85 | |
86 | #################### |
87 | ###### combined with MooseX::Getopt: |
88 | |
89 | ## In your class |
90 | package My::App; |
91 | use Moose; |
92 | |
93 | with 'MooseX::SimpleConfig'; |
94 | with 'MooseX::Getopt'; |
95 | |
96 | has 'foo' => (is => 'ro', isa => 'Str', required => 1); |
97 | has 'baz' => (is => 'rw', isa => 'Int', required => 1); |
98 | |
99 | # ... rest of the class here |
100 | |
101 | ## in your script |
102 | #!/usr/bin/perl |
103 | |
104 | use My::App; |
105 | |
106 | my $app = My::App->new_with_options(); |
107 | # ... rest of the script here |
108 | |
109 | ## on the command line |
110 | % perl my_app_script.pl -configfile /etc/my_app.yaml -otherthing 123 |
111 | |
112 | =head1 DESCRIPTION |
113 | |
114 | This role loads simple configfiles to set object attributes. It |
115 | is based on the abstract role L<MooseX::ConfigFromFile>, and uses |
116 | L<Config::Any> to load your configfile. L<Config::Any> will in |
117 | turn support any of a variety of different config formats, detected |
118 | by the file extension. See L<Config::Any> for more details about |
119 | supported formats. |
120 | |
121 | Like all L<MooseX::ConfigFromFile> -derived configfile loaders, this |
122 | module is automatically supported by the L<MooseX::Getopt> role as |
123 | well, which allows specifying C<-configfile> on the commandline. |
124 | |
125 | =head1 ATTRIBUTES |
126 | |
127 | =head2 configfile |
128 | |
a96520dd |
129 | Provided by the base role L<MooseX::ConfigFromFile>. You can |
130 | provide a default configfile pathname like so: |
131 | |
132 | has +configfile ( default => '/etc/myapp.yaml' ); |
33defd43 |
133 | |
134 | =head1 CLASS METHODS |
135 | |
136 | =head2 new_with_config |
137 | |
138 | Provided by the base role L<MooseX::ConfigFromFile>. Acts just like |
139 | regular C<new()>, but also accepts an argument C<configfile> to specify |
140 | the configfile from which to load other attributes. Explicit arguments |
141 | to C<new_with_config> will override anything loaded from the configfile. |
142 | |
143 | =head2 get_config_from_file |
144 | |
145 | Called internally by either C<new_with_config> or L<MooseX::Getopt>'s |
146 | C<new_with_options>. Invokes L<Config::Any> to parse C<configfile>. |
147 | |
148 | =head1 AUTHOR |
149 | |
150 | Brandon L. Black, E<lt>blblack@gmail.comE<gt> |
151 | |
152 | =head1 LICENSE |
153 | |
154 | This library is free software; you can redistribute it and/or modify |
155 | it under the same terms as Perl itself. |
156 | |
157 | =cut |