Skip to content

Commit

Permalink
process config files more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 23, 2014
1 parent 8881290 commit 27a3518
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/Mojolicious/Plugin/JSONConfig.pm
@@ -1,16 +1,14 @@
package Mojolicious::Plugin::JSONConfig;
use Mojo::Base 'Mojolicious::Plugin::Config';

use Mojo::JSON;
use Mojo::JSON 'from_json';
use Mojo::Template;
use Mojo::Util 'encode';

sub parse {
my ($self, $content, $file, $conf, $app) = @_;

my $json = Mojo::JSON->new;
my $config = $json->decode($self->render($content, $file, $conf, $app));
if (my $err = $json->error) { die qq{Can't parse config "$file": $err} }
my $config = eval { from_json $self->render($content, $file, $conf, $app) };
die qq{Can't parse config "$file": $@} if !$config && $@;
die qq{Invalid config "$file"} unless ref $config eq 'HASH';

return $config;
Expand All @@ -25,10 +23,9 @@ sub render {
my $prepend = q[my $app = shift; no strict 'refs'; no warnings 'redefine';];
$prepend .= q[sub app; local *app = sub { $app }; use Mojo::Base -strict;];

# Render and encode for JSON decoding
my $mt = Mojo::Template->new($conf->{template} || {})->name($file);
my $json = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
return ref $json ? die $json : encode 'UTF-8', $json;
my $output = $mt->prepend($prepend . $mt->prepend)->render($content, $app);
return ref $output ? die $output : $output;
}

1;
Expand Down

0 comments on commit 27a3518

Please sign in to comment.