Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

t/02 passes with dancer2

  • Loading branch information...
commit 0f5beda5352b0e0d1f92de16e13d7153ff93de33 1 parent 796f9b1
Alexis Sukrieh authored
View
44 lib/Dancer/Plugin/FlashMessage.pm
@@ -16,29 +16,45 @@ my $session_hash_key = $conf->{session_hash_key} || '_flash';
my $session_engine;
-register flash => sub ($;$) {
+set template => 'template_toolkit';
+
+register flash => sub {
+ my $dsl = shift;
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' ) };
+ $@ 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
+ };
};
register_plugin;
View
1  t/02_flash.t
@@ -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
2  t/views/index.tt
@@ -1 +1 @@
-foo : <% foo %>, message : <% flash.error %>
+foo : [% foo %], message : [% flash.error %]
Please sign in to comment.
Something went wrong with that request. Please try again.