/[Frey]/trunk/lib/Frey/Config.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/Config.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26