File Coverage

blib/lib/Catalyst/Model/InjectionHelpers/PerSession.pm
Criterion Covered Total %
statement 24 25 96.0
branch 6 10 60.0
condition n/a
subroutine 4 4 100.0
pod 0 2 0.0
total 34 41 82.9


line stmt bran cond sub pod time code
1             package Catalyst::Model::InjectionHelpers::PerSession;
2              
3 1     1   566 use Moose;
  1         3  
  1         6  
4 1     1   6688 use Scalar::Util qw/blessed refaddr/;
  1         2  
  1         459  
5              
6             with 'Catalyst::ComponentRole::InjectionHelpers';
7              
8             sub restore_from_session {
9 8     8 0 22 my ($self, $c, @args) = @_;
10 8 50       36 my $key = blessed $self ? refaddr $self : $self;
11              
12 8         32 $key = "__InstancePerContext_${key}";
13              
14 8 50       42 return $c->stash->{$key} if $c->stash->{$key};
15            
16 8 100       581 if(exists $c->session->{$self->composed_class}) {
17 6         22 my $info = $c->session->{$self->composed_class};
18 6         217 my $thawed = $self->composed_class->thaw($info);
19 6         5387 $thawed->__session($c->session);
20 6         22 $thawed->__stash($c->stash);
21 6         218 $thawed->__key($key);
22              
23 6         44 return $thawed;
24             } else {
25 2         12 my $new = $self->build_new_instance($c, @args, __session=>$c->session, __key=>$key, __stash=>$c->stash);
26 2         2410 $c->stash->{$key} = $new;
27 2         159 return $new;
28             }
29             }
30              
31             around 'BUILDARGS', sub {
32             my ($orig, $self, @args) = @_;
33             my $args = $self->$orig(@args);
34             if($args->{roles}) {
35             push @{$args->{roles}}, 'Catalyst::ComponentRole::StoreToSession';
36             } else {
37             $args->{roles} = ['Catalyst::ComponentRole::StoreToSession'];
38             }
39             return $args;
40             };
41              
42             sub ACCEPT_CONTEXT {
43 8     8 0 283264 my ($self, $c, @args) = @_;
44 8 50       41 return $self->build_new_instance($c, @args) unless blessed $c;
45              
46 8 50       43 unless($c->can('session')) {
47 0         0 die "Can't use a PerSession model adaptor unless you are using the Session Plugin";
48             }
49              
50 8         29 return $self->restore_from_session($c, @args);
51             }
52              
53             __PACKAGE__->meta->make_immutable;
54              
55             =head1 NAME
56              
57             Catalyst::Model::InjectionHelpers::PerSession - Adaptor that returns a session scoped model
58              
59             =head1 SYNOPSIS
60              
61             package MyApp::PerSession;
62              
63             use Moose;
64              
65             sub freeze {
66             my ($self) = @_;
67             return $self->id;
68             }
69              
70             sub thaw {
71             my ($self, $from_session) = @_;
72             return $self->new_from($from_session);
73             }
74              
75             package MyApp;
76              
77             use Catalyst 'InjectionHelper';
78              
79             MyApp->inject_components(
80             'Model::PerRequest' => {
81             from_class=>'MyApp::PerSession',
82             adaptor=>'PerSession',
83             });
84              
85             MyApp->setup;
86            
87             =head1 DESCRIPTION
88              
89             Injection helper adaptor that returns a new model once for session.
90             See L<Catalyst::Plugin::InjectionHelpers> for details. The adapted model
91             MUST provide the following methods:
92              
93             =head2 freeze
94              
95             This method should provide a serialized version of the object suitable for
96             placing in the session. To be safe you should provide a string. We recommend that
97             you provide the smallest possible token useful for restoring a model at a later time,
98             such the primary key of a database row, rather than all the data since session space
99             may be limited, depending on the session type you use.
100              
101             =head2 thaw
102              
103             This receives the serialized version of the object that you created with 'freeze'
104             and you shold use it to restore your object.
105              
106             =head2 cleanup
107              
108             Optional. When calling 'discard' on your model to discard the current saved version
109             you may need to add this method in order to properly cleanup. For example if you
110             save some temporary files as part of freeze, you may wish to remove those.
111              
112             =head1 NOTE
113              
114             We consider this adaptor to be someone experimental since its new and is not based on
115             any existing prior art. Please register issues so we can improve it for the future.
116              
117             =head1 AUTHOR
118              
119             John Napiorkowski L<email:jjnapiork@cpan.org>
120            
121             =head1 SEE ALSO
122            
123             L<Catalyst::Plugin::InjectionHelpers>
124             L<Catalyst>, L<Catalyst::Model::InjectionHelpers::Application>,
125             L<Catalyst::Model::InjectionHelpers::Factory>, L<Catalyst::Model::InjectionHelpers::PerRequest>
126             L<Catalyst::ModelRole::InjectionHelpers>
127              
128             =head1 COPYRIGHT & LICENSE
129            
130             Copyright 2016, John Napiorkowski L<email:jjnapiork@cpan.org>
131            
132             This library is free software; you can redistribute it and/or modify it under
133             the same terms as Perl itself.
134            
135             =cut