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