Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for "has" in Dancer2::Plugin #1292

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/Dancer2/Plugin.pm
Expand Up @@ -342,8 +342,18 @@ sub _exporter_plugin {
our \@EXPORT = ( ':app' );

around has => sub {
my( \$orig, \@args ) = \@_;
\$orig->( ${caller}->_p2_has( \@args) );
my( \$orig, \$name, \%args ) = \@_;

if (ref \$name eq 'ARRAY'
&& exists \$args{'plugin_keyword'}
&& ref \$args{'plugin_keyword'} eq 'ARRAY') {

Carp::croak('Setting "plugin_keyword" to an array is disallowed'
. ' when defining multiple attributes simultaneously');
}

\$orig->( ${caller}->_p2_has( \$_, \%args) )
for ref \$name ? @\$name : \$name;
};

sub PluginKeyword :ATTR(CODE,BEGIN) {
Expand Down
26 changes: 24 additions & 2 deletions t/plugin2/from-config.t
@@ -1,7 +1,7 @@
use strict;
use warnings;

use Test::More tests => 5;
use Test::More tests => 8;

{
package Dancer2::Plugin::FromConfig;
Expand Down Expand Up @@ -37,6 +37,21 @@ has six => (
plugin_keyword => 1,
);

has [qw(seven eight)] => (
is => 'ro',
from_config => 1,
plugin_keyword => 1,
);

eval {
has [qw(nine ten)] => (
is => 'ro',
from_config => 1,
plugin_keyword => ['nine', 'ten'],
);
};
our $plugin_keyword_exception = $@;

plugin_keywords qw/ one three four five /;

}
Expand All @@ -54,7 +69,9 @@ plugin_keywords qw/ one three four five /;
one => 'un',
two => {
three => 'trois',
}
},
seven => 'sept',
eight => 'huit',
}
};

Expand All @@ -64,6 +81,11 @@ plugin_keywords qw/ one three four five /;
Test::More::is four() => 'quatre', 'nothing in config, default value';
Test::More::is five() => 'cinq', 'from_config a coderef';
Test::More::is six() => 'AH!', 'from_config a coderef, no override';
Test::More::is seven() => 'sept', 'from_config, defined two fields at once #1';
Test::More::is eight() => 'huit', 'from_config, defined two fields at once #2';
Test::More::ok $Dancer2::Plugin::FromConfig::plugin_keyword_exception,
"defining two fields simultaneously with multiple plugin_keyword values"
. " is disallowed";
}


Expand Down