blib/lib/Dancer/Session/KiokuDB.pm

filter: Statementstmt 95.0% 58 / 61 Branchbran 70.0% 7 / 10 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 90.9% 90 / 99 CC 8 SCAR 12.9
CC
8
Cov
91%
init
8.1
destroy
2.0
BEGIN
1.0
CRAP 3.6
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Dancer::Session::KiokuDB;
4 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 $Dancer::Session::KiokuDB::VERSION = '0.05';
6 }
7 # ABSTRACT: KiokuDB Dancer session backend
8  
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use base 'Dancer::Session::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use KiokuDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # to have access to configuration data and a helper for paths
15 3 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Dancer::FileUtils 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 my ( $db, $warned );
21  
22 3 ▶sub init {
Subroutine init: called CC=8 SCAR=21.0
Pod documented
23 3 my $self = shift;
24 3 ▶ my $backend = setting('kiokudb_backend') || 'Hash';
Condition100%2/2
setting('kiokudb_backend') || 'Hash'
l!l
12
MC/DC100%1/1
setting('kiokudb_backend') || 'Hash'
setting('kiokudb_backend')
Truth table100%2/2
A
A= setting('kiokudb_backend')
Aresult
11
00
25 3 my $class = "KiokuDB::Backend::$backend";
26 3 my %opts = ();
27  
28 3 $self->SUPER::init(@_);
29  
30 # making sure that if we get backend opts, they're a hashref
31 3 ▶ if ( my $opts = setting('kiokudb_backend_opts') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = setting("kiokudb_backend_opts")) 2 1 3
32 2 ▶ if ( ref $opts and ref $opts eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts and ref $opts eq 'HASH') { } 1 1 2
Condition66%2/3
ref $opts and ref $opts eq "HASH"
!ll&&!rl&&r
011
MC/DC50%1/2
ref $opts and ref $opts eq "HASH"
ref $opts ref $opts eq "HASH"
Truth table66%2/3
A and B
A= ref $optsB= ref $opts eq "HASH"
ABresult
0X0
100
111
33 1 %opts = %{$opts};
34 } else {
35 1 croak 'kiokudb_backend_opts must be a hash reference';
36 }
37 }
38  
39 # default is to create
40 2 ▶ defined $opts{'create'} or $opts{'create'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"create"} or $opts{"create"} = 1 0 2 2
41  
42 2 ▶ if ( not $warned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($warned) 2 0 2
43 2 Dancer::Logger::warning("No session KiokuDB backend, using 'Hash'");
44 2 $warned++;
45 }
46  
47 2 ▶ Dancer::ModuleLoader->load($class)
Branch: 1 branch
BranchTrueFalseTotal
"Dancer::ModuleLoader"->load($class) or croak("Cannot load ${class}: perhaps you need to install it?") 2 0 2
48 or croak "Cannot load $class: perhaps you need to install it?";
49  
50 2 $db = KiokuDB->new(
51 backend => $class->new(%opts),
52 allow_classes => ['Dancer::Session::KiokuDB'],
53 );
54 }
55  
56 1 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
57 1 my $class = shift;
58 1 my $self = $class->new;
59  
60 1 $self->flush;
61  
62 1 return $self;
63 }
64  
65 1 ▶sub retrieve {
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
66 1 my $self = shift;
67 1 my ($id) = @_;
68 1 my $scope = $db->new_scope;
69  
70 # return object
71 1 return $db->lookup($id);
72 }
73  
74 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
75 0 my $self = shift;
76 0 my $scope = $db->new_scope;
77  
78 0 $db->delete($self);
79 }
80  
81 1 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
82 1 my $self = shift;
83 1 my $id = $self->{'id'};
84 1 my $scope = $db->new_scope;
85  
86 1 $db->insert( $id => $self );
87 }
88  
89 3 1;
90  
91  
92  
93 =pod
94  
95 =head1 NAME
96  
97 Dancer::Session::KiokuDB - KiokuDB Dancer session backend
98  
99 =head1 VERSION
100  
101 version 0.05
102  
103 =head1 SYNOPSIS
104  
105 # in your Dancer app:
106 setting session => 'KiokuDB';
107 setting kiokudb_backend => 'DBI';
108 setting kiokudb_backend_opts => {
109 dsn => 'dbi:SQLite:dbname=mydb.sqlite',
110 };
111  
112 # or in your Dancer config file:
113 session: 'KiokuDB'
114 kiokudb_backend: 'DBI'
115 kiokudb_backend_opts:
116 dsn: 'dbi:SQLite:dbname=mydb.sqlite'
117  
118 =head1 DESCRIPTION
119  
120 When you want to save session information, you can pick from various session
121 backends, and they each determine how the session information will be saved. You
122 can use L<Dancer::Session::Cookie>, L<Dancer::Session::MongoDB> or...
123 you use L<Dancer::Session::KiokuDB>.
124  
125 This backend uses L<KiokuDB> to save and access session data.
126  
127 =head1 OPTIONS
128  
129 =head2 kiokudb_backend
130  
131 A string which specifies what backend to use, under C<KiokuDB::Backend>, that
132 means that backend I<DBI> will be C<KiokuDB::Backend::DBI>. If you'll get smart
133 and provide I<KiokuDB::Backend::Cool>, you'll get
134 C<KiokuDB::Backend::KiokuDB::Backend::Cool>, which is, evidently, not cool! :)
135  
136 Not mandatory.
137  
138 The default backend is L<KiokuDB::Backend::Hash>.
139  
140 =head2 kiokudb_backend_opts
141  
142 A hash reference which indicates options you want to send to the backend's
143 C<new()> method.
144  
145 Not mandatory.
146  
147 The default opts are C<<create => 1>>. If you do not want it to automatically
148 create, set:
149  
150 # in your app
151 set kiokudb_backend_opts => {
152 create => 0,
153 ...
154 };
155  
156 # or in your configuration
157 kiokudb_backend_opts:
158 create: 0
159  
160 =head1 SUBROUTINES/METHODS
161  
162 =head2 init
163  
164 Initializes the object by loading the proper KiokuDB backend and creating the
165 initial connection.
166  
167 =head2 create
168  
169 Creates a new object, runs C<flush> and returns the object.
170  
171 =head2 flush
172  
173 Writes the session information to the KiokuDB session database.
174  
175 =head2 retrieve
176  
177 Retrieves session information from the KiokuDB session database.
178  
179 =head2 destroy
180  
181 Deletes session information from the KiokuDB session database.
182  
183 =head1 SEE ALSO
184  
185 The Dancer Advent Calendar 2010.
186  
187 =head1 AUTHOR
188  
189 Sawyer X <xsawyerx@cpan.org>
190  
191 =head1 COPYRIGHT AND LICENSE
192  
193 This software is copyright (c) 2010 by Sawyer X.
194  
195 This is free software; you can redistribute it and/or modify it under
196 the same terms as the Perl 5 programming language system itself.
197  
198 =cut
199  
200  
201 __END__
↑ Summary