Skip to content

Instantly share code, notes, and snippets.

@marcusramberg
Created October 3, 2010 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusramberg/608721 to your computer and use it in GitHub Desktop.
Save marcusramberg/608721 to your computer and use it in GitHub Desktop.
commit 0601acc9b4ae5cfe949415c3cb501596fa8417f2
Author: Marcus Ramberg <marcus@nordaaker.com>
Date: Sun Oct 3 18:07:52 2010 +0200
Support .mode. json config file
diff --git a/lib/Mojolicious/Plugin/JsonConfig.pm b/lib/Mojolicious/Plugin/JsonConfig.pm
index 9ceb878..e314447 100644
--- a/lib/Mojolicious/Plugin/JsonConfig.pm
+++ b/lib/Mojolicious/Plugin/JsonConfig.pm
@@ -21,6 +21,7 @@ sub register {
# File
my $file = $conf->{file};
+ my $mode_file;
unless ($file) {
# Basename
@@ -29,6 +30,7 @@ sub register {
# Remove .pl, .p6 and .t extentions
$file =~ s/(?:\.p(?:l|6))|\.t$//i;
+ $mode_file=$file.'.' . $app->mode . '.' . ($conf->{ext} || 'json');
# Default extension
$file .= '.' . ($conf->{ext} || 'json');
}
@@ -36,10 +38,13 @@ sub register {
# Absolute path
$file = $app->home->rel_file($file)
unless File::Spec->file_name_is_absolute($file);
+ $mode_file = $app->home->rel_file($mode_file)
+ if defined $mode_file && ! File::Spec->file_name_is_absolute($mode_file);
# Read config file
my $config = {};
my $template = $conf->{template} || {};
+
if (-e $file) { $config = $self->_read_config($file, $template, $app) }
# Check for default
@@ -54,6 +59,11 @@ sub register {
qq/Config file "$file" missing, using default config./);
}
+ if( defined $mode_file && -e $mode_file ) {
+ my $mode_config = $self->_read_config($mode_file, $template, $app);
+ $config = {%$config, %$mode_config};
+ }
+
# Stash key
my $stash_key = $conf->{stash_key} || 'config';
@@ -157,6 +167,11 @@ preprocesses it's input with L<Mojo::Template>.
The application object can be accessed via C<$app> or the C<app> helper.
+=head2 Mode specific config
+
+You can override the default config on a per key basis by adding a
+myapp.$mode.json config file.
+
=head2 Options
=over 4
diff --git a/t/mojolicious/json_config_lite_app_mode.json b/t/mojolicious/json_config_lite_app_mode.json
new file mode 100644
index 0000000..db85acb
--- /dev/null
+++ b/t/mojolicious/json_config_lite_app_mode.json
@@ -0,0 +1,3 @@
+{
+ "foo" : "bar"
+}
diff --git a/t/mojolicious/json_config_lite_app_mode.t b/t/mojolicious/json_config_lite_app_mode.t
new file mode 100644
index 0000000..914a3e8
--- /dev/null
+++ b/t/mojolicious/json_config_lite_app_mode.t
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use utf8;
+
+# Disable epoll, kqueue and IPv6
+BEGIN { $ENV{MOJO_POLL} = $ENV{MOJO_NO_IPV6} = 1; $ENV{MOJO_MODE}='testing'; }
+
+use Mojo::IOLoop;
+use Test::More;
+
+# Make sure sockets are working
+plan skip_all => 'working sockets required for this test!'
+ unless Mojo::IOLoop->new->generate_port;
+plan tests => 3;
+
+# QUOTE GOES HERE
+
+use Mojolicious::Lite;
+use Test::Mojo;
+
+# Silence
+app->log->level('error');
+
+# Load plugin
+plugin 'json_config';
+
+# GET /
+get '/' => 'index';
+
+my $t = Test::Mojo->new;
+
+# GET /
+$t->get_ok('/')->status_is(200)->content_like(qr/baz/);
+
+
+__DATA__
+@@ index.html.ep
+<%= $config->{foo} %>
diff --git a/t/mojolicious/json_config_lite_app_mode.testing.json b/t/mojolicious/json_config_lite_app_mode.testing.json
new file mode 100644
index 0000000..cbd0afe
--- /dev/null
+++ b/t/mojolicious/json_config_lite_app_mode.testing.json
@@ -0,0 +1,3 @@
+{
+ "foo" : "baz"
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment