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