File Coverage

blib/lib/CHI/Driver/DBIC.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package CHI::Driver::DBIC;
2              
3 1     1   15108 use 5.008;
  1         3  
  1         43  
4 1     1   4 use strict;
  1         1  
  1         33  
5 1     1   5 use warnings;
  1         5  
  1         31  
6 1     1   496 use Params::Validate qw/:all/;
  1         7730  
  1         190  
7 1     1   11571 use Moose;
  0            
  0            
8             extends 'CHI::Driver';
9              
10             =head2 Attributes
11              
12             =over
13              
14             =item schema
15              
16             The DBIx::Class schema
17              
18             =item resultset
19              
20             The DBIx::Class ResultSet which will be use to operate on the database table.
21             Internally the calls will be $self->schema->($self->resultset) etc.
22              
23             =item column_map
24              
25             A hash ref with the keys key, data and expires_in. Used to map to the table columns.
26             Defaults to:
27              
28             {
29             key => 'id',
30             data => 'session_data',
31             expires_in => 'timestamp'
32             }
33              
34             =item expiry_calc_in
35              
36             =item expiry_calc_out
37              
38             =back
39              
40             =cut
41              
42             # has 'schema' => ( 'is' => 'ro', 'isa' => 'Object', 'required' => 1 );
43             has 'resultset' => ( 'is' => 'ro', 'isa' => 'Object', 'required' => 1 );
44             has 'expiry_calc_in' => (
45             'is' => 'ro',
46             'isa' => 'CodeRef',
47             default => sub {
48             my $self = shift;
49             return sub { my $expiry = shift; $expiry += time(); }
50             },
51             lazy => 1
52             );
53             has 'expiry_calc_out' => (
54             'is' => 'ro',
55             'isa' => 'CodeRef',
56             default => sub {
57             return sub { my ( $self, $expiry ); return $expiry; }
58             },
59             lazy => 1
60             );
61              
62             has 'column_map' => (
63             'is' => 'ro',
64             'isa' => 'HashRef',
65             default => sub {
66             return {
67             key => 'id',
68             data => 'session_data',
69             expires_in => 'expires'
70             };
71             }
72             );
73              
74             has '_rs' => (
75             'is' => 'ro',
76             'isa' => 'Object',
77             'lazy' => 1,
78             'default' => sub { my $self = shift; $self->resultset; }
79             );
80              
81             =head1 NAME
82              
83             CHI::Driver::DBIC - The great new CHI::Driver::DBIC!
84              
85             =head1 VERSION
86              
87             Version 0.001_003
88              
89             =cut
90              
91             our $VERSION = '0.001_003';
92              
93             =head1 SYNOPSIS
94              
95             Quick summary of what the module does.
96              
97             Perhaps a little code snippet.
98              
99             use CHI::Driver::DBIC;
100              
101             my $foo = CHI::Driver::DBIC->new();
102             ...
103              
104             =head1 EXPORT
105              
106             A list of functions that can be exported. You can delete this section
107             if you don't export anything, such as for a purely object-oriented module.
108              
109             =head1 SUBROUTINES/METHODS
110              
111             =cut
112              
113             =head2 store
114              
115             =cut
116              
117             sub store {
118             my ( $self, $key, $data, $expires_in ) = validate_pos( @_, 1, 1, 1, 0 );
119             my $cm = $self->column_map;
120             my $hr = {
121             $cm->{key} => $key,
122             $cm->{data} => $data
123             };
124              
125             $hr->{ $cm->{expires_in} } = $self->expiry_calc_in->($expires_in);
126             $self->_rs->update_or_create($hr);
127             return 1;
128             }
129              
130             =head2 fetch
131              
132             =cut
133              
134             sub fetch {
135             my ( $self, $key ) = validate_pos( @_, 1, 1 );
136              
137             my $id = $self->column_map->{key};
138             my $result = $self->_rs->find( { $id => $key } );
139             return $result->session_data if $result;
140             return;
141             }
142              
143             =head2 remove
144              
145             =cut
146              
147             sub remove {
148             my ( $self, $key ) = validate_pos( @_, 1, 1 );
149             my $result = $self->_rs->find( { $self->column_map->{key} => $key } );
150             $result->delete if $result;
151             return 1;
152             }
153              
154             =head2 clear
155              
156             =cut
157              
158             sub clear {
159             my ($self) = validate_pos( @_, 1 );
160             $self->_rs->search( {} )->delete;
161             return 1;
162             }
163              
164             =head1 AUTHOR
165              
166             Motortrak Ltd, C<< <duncan.garland at motortrak.com> >>
167              
168             =head1 BUGS
169              
170             Please report any bugs or feature requests to C<bug-chi-driver-dbic at rt.cpan.org>, or through
171             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CHI-Driver-DBIC>. I will be notified, and then you'll
172             automatically be notified of progress on your bug as I make changes.
173              
174              
175              
176              
177             =head1 SUPPORT
178              
179             You can find documentation for this module with the perldoc command.
180              
181             perldoc CHI::Driver::DBIC
182              
183              
184             You can also look for information at:
185              
186             =over 4
187              
188             =item * RT: CPAN's request tracker (report bugs here)
189              
190             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CHI-Driver-DBIC>
191              
192             =item * AnnoCPAN: Annotated CPAN documentation
193              
194             L<http://annocpan.org/dist/CHI-Driver-DBIC>
195              
196             =item * CPAN Ratings
197              
198             L<http://cpanratings.perl.org/d/CHI-Driver-DBIC>
199              
200             =item * Search CPAN
201              
202             L<http://search.cpan.org/dist/CHI-Driver-DBIC/>
203              
204             =back
205              
206              
207             =head1 ACKNOWLEDGEMENTS
208              
209              
210             =head1 LICENSE AND COPYRIGHT
211              
212             Copyright 2014 Motortrak Ltd.
213              
214             This program is free software; you can redistribute it and/or modify it
215             under the terms of the the Artistic License (2.0). You may obtain a
216             copy of the full license at:
217              
218             L<http://www.perlfoundation.org/artistic_license_2_0>
219              
220             Any use, modification, and distribution of the Standard or Modified
221             Versions is governed by this Artistic License. By using, modifying or
222             distributing the Package, you accept this license. Do not use, modify,
223             or distribute the Package, if you do not accept this license.
224              
225             If your Modified Version has been derived from a Modified Version made
226             by someone other than you, you are nevertheless required to ensure that
227             your Modified Version complies with the requirements of this license.
228              
229             This license does not grant you the right to use any trademark, service
230             mark, tradename, or logo of the Copyright Holder.
231              
232             This license includes the non-exclusive, worldwide, free-of-charge
233             patent license to make, have made, use, offer to sell, sell, import and
234             otherwise transfer the Package with respect to any patent claims
235             licensable by the Copyright Holder that are necessarily infringed by the
236             Package. If you institute patent litigation (including a cross-claim or
237             counterclaim) against any party alleging that the Package constitutes
238             direct or contributory patent infringement, then this Artistic License
239             to you shall terminate on the date that such litigation is filed.
240              
241             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
242             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
243             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
244             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
245             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
246             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
247             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
248             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249              
250              
251             =cut
252              
253             __PACKAGE__->meta->make_immutable;
254              
255             1; # End of CHI::Driver::DBIC