1 |
dpavlin |
221 |
package Frey::Config; |
2 |
|
|
use Moose::Role; |
3 |
|
|
|
4 |
dpavlin |
383 |
#requires 'debug'; |
5 |
|
|
|
6 |
dpavlin |
221 |
use YAML qw/LoadFile/; |
7 |
|
|
use Hash::Merge qw/merge/; |
8 |
|
|
use Data::Dump qw/dump/; |
9 |
|
|
|
10 |
|
|
=head1 NAME |
11 |
|
|
|
12 |
|
|
Frey::Config - read configuration YAML files |
13 |
|
|
|
14 |
|
|
=head1 SYNOPSYS |
15 |
|
|
|
16 |
|
|
Foo::Bar->config->{'baz'} == 42 |
17 |
|
|
|
18 |
|
|
If C<etc/site_config.yml> or C<etc/config.yml> have: |
19 |
|
|
|
20 |
|
|
'Foo::Bar': |
21 |
|
|
baz: 42 |
22 |
|
|
|
23 |
|
|
or C<etc/Foo/Bar.yml> has |
24 |
|
|
|
25 |
|
|
baz: 42 |
26 |
|
|
|
27 |
|
|
You can also force config re-load with |
28 |
|
|
|
29 |
|
|
Foo::Bar->load_config('custom'); |
30 |
|
|
|
31 |
|
|
which will load C<etc/custom.yml> as most specific |
32 |
|
|
configuration. |
33 |
|
|
|
34 |
|
|
=cut |
35 |
|
|
|
36 |
|
|
our %config; |
37 |
|
|
|
38 |
|
|
sub load_config { |
39 |
|
|
my $self = shift; |
40 |
dpavlin |
437 |
%config = (); |
41 |
dpavlin |
221 |
foreach my $name ( 'config', 'site_config', ref($self), @_ ) { |
42 |
|
|
my $path = "etc/$name.yml"; |
43 |
|
|
if ( $path =~ s{::}{/}g ) { |
44 |
|
|
# load under Package name |
45 |
|
|
%config = %{ merge( { ref($self) => LoadFile($path) }, \%config ) } if -e $path; |
46 |
|
|
} else { |
47 |
|
|
%config = %{ merge( LoadFile($path) , \%config ) } if -e $path; |
48 |
|
|
} |
49 |
dpavlin |
383 |
warn "## load_config $path current config = ",dump( %config ) if $self->debug; |
50 |
dpavlin |
221 |
} |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
sub config { |
54 |
dpavlin |
322 |
my ( $self, $key ) = @_; |
55 |
|
|
$key ||= ref($self); |
56 |
dpavlin |
221 |
warn "## config $key" if $self->debug; |
57 |
|
|
$self->load_config unless defined %config; |
58 |
dpavlin |
229 |
#confess "$key doesn't exist in config" unless defined $config{ $key }; |
59 |
|
|
return unless defined $config{ $key }; |
60 |
dpavlin |
221 |
return $config{ $key }; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
1; |