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