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