File Coverage

blib/lib/Dancer2/Session/MongoDB.pm
Criterion Covered Total %
statement 20 36 55.5
branch n/a
condition n/a
subroutine 7 13 53.8
pod n/a
total 27 49 55.1


line stmt bran cond sub pod time code
1 1     1   7509 use 5.008001;
  1         4  
2 1     1   3 use strict;
  1         1  
  1         16  
3 1     1   2 use warnings;
  1         1  
  1         39  
4              
5             package Dancer2::Session::MongoDB;
6             # ABSTRACT: Dancer 2 session storage with MongoDB
7             our $VERSION = '0.003';
8              
9 1     1   2 use Moo;
  1         1  
  1         8  
10 1     1   218 use MongoDB::MongoClient;
  1         1  
  1         20  
11 1     1   3 use MongoDB::OID;
  1         1  
  1         14  
12 1     1   3 use Dancer2::Core::Types;
  1         4  
  1         12  
13              
14             #--------------------------------------------------------------------------#
15             # Public attributes
16             #--------------------------------------------------------------------------#
17              
18             #pod =attr database_name (required)
19             #pod
20             #pod Name of the database to hold the sessions collection.
21             #pod
22             #pod =cut
23              
24             has database_name => (
25             is => 'ro',
26             isa => Str,
27             required => 1,
28             );
29              
30             #pod =attr collection_name
31             #pod
32             #pod Collection name for storing session data. Defaults to 'dancer_sessions'.
33             #pod
34             #pod =cut
35              
36             has collection_name => (
37             is => 'ro',
38             isa => Str,
39             default => sub { "dancer_sessions" },
40             );
41              
42             #pod =attr client_options
43             #pod
44             #pod Hash reference of configuration options to pass through to
45             #pod L constructor. See that module for details on
46             #pod configuring authentication, replication, etc.
47             #pod
48             #pod =cut
49              
50             has client_options => (
51             is => 'ro',
52             isa => HashRef,
53             default => sub { {} },
54             );
55              
56             #--------------------------------------------------------------------------#
57             # Private attributes
58             #--------------------------------------------------------------------------#
59              
60             has _client => (
61             is => 'lazy',
62             isa => InstanceOf ['MongoDB::MongoClient'],
63             );
64              
65             sub _build__client {
66 0     0     my ($self) = @_;
67 0           return MongoDB::MongoClient->new( $self->client_options );
68             }
69              
70             has _collection => (
71             is => 'lazy',
72             isa => InstanceOf ['MongoDB::Collection'],
73             );
74              
75             sub _build__collection {
76 0     0     my ($self) = @_;
77 0           my $db = $self->_client->get_database( $self->database_name );
78 0           return $db->get_collection( $self->collection_name );
79             }
80              
81             #--------------------------------------------------------------------------#
82             # Role composition
83             #--------------------------------------------------------------------------#
84              
85             with 'Dancer2::Core::Role::SessionFactory';
86              
87             # When saving/retrieving, we need to add/strip the _id parameter
88             # because the Dancer2::Core::Session object keeps them as separate
89             # attributes
90              
91             sub _retrieve {
92 0     0     my ( $self, $id ) = @_;
93 0           my $doc = $self->_collection->find_one( { _id => $id } );
94 0           return $doc->{data};
95             }
96              
97             sub _flush {
98 0     0     my ( $self, $id, $data ) = @_;
99 0           $self->_collection->save( { _id => $id, data => $data }, { safe => 1 } );
100             }
101              
102             sub _destroy {
103 0     0     my ( $self, $id ) = @_;
104 0           $self->_collection->remove( { _id => $id }, { safe => 1 } );
105             }
106              
107             sub _sessions {
108 0     0     my ($self) = @_;
109 0           my $cursor = $self->_collection->query->fields( { _id => 1 } );
110 0           return [ map { $_->{_id} } $cursor->all ];
  0            
111             }
112              
113             1;
114              
115              
116             # vim: ts=4 sts=4 sw=4 et:
117              
118             __END__