Mojito/Collection/CRUD/Mongo.pm

filter: Statementstmt 26.2% 21 / 80 Branchbran 0.0% 0 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 6 / 14 Podpod 75.0% 6 / 8 totaltotal 26.6% 33 / 124 CC 14 SCAR 37.8
CC
14
Cov
21%
update_collection_membership
110.0
create
12.0
collections_for_page
12.0
CRAP 44.0
« Mojito/Template.pm ↑ Summary Mojito/Model/Link.pm »
1 4 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Collection::CRUD::Mongo;
3 4 $Mojito::Collection::CRUD::Mongo::VERSION = '0.25';
4 4 ▶use MongoDB::OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Syntax::Keyword::Junction qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 with('Mojito::Role::DB::Mongo');
11  
12 4 has base_url => ( is => 'rw', );
13  
14 =head1 Methods
15  
16 =head2 create
17  
18 Create a list of document ids in the collection named 'collection'.
19 The funny name is due to the fact we're storing document ids from
20 the notes collection in individual documents of the collection collection.
21 i.e. we are creating (ordered) sets of documents identified with
22 a document name.
23  
24 The motivation for collections of docs collection is so that we can group
25 documents together form display purposes. Form example, say I'm working
26 on a project with multiple documents. I'd like to be able to "identify/tag"
27 them to the project so I can narrow my viewing focus to just those documents.
28  
29 =cut
30  
31 0 ▶sub create {
Subroutine create: not called CC=3 SCAR=24.8
Pod documented
32 0 my ( $self, $params ) = @_;
33  
34 # We don't need to store the form submit value
35 0 delete $params->{collect};
36 0 my $page_ids = $params->{collected_page_ids};
37
38 # NOTE: The $page_ids input can come in two different forms:
39 # - String: "$page_id1,$page_id2,...$page_id_n";
40 # - ArrayRef: [$page_id1,$page_id2,..., $page_id_n];
41 0 ▶ $params->{collected_page_ids} = [split ',', $page_ids] if (!ref($page_ids));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page_ids 0 0 0
42
43 # add save time as last_modified and created
44 0 $params->{last_modified} = $params->{created} = time();
45  
46 0 my $collection = $self->collection->find_one({ collection_name => $params->{collection_name} });
47 0 my $oid;
48 0 ▶ if ( $oid = $collection->{_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oid = $collection->{'_id'}) { } 0 0 0
49 0 $self->collection->update( { '_id' => $oid }, $params );
50 }
51 else {
52 0 $oid = $self->collection->insert($params);
53 }
54
55 0 return $oid->value;
56 }
57  
58 =head2 read
59  
60 Read a collection from the database.
61  
62 =cut
63  
64 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
Pod documented
65 0 my ( $self, $id ) = @_;
66 0 my $oid = MongoDB::OID->new( value => $id );
67 0 return $self->collection->find_one( { _id => $oid } );
68 }
69  
70 =head2 update
71  
72 Update a collection in the database.
73  
74 =cut
75  
76 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
77 0 my ( $self, $params) = @_;
78  
79 0 my $oid = MongoDB::OID->new( value => $params->{id} );
80 0 $params->{last_modified} = time();
81  
82 0 $self->collection->update( { '_id' => $oid }, $params );
83 }
84  
85 =head2 delete
86  
87 Delete a collection from the database.
88  
89 =cut
90  
91 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
92 0 my ( $self, $id ) = @_;
93  
94 0 my $oid = MongoDB::OID->new( value => $id );
95 0 $self->collection->remove( { '_id' => $oid } );
96 }
97  
98 =head2 get_all
99  
100 Get all pages in the notes collection.
101 Returns a MongoDB cursor one can iterate over.
102  
103 =cut
104  
105 0 ▶sub get_all {
Subroutine get_all: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = shift;
107 0 return $self->collection->find;
108 }
109  
110 =head2 collection_for_page
111  
112 Get all the collection ids for which this page is a member of.
113  
114 =cut
115  
116 0 ▶sub collections_for_page {
Subroutine collections_for_page: not called CC=3 SCAR=24.8
Pod undocumented
117 0 my ($self, $page_id) = @_;
118  
119 # NOTE: For some yet to be determined reason I could not
120 # pass {collected_page_ids => $page_id} to $self->collection->find();
121 0 my $collections = $self->get_all;
122 0 my @collection_ids;
123 0 ▶ while (my $doc = $collections->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $collections->next) 0 0 0
124 0 my @collected_pages = @{$doc->{collected_page_ids}};
125 0 ▶ if ($page_id eq any(@collected_pages)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_id eq any(@collected_pages)) 0 0 0
126 0 push @collection_ids, $doc->{_id}->value ;
127 }
128 }
129  
130 0 return @collection_ids;
131 }
132  
133 0 ▶sub update_collection_membership {
Subroutine update_collection_membership: not called CC=10 SCAR=47.0
Pod undocumented
134 0 my ($self, $params) = @_;
135  
136 0 my $collection_ids = $params->{collection_select};
137 # Want to coerce (single select) SCALAR into an ArrayRef (happens w/ Dancer params)
138 0 ▶ if (ref($collection_ids) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $collection_ids ne "ARRAY") 0 0 0
139 0 ▶ warn "Coercing collection select params into an ArrefRef" if $ENV{MOJITO_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"MOJITO_DEBUG"} 0 0 0
140 0 $collection_ids = [$collection_ids];
141 }
142 # Have we assigned the page to at least one collection?
143 0 ▶ if (defined $collection_ids->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $collection_ids->[0]) 0 0 0
144 0 my $cursor = $self->db->collection->find({collected_page_ids => $params->{mongo_id}});
145 0 my %HAVE;
146 0 ▶ while (my $collection = $cursor->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $collection = $cursor->next) 0 0 0
147 0 $HAVE{$collection->{_id}} = 1;
148 }
149 0 my %WANT = map { $_ => 1 } @{$collection_ids};
150 # collection_id of zero in this case means we don't want to assign
151 # the page to any collection
152 0 ▶ %WANT = () if not $collection_ids->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $collection_ids->[0] 0 0 0
153 0 foreach my $collection_id (keys %WANT) {
154 0 ▶ if (not $HAVE{$collection_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($HAVE{$collection_id}) 0 0 0
155 # add page_id to the collection
156 0 my $collection = $self->collector->read($collection_id);
157 0 push @{$collection->{collected_page_ids}}, $params->{mongo_id};
158 0 my $oid = MongoDB::OID->new( value => $collection_id );
159 0 $self->db->collection->update({_id => $oid}, $collection);
160 }
161 }
162 0 foreach my $collection_id (keys %HAVE) {
163 0 ▶ if (not $WANT{$collection_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($WANT{$collection_id}) 0 0 0
164 # remove the page_id from the collection
165 0 my $oid = MongoDB::OID->new( value => $collection_id );
166 $self->db->collection->update(
167 { _id => $oid },
168 { '$pull' => {collected_page_ids => $params->{mongo_id} } },
169 0 );
170 }
171 }
172 }
173
174 }
175  
176 =head2 BUILD
177  
178 Set the collection we want to work with.
179 In this case it's the collection named 'collection'.
180 It's a bit meta is why the funny naming.
181  
182 =cut
183  
184 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
185 0 my $self = shift;
186 0 $self->collection_name('collection');
187 }
188  
189 4 1
« Mojito/Template.pm ↑ Summary Mojito/Model/Link.pm »