Skip to content

Commit

Permalink
a couple of cosmetic changes for dsl->dancer_version and register_plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Sukrieh committed Mar 27, 2012
1 parent 938bace commit e4cbca9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/Dancer.pm
Expand Up @@ -10,7 +10,7 @@ use Dancer::Core::App;
use Dancer::Core::DSL;
use Dancer::FileUtils;

our $VERSION = '1.9999_01';
our $VERSION = '2.000000_01';
our $AUTHORITY = 'SUKRIA';

#
Expand Down
2 changes: 2 additions & 0 deletions lib/Dancer/Core/DSL.pm
Expand Up @@ -25,6 +25,7 @@ sub dsl_keywords {
[core_debug => 1],
[dance => 1],
[dancer_app => 1],
[dancer_version => 1],
[debug => 1],
[del => 1],
[dirname => 1],
Expand Down Expand Up @@ -83,6 +84,7 @@ sub dsl_keywords_as_list {
}

sub dancer_app { shift->app }
sub dancer_version { (split /\./, $Dancer::VERSION)[0] }
sub dsl { shift }

sub debug { shift->log(debug => @_) }
Expand Down
28 changes: 19 additions & 9 deletions lib/Dancer/Plugin.pm
Expand Up @@ -3,11 +3,6 @@ use Moo::Role;
use Carp 'croak';
use Dancer::Core::DSL;

# The plugin system major version, to check compatibility against it
our $PLUGIN_MAJOR_VERSION = 2;

sub major_version { $PLUGIN_MAJOR_VERSION }

sub _get_dsl {
my $dsl;
my $deep = 2;
Expand Down Expand Up @@ -99,6 +94,16 @@ sub register {
];
}

=method register_plugin
A Dancer plugin must end with this statement. This lets the plugin register all
the symbols define with C<register> as exported symbols (via the L<Exporter>
module).
A Dancer plugin inherits from Dancer::Plugin and Exporter transparently.
=cut

sub register_plugin {
my $plugin = caller;
my $caller = caller(1);
Expand All @@ -110,13 +115,18 @@ sub register_plugin {

my $supported_versions = $params{for_versions} || [ 1 ];
ref $supported_versions eq 'ARRAY'
or croak "register_plugin must be called like this : register_plugin for_versions => [ 1, 2 ]";

+{ map { $_ => 1 } @$supported_versions }->{$PLUGIN_MAJOR_VERSION}
or croak "can't register plugin '$plugin', it doesn't support Dancer version $PLUGIN_MAJOR_VERSION, it only supports these version(s): " . join(',', @$supported_versions) . ". Please upgrade the plugin.";
or croak "register_plugin must be called with an array ref";

# if the caller has not a dsl, we cant register the plugin
return if ! $caller->can('dsl');
my $dancer_version = $caller->dsl->dancer_version;
my $plugin_version = eval "\$${plugin}::VERSION" || '??';

warn "supported_versions : ".join(', ', @$supported_versions);
# make sure the plugin is compatible with this version of Dancer
grep /^$dancer_version$/, @{ $supported_versions }
or croak "$plugin $plugin_version does not support Dancer $dancer_version.";


# we have a $dsl in our caller, we can register our symbols then
my $dsl = $caller->dsl;
Expand Down
1 change: 1 addition & 0 deletions t/dsl.t
Expand Up @@ -19,4 +19,5 @@ any ['get', 'post'], '/' => sub {
is $r->[2][0], 'POST';
}

is dancer_version, 2, 'dancer_version';
done_testing;
12 changes: 12 additions & 0 deletions t/plugin_register.t
Expand Up @@ -44,4 +44,16 @@ subtest 'plugin reserved keywords' => sub {
}
};

subtest 'plugin version' => sub {

package Foo;
our $VERSION = '1.034';
use Dancer;
use Dancer::Plugin;

eval { register_plugin };
Test::More::like $@, qr{Foo 1.034 does not support Dancer 2};
};


done_testing;

0 comments on commit e4cbca9

Please sign in to comment.