Support for Dancer2 #2

Open
wants to merge 2 commits into
from
@@ -16,29 +16,45 @@ my $session_hash_key = $conf->{session_hash_key} || '_flash';
my $session_engine;
-register flash => sub ($;$) {
+set template => 'template_toolkit';
@sukria
sukria Mar 25, 2012

Yes, by the way, the way the plugin works depends on TT : it uses coderefs in the tokens hash, for instance Template::Tiny which is the default one in Dancer 2 does not support that.

Maybe the plugin wants to document that?

+
+register flash => sub {
+ my $dsl = shift;
@sukria
sukria Mar 25, 2012

The main change, shifting $dsl before processing arguments. This allows a cleaner syntax in the plugin code ($dsl->keyword)

@dams
dams Apr 2, 2012 Owner

as discussed, I think Dancer2 should ship Dancer::Template::Tiny or something

@sukria
sukria Apr 3, 2012

Well, this won't change the fact that under Dancer 2, a sub passed to "register" must shift first the $dsl object ;)

@dams
dams Apr 3, 2012 Owner

Sorry I wrote my note at the wrong place :) I intended to comment the set template => 'template_toolkit'. But yes, I agree :)

my ($key, $value) = @_;
- $session_engine ||= engine 'session'
- or croak __PACKAGE__ . " error2 : there is no session engine configured in the configuration. You need a session engine to be able to use this plugin";
+ if (! defined $session_engine) {
+ eval { $session_engine = $dsl->engine( 'session' ) };
@sukria
sukria Mar 25, 2012

In Dancer 2 $dsl->engine triggers an exception if the engine is not set. Don't know if you really want to wrap that?

Also, why not doing that in init time?

@dams
dams Apr 2, 2012 Owner

I don't want to do that at init time, because I initially did, but realized that people actually change their session engine on the fly. Yes they do :)
So need to do the check.

+ $@ and croak __PACKAGE__ . " error2 : there is no session engine configured in the configuration. You need a session engine to be able to use this plugin";
+ }
- my $flash = session($session_hash_key) || {};
+ my $flash = $dsl->session($session_hash_key) || {};
@_ == 2 and $flash->{$key} = $value;
@_ == 1 and $value = delete $flash->{$key};
- session $session_hash_key, $flash;
+
+ $dsl->session($session_hash_key, $flash);
return $value;
};
hook before_template => sub {
- shift->{$token_name} = { map { my $key = $_; my $value;
- ( $key, sub { defined $value and return $value;
- my $flash = session($session_hash_key) || {};
- $value = delete $flash->{$key};
- session $session_hash_key, $flash;
- return $value;
- } );
- } ( keys %{session($session_hash_key) || {} })
- };
+ my $tokens = shift;
+
+ my @keys_in_session = keys %{session($session_hash_key) || {}};
+
+ $tokens->{$token_name} = {
+ map {
+ my $key = $_;
+ my $value;
+ ( $key,
+ sub {
+ defined $value and return $value;
+ my $flash = session($session_hash_key) || {};
+ $value = delete $flash->{$key};
+ session $session_hash_key, $flash;
+ return $value;
+ }
+ );
+ } @keys_in_session
+ };
@sukria
sukria Mar 25, 2012

Oh, Perl::Tidy went over this apparently, sorry ;)

};
register_plugin;
View
@@ -28,6 +28,7 @@ response_content_like( [ GET => '/nothing' ], qr/foo :\s*, message :\s*$/ );
# first time we get the error message
route_exists [ GET => '/' ];
response_content_like( [ GET => '/' ], qr/foo : bar, message : plop$/ );
+
# second time the error has disappeared
route_exists [ GET => '/different' ];
response_content_like( [ GET => '/different' ], qr/foo : bar, message : \s*$/ );
View
@@ -4,6 +4,11 @@ use Dancer::Test;
use_ok 'Dancer::Plugin::FlashMessage';
-is(flash(foo => 'bar'), 'bar');
-is(flash('foo'), 'bar');
-is(flash('foo'), undef);
+get '/' => sub {
+
+ is(flash(foo => 'bar'), 'bar');
+ is(flash('foo'), 'bar');
+ is(flash('foo'), undef);
+};
+
+dancer_response GET => '/';
View
@@ -1 +1 @@
-foo : <% foo %>, message : <% flash.error %>
+foo : [% foo %], message : [% flash.error %]