Skip to content

Commit

Permalink
added json_class attribute to Mojolicious::Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 20, 2012
1 parent 01816db commit 84755b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@
- Added build_json_tx and post_json methods to Mojo::UserAgent.
- Added post_json_ok method to Test::Mojo.
- Added json_class attribute to Mojo::UserAgent::Transactor.
- Added json_class attribute to Mojolicious::Sessions.
- Added n function to ojo.
- Improved text_field helper to always set the type attribute. (marty, sri)
- Improved documentation.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Renderer.pm
Expand Up @@ -154,8 +154,8 @@ sub render {

sub template_name {
my ($self, $options) = @_;
return unless my $template = $options->{template} || '';
return unless my $format = $options->{format};
return unless my $template = $options->{template};
return unless my $format = $options->{format};
my $handler = $options->{handler};
return defined $handler ? "$template.$format.$handler" : "$template.$format";
}
Expand Down
38 changes: 24 additions & 14 deletions lib/Mojolicious/Sessions.pm
Expand Up @@ -8,6 +8,7 @@ has [qw(cookie_domain secure)];
has cookie_name => 'mojolicious';
has cookie_path => '/';
has default_expiration => 3600;
has json_class => 'Mojo::JSON';

# "Bender, quit destroying the universe!"
sub load {
Expand All @@ -18,7 +19,8 @@ sub load {

# Deserialize
$value =~ s/-/=/g;
return unless my $session = Mojo::JSON->new->decode(b64_decode $value);
return
unless my $session = $self->json_class->new->decode(b64_decode $value);

# Expiration
my $expiration = $self->default_expiration;
Expand Down Expand Up @@ -56,7 +58,7 @@ sub store {
if $expiration || $default;

# Serialize
my $value = b64_encode(Mojo::JSON->new->encode($session), '');
my $value = b64_encode($self->json_class->new->encode($session), '');
$value =~ s/=/-/g;

# Session cookie
Expand Down Expand Up @@ -94,30 +96,30 @@ L<Mojolicious::Sessions> implements the following attributes.
=head2 C<cookie_domain>
my $domain = $session->cookie_domain;
$session = $session->cookie_domain('.example.com');
my $domain = $sessions->cookie_domain;
$sessions = $sessions->cookie_domain('.example.com');
Domain for session cookie, not defined by default.
=head2 C<cookie_name>
my $name = $session->cookie_name;
$session = $session->cookie_name('session');
my $name = $sessions->cookie_name;
$sessions = $sessions->cookie_name('session');
Name of the signed cookie used to store session data, defaults to
C<mojolicious>.
=head2 C<cookie_path>
my $path = $session->cookie_path;
$session = $session->cookie_path('/foo');
my $path = $sessions->cookie_path;
$sessions = $sessions->cookie_path('/foo');
Path for session cookie, defaults to C</>.
=head2 C<default_expiration>
my $time = $session->default_expiration;
$session = $session->default_expiration(3600);
my $time = $sessions->default_expiration;
$sessions = $sessions->default_expiration(3600);
Time for the session to expire in seconds from now, defaults to C<3600>. The
expiration timeout gets refreshed for every request. Setting the value to C<0>
Expand All @@ -132,10 +134,18 @@ epoch seconds.
# Expire a long long time ago
$c->session(expires => 1);
=head2 C<json_class>
my $class = $sessions->json_class;
$sessions = $sessions->json_class('Mojo::JSON');
Class to be used for JSON serialization and deserialization of session data,
defaults to L<Mojo::JSON>.
=head2 C<secure>
my $secure = $session->secure;
$session = $session->secure(1);
my $secure = $sessions->secure;
$sessions = $sessions->secure(1);
Set the secure flag on all session cookies, so that browsers send them only
over HTTPS connections.
Expand All @@ -147,13 +157,13 @@ implements the following ones.
=head2 C<load>
$session->load(Mojolicious::Controller->new);
$sessions->load(Mojolicious::Controller->new);
Load session data from signed cookie.
=head2 C<store>
$session->store(Mojolicious::Controller->new);
$sessions->store(Mojolicious::Controller->new);
Store session data in signed cookie.
Expand Down

0 comments on commit 84755b8

Please sign in to comment.