File Coverage

blib/lib/Catalyst/Plugin/Session/AsObject.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Session::AsObject;
2             {
3             $Catalyst::Plugin::Session::AsObject::VERSION = '0.05';
4             }
5              
6 1     1   57745 use strict;
  1         3  
  1         40  
7 1     1   7 use warnings;
  1         1  
  1         45  
8              
9 1     1   2222 use Catalyst::Plugin::Session 0.27;
  0            
  0            
10             use base 'Catalyst::Plugin::Session';
11              
12             use MRO::Compat;
13              
14             sub setup {
15             my $self = shift;
16              
17             $self->maybe::next::method(@_);
18              
19             my $class = $self->_session_plugin_config()->{object_class};
20              
21             die 'Must provide an object_class in the session config when using '
22             . __PACKAGE__
23             unless defined $class;
24              
25             die
26             "The object_class in the session config is either not loaded or does not have a new() method"
27             unless $class->can('new');
28             }
29              
30             sub has_session_object {
31             my $self = shift;
32              
33             return $self->sessionid() && $self->session()->{__object};
34             }
35              
36             sub session_object {
37             my $self = shift;
38              
39             my $session = $self->session();
40              
41             $session->{__object}
42             ||= $self->_session_plugin_config()->{object_class}->new();
43              
44             return $self->session()->{__object};
45             }
46              
47             1;
48              
49             # ABSTRACT: Make your session data an object
50              
51              
52              
53             =pod
54              
55             =head1 NAME
56              
57             Catalyst::Plugin::Session::AsObject - Make your session data an object
58              
59             =head1 VERSION
60              
61             version 0.05
62              
63             =head1 SYNOPSIS
64              
65             package MyApp;
66              
67             use MyApp::Session;
68              
69             use Catalyst qw(
70             Session
71             Session::AsObject
72             Session::Store::DBI
73             Session::State::Cookie
74             );
75              
76             __PACKAGE__->config(
77             'Plugin::Session' => {
78             ...,
79             object_class => 'MyApp::Session',
80             },
81             );
82              
83             sub foo : Global {
84             my $self = shift;
85             my $c = shift;
86              
87             my $session = $c->session_object();
88              
89             if ( $session->has_error_messages() ) {...}
90             }
91              
92             =head1 DESCRIPTION
93              
94             This class makes it easier to treat the session as an object rather
95             than a plain hash reference. This is useful if you want to ensure that
96             the session only contains specific pieces of data.
97              
98             However, because of implementation details, we cannot override the
99             existing C<< $c->session() >> method, so you need to use the new C<<
100             $c->session_object() >> method provided by this plugin.
101              
102             =head1 METHODS
103              
104             This class provides the following methods:
105              
106             =head2 $c->session_object()
107              
108             Returns the object stored in the session. If needed, a new object is
109             created.
110              
111             =head2 $c->has_session_object()
112              
113             Returns true if there is an object already in the session.
114              
115             =head1 CONFIG
116              
117             This plugin has only configuration key, "object_class". This key
118             should appear under the existing top-level "session" configuration
119             key.
120              
121             The "object_class" must already be loaded, and must have a C<new()>
122             method as its constructor. This constructor must not require any
123             parameters, as it will be called without any arguments.
124              
125             =head1 BUGS
126              
127             Please report any bugs or feature requests to
128             C<bug-catalyst-plugin-sessionasobject@rt.cpan.org>, or through the web
129             interface at L<http://rt.cpan.org>. I will be notified, and then
130             you'll automatically be notified of progress on your bug as I make
131             changes.
132              
133             =head1 DONATIONS
134              
135             If you'd like to thank me for the work I've done on this module,
136             please consider making a "donation" to me via PayPal. I spend a lot of
137             free time creating free software, and would appreciate any support
138             you'd care to offer.
139              
140             Please note that B<I am not suggesting that you must do this> in order
141             for me to continue working on this particular software. I will
142             continue to do so, inasmuch as I have in the past, for as long as it
143             interests me.
144              
145             Similarly, a donation made in this way will probably not make me work
146             on this software much more, unless I get so many donations that I can
147             consider working on free software full time, which seems unlikely at
148             best.
149              
150             To donate, log into PayPal and send money to autarch@urth.org or use
151             the button on this page:
152             L<http://www.urth.org/~autarch/fs-donation.html>
153              
154             =head1 AUTHOR
155              
156             Dave Rolsky, E<gt>autarch@urth.orgE<lt>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is Copyright (c) 2012 by Dave Rolsky.
161              
162             This is free software, licensed under:
163              
164             The Artistic License 2.0 (GPL Compatible)
165              
166             =cut
167              
168              
169             __END__
170