File Coverage

blib/lib/Mojito/Role/DB/Mongo.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 2     2   13258 use strictures 1;
  2         20  
  2         69  
2             package Mojito::Role::DB::Mongo;
3             {
4             $Mojito::Role::DB::Mongo::VERSION = '0.24';
5             }
6 2     2   220 use Moo::Role;
  2         4  
  2         16  
7 2     2   1696 use MongoDB;
  0            
  0            
8              
9             # Create a database and get a handle on a users collection.
10             has 'conn' => (
11             is => 'ro',
12             lazy => 1,
13             builder => '_build_conn',
14             );
15             has 'db_name' => (
16             is => 'rw',
17             lazy => 1,
18             default => sub { $ENV{RELEASE_TESTING} ? 'mojito_test' : 'mojito' },
19             clearer => 'clear_db_name',
20             );
21             has 'db' => (
22             is => 'rw',
23             lazy => 1,
24             builder => '_build_db',
25             clearer => 'clear_db',
26             );
27             has 'collection' => (
28             is => 'ro',
29             lazy => 1,
30             builder => '_build_collection',
31             clearer => 'clear_collection',
32             );
33             has 'collection_name' => (
34             is => 'rw',
35             lazy => 1,
36             default => sub { 'notes' },
37             clearer => 'clear_collection_name',
38             );
39             has 'db_host' => (
40             is => 'ro',
41             lazy => 1,
42             default => sub { 'localhost:27017' },
43             );
44              
45             sub _build_conn {
46             MongoDB::Connection->new(host => $_[0]->db_host);
47             }
48              
49             sub _build_db {
50             warn "BUILD MONGO DB CONNECTION" if $ENV{MOJITO_DEBUG};
51             # use Devel::StackTrace;
52             # my $trace = Devel::StackTrace->new;
53             # warn $trace->as_string;
54             my $self = shift;
55             my $db_name = $self->db_name;
56             return $self->conn->get_database($db_name);
57             }
58             sub _build_collection {
59             my $self = shift;
60             my $collection_name = $self->collection_name;
61             # Ran into trouble with $self->db not being defined when it seemed
62             # that it should be. Clearing the db attribute resolved it.
63             if (not defined $self->db) {
64             $self->clear_db;
65             }
66             $self->db->get_collection($collection_name);
67             }
68              
69             1;