Mojito/Collection/CRUD/Deep.pm

filter: Statementstmt 36.9% 24 / 65 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 46.6% 7 / 15 Podpod 85.7% 6 / 7 totaltotal 35.9% 37 / 103 CC 8 SCAR 21.5
CC
8
Cov
31%
create
20.0
collections_for_page
20.0
read
6.0
CRAP 8.6
« Mojito/Auth/Mongo.pm ↑ Summary Mojito/Model/Doc/Mongo.pm »
1 4 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Collection::CRUD::Deep;
3 4 $Mojito::Collection::CRUD::Deep::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 List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Syntax::Keyword::Junction qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 with('Mojito::Role::DB::Deep');
12  
13 4 has base_url => ( is => 'rw', );
14  
15 =head1 Methods
16  
17 =head2 create
18  
19 Create a list of document ids in the collection named 'collection'.
20 The funny name is due to the fact we're storing document ids from
21 the notes collection in individual documents of the collection collection.
22 i.e. we are creating (ordered) sets of documents identified with
23 a document name.
24  
25 The motivation for collections of docs collection is so that we can group
26 documents together form display purposes. Form example, say I'm working
27 on a project with multiple documents. I'd like to be able to "identify/tag"
28 them to the project so I can narrow my viewing focus to just those documents.
29  
30 =cut
31  
32 0 ▶sub create {
Subroutine create: not called CC=4 SCAR=30.0
Pod documented
33 0 my ( $self, $params ) = @_;
34  
35 # We don't need to store the form submit value
36 0 delete $params->{collect};
37 0 my $page_ids = $params->{collected_page_ids};
38
39 # NOTE: The $page_ids input can come in two different forms:
40 # - String: "$page_id1,$page_id2,...$page_id_n";
41 # - ArrayRef: [$page_id1,$page_id2,..., $page_id_n];
42 0 ▶ $params->{collected_page_ids} = [split ',', $page_ids] if (!ref($page_ids));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page_ids 0 0 0
43
44 # add save time as last_modified and created
45 0 $params->{last_modified} = $params->{created} = time();
46  
47 0 my $collection_struct = $self->collection->export;
48 0 my @collections = values %{$collection_struct};
49 0 ▶ my $collection = first { $_->{collection_name} eq $params->{collection_name} } @collections;
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my $id;
51 0 ▶ if ( $id = $collection->{_id}||$collection->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id = $collection->{'_id'} || $collection->{'id'}) { } 0 0 0
Condition0%0/3
$collection->{'_id'} || $collection->{'id'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$collection->{'_id'} || $collection->{'id'}
$collection->{'_id'} $collection->{'id'}
Truth table0%0/3
A || B
A= $collection->{'_id'}B= $collection->{'id'}
ABresult
1X1
011
000
52 0 $params->{id} = $id;
53 0 $self->update($params);
54 }
55 else {
56 0 $id = $self->generate_mongo_like_oid;
57 0 $params->{id} = $id;
58 0 $self->collection->{$id} = $params;
59 }
60 0 return $id;
61 }
62  
63 =head2 read
64  
65 Read a collection from the database.
66  
67 =cut
68  
69 0 ▶sub read {
Subroutine read: not called CC=2 SCAR=17.9
Pod documented
70 0 my ( $self, $id ) = @_;
71 0 ▶ if ($self->collection->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->collection->{$id}) 0 0 0
72 0 return $self->collection->{$id}->export;
73 }
74 0 return;
75 }
76  
77 =head2 update
78  
79 Update a collection in the database.
80  
81 =cut
82  
83 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
84 0 my ( $self, $params) = @_;
85  
86 0 my $id = $params->{id};
87 0 $params->{last_modified} = time();
88 0 $self->collection->{$id} = $params;
89 }
90  
91 =head2 delete
92  
93 Delete a collection from the database.
94  
95 =cut
96  
97 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
98 0 my ( $self, $id ) = @_;
99 0 $self->collection->delete($id);
100 }
101  
102 =head2 get_all
103  
104 Get all pages in the notes collection.
105 Returns a MongoDB cursor one can iterate over.
106  
107 =cut
108  
109 0 ▶sub get_all {
Subroutine get_all: not called CC=1 SCAR=6.9
Pod documented
110 0 my $self = shift;
111 0 return $self->collection->export;
112 }
113  
114 =head2 collection_for_page
115  
116 Get all the collection ids for which this page is a member of.
117  
118 =cut
119  
120 0 ▶sub collections_for_page {
Subroutine collections_for_page: not called CC=4 SCAR=30.0
Pod undocumented
121 0 my ( $self, $page_id ) = @_;
122  
123 0 ▶ $page_id //= '';
Condition0%0/2
$page_id //= ""
l!l
00
MC/DC0%0/1
$page_id //= ""
$page_id
Truth table0%0/2
A
A= $page_id
Aresult
11
00
124 0 my @collection_ids = ();
125 0 foreach my $collection (@{$self->get_all}) {
126 0 ▶ if ($page_id eq any(@{$collection->{collected_page_ids}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_id eq any(@{$$collection{"collected_page_ids"};})) 0 0 0
127 0 push @collection_ids, $collection->{id};
128 }
129 }
130  
131 0 return @collection_ids;
132 }
133  
134 =head2 BUILD
135  
136 Set the collection we want to work with.
137 In this case it's the collection named 'collection'.
138 It's a bit meta is why the funny naming.
139  
140 =cut
141  
142 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod documented
143 0 my $self = shift;
144 0 $self->collection_name('collection');
145 }
146  
147 4 1
« Mojito/Auth/Mongo.pm ↑ Summary Mojito/Model/Doc/Mongo.pm »