File Coverage

blib/lib/Catalyst/Plugin/Config/JSON.pm
Criterion Covered Total %
statement 29 31 93.5
branch 3 6 50.0
condition 1 2 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 41 47 87.2


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Config::JSON;
2              
3 1     1   3667 use strict;
  1         3  
  1         32  
4 1     1   4 use warnings;
  1         2  
  1         32  
5              
6 1     1   1076 use UNIVERSAL 'isa';
  1         25  
  1         5  
7              
8 1     1   355 use NEXT;
  1         2  
  1         9  
9 1     1   26 use JSON;
  1         2  
  1         9  
10 1     1   138 use Path::Class 'file';
  1         1  
  1         263  
11              
12             our $VERSION = '0.03';
13              
14             =head1 NAME
15              
16             Catalyst::Plugin::Config::JSON - Configure your Catalyst application via an external
17             JSON file
18              
19             =head1 SYNOPSIS
20              
21             use Catalyst 'Config::JSON';
22            
23             __PACKAGE__->config('config_file' => 'config.json');
24              
25             =head1 DESCRIPTION
26              
27             This Catalyst plugin enables you to configure your Catalyst application with an
28             external JSON file instead of somewhere in your application code.
29              
30             This is useful for example if you want to quickly change the configuration for
31             different deployment environments (like development, testing or production)
32             without changing your code.
33              
34             The configuration file is assumed to be in your application home. Its name can
35             be specified with the config parameter C<config_file> (default is F<config.json>).
36              
37             For any keys in the configuration file that start with C<Catalyst::>, the
38             corresponding value is taken as the configuration for that class.
39              
40             =head2 EXTENDED METHODS
41              
42             =over
43              
44             =item setup
45              
46             =cut
47              
48             sub setup {
49 1     1 1 285693 my $c = shift;
50 1   50     12 my $config_file = $c->config->{'config_file'} || 'config.json';
51 1 50       117 $config_file = file($c->config->{'home'}, $config_file) unless file($config_file)->is_absolute;
52 1 50       524 open CONFIG, "<$config_file"
53             or die "failed to open $config_file for reading; $!";
54 1         626 local($/)=undef;
55 1         33 my $config = <CONFIG>;
56 1         13 close CONFIG;
57 1         8 my $options = jsonToObj($config);
58 1         310 foreach my $key (keys %$options) {
59 1 50       7 if (isa($key, 'Catalyst::Base')) {
60 1         224 $key->config(delete $options->{$key});
61             }
62             }
63 0           $c->config($options);
64 0           $c->NEXT::setup;
65             }
66              
67             =back
68              
69             =head1 SEE ALSO
70              
71             L<Catalyst>, L<JSON>.
72              
73             =head1 AUTHOR
74              
75             Catalyst::Plugin::Config::YAML by Bernhard Bauer,
76             E<lt>bauerb@in.tum.deE<gt>
77              
78             Tweaked for JSON by Sam Vilain, E<lt>samv@cpan.orgE<gt>
79              
80             =head1 COPYRIGHT AND LICENSE
81              
82             Copyright 2005 by Sam Vilain
83              
84             Portions
85              
86             Copyright 2005 by Bernhard Bauer
87              
88             This library is free software; you can redistribute it and/or modify
89             it under the same terms as Perl itself.
90              
91             =cut