MongoDB.pm

filter: Statementstmt 86.1% 56 / 65 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 76.5% 85 / 111 CC 14 SCAR 20.0
CC
14
Cov
76%
message
14.3
_get_documents
2.2
new
2.0
CRAP 7.4
↑ Summary MongoDB/Test.pm »
1 package Message::MongoDB;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict; use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use MongoDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Message::MongoDB - Message-oriented interface to MongoDB
10  
11 =head1 VERSION
12  
13 Version 0.1
14  
15 =cut
16  
17 3 our $VERSION = '0.1';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Message::MongoDB;
23  
24 my $mongo = Message::MongoDB->new();
25  
26 $mongo->message({
27 mongo_db => 'my_db',
28 mongo_collection => 'my_collection',
29 mongo_method => 'insert',
30 mongo_write => { a => 'b' },
31 });
32  
33 $mongo->message({
34 mongo_db => 'my_db',
35 mongo_collection => 'my_collection',
36 mongo_method => 'find',
37 mongo_search => { },
38 });
39  
40 #the emit method will be called with an array reference that contains
41 #{a => 'b'}
42  
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 new
47  
48 my $mongo = Message::MongoDB->new();
49  
50 Nothing too interesting at this point.
51  
52 =cut
53  
54 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
55 1 my $class = shift;
56 1 ▶ die "Message::MongoDB::new: even number of argument required\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 0 1 1
57 if scalar @_ % 2;
58 1 my $self = {
59 connection => undef,
60 };
61  
62 1 my %args = @_;
63 1 bless ($self, $class);
64 1 return $self;
65 }
66  
67 11 ▶sub _connect {
Subroutine _connect: called CC=2 SCAR=6.9
68 11 my $self = shift;
69 11 ▶ return if $self->{connection};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"connection"} 10 1 11
70 1 return $self->{connection} = MongoDB::MongoClient->new($self->auth);
71 }
72  
73 11 ▶sub _collection {
Subroutine _collection: called CC=1 SCAR=0.0
74 11 my $self = shift;
75 11 my $db_name = shift;
76 11 my $collection_name = shift;
77 11 $self->_connect();
78 11 my $db = $self->{connection}->get_database($db_name);
79 11 return $db->get_collection($collection_name);
80 }
81  
82 #this is for testing...probably should put it in Test.pm
83 5 ▶sub _get_documents {
Subroutine _get_documents: called CC=2 SCAR=7.8
84 5 my $self = shift;
85 5 my $db_name = shift;
86 5 my $collection_name = shift;
87 5 my $collection = $self->_collection($db_name,$collection_name);
88 5 my $ret = [];
89 5 my $cursor = $collection->find;
90 5 ▶ while(my $doc = $cursor->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $cursor->next) 0 0 0
91 0 push @$ret, $doc;
92 }
93 0 return $ret;
94 }
95  
96  
97 =head2 message
98  
99 $mongo->message({
100 mongo_db => 'my_db',
101 mongo_collection => 'my_collection',
102 mongo_method => 'insert',
103 mongo_write => { a => 'b' },
104 });
105  
106 Execute the specified mongo_method on the specified mongo_db and
107 mongo_collection.
108  
109 =over 4
110  
111 =item * message (first positional, required)
112  
113 =over 4
114  
115 =item * mongo_db (required)
116  
117 Scalar referencing the mongo database to operate on.
118  
119 =item * mongo_collection (required)
120  
121 Scalar referencing the mongo collection to operate on.
122  
123 =item * mongo_method (required)
124  
125 Scalar indicating the mongo method to run. One of
126  
127 =over 4
128  
129 =item * find
130  
131 Requires C<mongo_search>
132  
133 =item * insert
134  
135 Requires C<mongo_write>
136  
137 =item * update
138  
139 Requires C<mongo_search> and C<mongo_write>
140  
141 =item * remove
142  
143 Requires C<mongo_search>
144  
145 =back
146  
147 =item * mongo_search
148  
149 MongoDB search criteria.
150  
151 { a => 'b', c => { '$gt' => 99 } }
152  
153 =item * mongo_write
154  
155 MongoDB 'write' criteria, for update and insert.
156  
157 { a => 'b', x => [1,2,3] } #for insert
158  
159 { a => 'b', c => { '$set' => 100 } } #for update
160  
161 =back
162  
163 =back
164 =cut
165 5 ▶sub message {
Subroutine message: called CC=11 SCAR=26.6
Pod documented
166 5 ▶ my $self = shift or die "Message::MongoDB::message: must be called as a method\n";
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or die "Message::MongoDB::message: must be called as a method\n" 5 0 5
167 5 my $message = shift;
168 5 ▶ die "Message::MongoDB::message: must have at least one argument, a HASH reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $message && ref $message or ref $message ne "HASH" 0 5 5
Condition33%2/6
not $message && ref $message or ref $message ne "HASH"
l!l&&r!l&&!r
005
$message && ref $message
!ll&&!rl&&r
005
MC/DC0%0/3
not $message && ref $message or ref $message ne "HASH"
$message ref $message ref $message ne "HASH"
Truth table25%1/4
not(A && B) or C
A= $messageB= ref $messageC= ref $message ne "HASH"
ABCresult
0XX1
10X1
1111
1100
169 if not $message or
170 not ref $message or
171 ref $message ne 'HASH';
172 5 ▶ die "Message::MongoDB::message: even number of argument required\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 0 5 5
173 if scalar @_ % 2;
174 5 my %args = @_;
175 5 my $mongo_db = $message->{mongo_db};
176 5 my $mongo_collection = $message->{mongo_collection};
177 5 my $mongo_method = $message->{mongo_method};
178 5 my $mongo_write = $message->{mongo_write};
179 5 my $mongo_search = $message->{mongo_search};
180 5 my $coll = $self->_collection($mongo_db, $mongo_collection);
181 5 ▶ if($mongo_method eq 'insert') {
Branch: 4 branches
BranchTrueFalseTotal
if ($mongo_method eq 'insert') { } 2 3 5
elsif ($mongo_method eq 'remove') { } 1 2 3
elsif ($mongo_method eq 'update') { } 1 1 2
elsif ($mongo_method eq 'find') { } 1 0 1
182 2 $coll->insert($mongo_write);
183 } elsif($mongo_method eq 'remove') {
184 1 $coll->remove($mongo_search);
185 } elsif($mongo_method eq 'update') {
186 1 $coll->update($mongo_search, { '$set' => $mongo_write }, {upsert => 1, multiple => 1});
187 } elsif($mongo_method eq 'find') {
188 1 my $cursor = $coll->find($mongo_search);
189 1 my $ret;
190 1 ▶ while(my $doc = $cursor->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $cursor->next) 0 0 0
191 0 push @$ret, $doc;
192 }
193 0 $self->emit(message => $ret);
194 } else {
195 0 die "unknown mongo_method passed: '$mongo_method'";
196 }
197 }
198  
199 =head2 auth
200  
201 This returns the authentication bits necessary to talk to the desired
202 MongoDB.
203  
204 Defaults to all defaults; localhost and port 27017. Over-ride as
205 necessary.
206  
207 =cut
208 1 ▶sub auth {
Subroutine auth: called CC=1 SCAR=0.0
Pod documented
209 1 my $self = shift;
210 1 return ();
211 }
212  
213 =head2 emit
214  
215 $merge->emit(%args)
216  
217 This method is designed to be over-ridden; the default implementation simply
218 adds the outbound message, which is an ARRAY reference of HASHrefs
219 which represents the MongoDB result set, to the package global
220 @Message::MongoDB::return_messages
221  
222 =cut
223 3 our @return_messages = ();
224 0 ▶sub emit {
Subroutine emit: not called CC=1 SCAR=6.9
Pod documented
225 0 my $self = shift;
226 0 my %args = @_;
227 0 push @return_messages, $args{message};
228 0 return \%args;
229 }
230  
231  
232  
233  
234 =head1 AUTHOR
235  
236 Dana M. Diederich, C<< <diederich at gmail.com> >>
237  
238 =head1 BUGS
239  
240 Please report any bugs or feature requests to C<bug-message-mongodb at rt.cpan.org>, or through
241 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Message-MongoDB>. I will be notified, and then you'll
242 automatically be notified of progress on your bug as I make changes.
243  
244  
245  
246  
247 =head1 SUPPORT
248  
249 You can find documentation for this module with the perldoc command.
250  
251 perldoc Message::MongoDB
252  
253  
254 You can also look for information at:
255  
256 =over 4
257  
258 =item * Report bugs and feature requests here
259  
260 L<https://github.com/dana/perl-Message-MongoDB/issues>
261  
262 =item * AnnoCPAN: Annotated CPAN documentation
263  
264 L<http://annocpan.org/dist/Message-MongoDB>
265  
266 =item * CPAN Ratings
267  
268 L<http://cpanratings.perl.org/d/Message-MongoDB>
269  
270 =item * Search CPAN
271  
272 L<https://metacpan.org/module/Message::MongoDB>
273  
274 =back
275  
276  
277 =head1 ACKNOWLEDGEMENTS
278  
279  
280 =head1 LICENSE AND COPYRIGHT
281  
282 Copyright 2013 Dana M. Diederich.
283  
284 This program is free software; you can redistribute it and/or modify it
285 under the terms of the the Artistic License (2.0). You may obtain a
286 copy of the full license at:
287  
288 L<http://www.perlfoundation.org/artistic_license_2_0>
289  
290 Any use, modification, and distribution of the Standard or Modified
291 Versions is governed by this Artistic License. By using, modifying or
292 distributing the Package, you accept this license. Do not use, modify,
293 or distribute the Package, if you do not accept this license.
294  
295 If your Modified Version has been derived from a Modified Version made
296 by someone other than you, you are nevertheless required to ensure that
297 your Modified Version complies with the requirements of this license.
298  
299 This license does not grant you the right to use any trademark, service
300 mark, tradename, or logo of the Copyright Holder.
301  
302 This license includes the non-exclusive, worldwide, free-of-charge
303 patent license to make, have made, use, offer to sell, sell, import and
304 otherwise transfer the Package with respect to any patent claims
305 licensable by the Copyright Holder that are necessarily infringed by the
306 Package. If you institute patent litigation (including a cross-claim or
307 counterclaim) against any party alleging that the Package constitutes
308 direct or contributory patent infringement, then this Artistic License
309 to you shall terminate on the date that such litigation is filed.
310  
311 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
312 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
313 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
314 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
315 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
316 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
317 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
318 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
319  
320  
321 =cut
322  
323 3 1; # End of Message::MongoDB
↑ Summary MongoDB/Test.pm »