Skip to content

Commit

Permalink
fix api break and support more YAML engines
Browse files Browse the repository at this point in the history
  • Loading branch information
Danijel Tasov committed Feb 26, 2011
1 parent c9033a2 commit 32a1afd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.1.0 - 2011-02-26
- adjusted to new shiny mojolicious api
- support YAML::Old and YAML
- depends on perl > 5.010 now

0.0.4 - 2010-08-25

- YAML::Tiny is the default backend now.
Expand Down
5 changes: 3 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ WriteMakefile(
'VERSION_FROM' => 'lib/Mojolicious/Plugin/YamlConfig.pm',
'NAME' => 'Mojolicious::Plugin::YamlConfig',
'PREREQ_PM' => {
'Mojolicious' => '0.999927',
'YAML::Tiny' => '0'
'Mojolicious' => '1.12',
'perl' => '5.010',
'YAML::Tiny' => '0'
},
META_ADD => {
resources => {
Expand Down
42 changes: 26 additions & 16 deletions lib/Mojolicious/Plugin/YamlConfig.pm
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
# vim: ai et sw=4
use strict;
use warnings;
use 5.010;
package Mojolicious::Plugin::YamlConfig;

use base 'Mojolicious::Plugin::JsonConfig';

our $VERSION = '0.0.4';
our $VERSION = '0.1.0';

sub register {
my ($self, $app, $conf) = @_;
$conf ||= {};
$conf->{ext} = 'yaml';
$conf->{class} ||= 'YAML::Tiny';
$self->{class} = $conf->{class};
return $self->SUPER::register($app, $conf);
my ( $self, $app, $conf ) = @_;
$conf ||= {};
$conf->{ext} = 'yaml';
$conf->{class} ||= $ENV{MOJO_YAML} || 'YAML::Tiny';
$self->{class} = $conf->{class};
unless ( $conf->{class} ~~ [qw(YAML YAML::XS YAML::Tiny YAML::Old)]) {
warn("$conf->{class} is not supported, use at your own risk");
}
return $self->SUPER::register( $app, $conf );
}

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

my $class = $self->{class};
eval "require $class; 1" || die($@);

# Parse
my ($config,$error);

$config = eval $class.'::Load($encoded)';
# Render
$content = $self->render($content, $file, $conf, $app);

if ($class ~~ [qw(YAML YAML::Old)]) {
# they are broken *sigh*
$content = Encode::decode('UTF-8', $content);
}

$config = eval $class.'::Load($content)';
if($@) {
$error = $@;
}

die qq/Couldn't parse config "$name": $error/ if !$config && $error;
die qq/Invalid config "$name"./ if !$config || ref $config ne 'HASH';
die qq/Couldn't parse config "$file": $error/ if !$config && $error;
die qq/Invalid config "$file"./ if !$config || ref $config ne 'HASH';

return $config;
}
Expand Down Expand Up @@ -76,9 +87,8 @@ and you should be fine. :)
=head2 LIMITATIONS
L<YAML::Tiny> is the default parser. It doesn't even try to implement the full
YAML spec. Currently you can use YAML::XS via the C<class> option to parse the
data with L<YAML::XS> (It's currently the only supported alternative. Use
L<YAML>, L<YAML::Any>, L<YAML::Old> or L<YAML::Syck> at your own risk).
YAML spec. Currently you can use L<YAML::XS>, L<YAML::Old> and L<YAML> via the
C<class> option to parse the data with a more advanced YAML parser.
=head2 AUTHOR
Expand Down
3 changes: 2 additions & 1 deletion t/30-yaml_config_lite_app.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 => 11;
plan tests => 12;

# Oh, I always feared he might run off like this.
# Why, why, why didn't I break his legs?
Expand All @@ -33,6 +33,7 @@ my $config =
is($config->{foo}, 'bar', 'right value');
is($config->{hello}, 'there', 'right value');
is($config->{utf}, 'утф', 'right value');
is($config->{bar}, app->home, 'rendering');

SKIP: {
eval 'require YAML::XS; 1' ||
Expand Down
1 change: 1 addition & 0 deletions t/30-yaml_config_lite_app.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
foo: "bar"
utf: "утф"
bar: "<%= app->home %>"

0 comments on commit 32a1afd

Please sign in to comment.