line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mongol::Roles::Core; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
19658
|
use Moose::Role; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
12
|
|
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
7258
|
use MooseX::ClassAttribute; |
|
2
|
|
|
|
|
96018
|
|
|
2
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
363234
|
use Mongol::Cursor; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
56
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
9
|
use Scalar::Util qw( blessed ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1129
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
requires 'pack'; |
12
|
|
|
|
|
|
|
requires 'unpack'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
class_has 'collection' => ( |
15
|
|
|
|
|
|
|
is => 'rw', |
16
|
|
|
|
|
|
|
isa => 'Maybe[MongoDB::Collection]', |
17
|
|
|
|
|
|
|
default => undef, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'id' => ( |
21
|
|
|
|
|
|
|
is => 'rw', |
22
|
|
|
|
|
|
|
isa => 'Maybe[MongoDB::OID|Str|Num]', |
23
|
|
|
|
|
|
|
lazy_build => 1, |
24
|
|
|
|
|
|
|
); |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
1
|
|
173
|
sub _build_id { undef } |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub find { |
29
|
0
|
|
|
0
|
1
|
|
my ( $class, $query, $options ) = @_; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
32
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $result = $class->collection() |
35
|
|
|
|
|
|
|
->find( $query, $options ) |
36
|
|
|
|
|
|
|
->result(); |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return Mongol::Cursor->new( |
39
|
|
|
|
|
|
|
{ |
40
|
|
|
|
|
|
|
type => $class, |
41
|
|
|
|
|
|
|
result => $result, |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub find_one { |
47
|
0
|
|
|
0
|
1
|
|
my ( $class, $query, $options ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
50
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
my $document = $class->collection() |
53
|
|
|
|
|
|
|
->find_one( $query, {}, $options ); |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
return defined( $document ) ? |
56
|
|
|
|
|
|
|
$class->to_object( $document ) : |
57
|
|
|
|
|
|
|
undef; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub retrieve { |
61
|
0
|
|
|
0
|
1
|
|
my ( $class, $id ) = @_; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
die( 'No identifier provided!' ) |
64
|
|
|
|
|
|
|
unless( defined( $id ) ); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return $class->find_one( { _id => $id } ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub count { |
70
|
0
|
|
|
0
|
1
|
|
my ( $class, $query, $options ) = @_; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
73
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
return $class->collection() |
76
|
|
|
|
|
|
|
->count( $query, $options ); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub exists { |
80
|
0
|
|
|
0
|
1
|
|
my ( $class, $id ) = @_; |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
return $class->count( { _id => $id } ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub update { |
86
|
0
|
|
|
0
|
1
|
|
my ( $class, $query, $update, $options ) = @_; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
89
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $result = $class->collection() |
92
|
|
|
|
|
|
|
->update_many( $query, $update, $options ); |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
return $result->acknowledged() ? |
95
|
|
|
|
|
|
|
$result->modified_count() : |
96
|
|
|
|
|
|
|
undef; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub delete { |
100
|
0
|
|
|
0
|
1
|
|
my ( $class, $query ) = @_; |
101
|
|
|
|
|
|
|
|
102
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
103
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $result = $class->collection() |
106
|
|
|
|
|
|
|
->delete_many( $query ); |
107
|
|
|
|
|
|
|
|
108
|
0
|
0
|
|
|
|
|
return $result->acknowledged() ? |
109
|
|
|
|
|
|
|
$result->deleted_count() : |
110
|
|
|
|
|
|
|
undef; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub save { |
114
|
0
|
|
|
0
|
1
|
|
my $self = shift(); |
115
|
0
|
|
|
|
|
|
my $class = blessed( $self ); |
116
|
|
|
|
|
|
|
|
117
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
118
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my $document = $self->pack(); |
121
|
0
|
|
|
|
|
|
$document->{_id} = delete( $document->{id} ); |
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
unless( defined( $document->{_id} ) ) { |
124
|
0
|
|
|
|
|
|
my $result = $class->collection() |
125
|
|
|
|
|
|
|
->insert_one( $document ); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
$self->id( $result->inserted_id() ); |
128
|
|
|
|
|
|
|
} else { |
129
|
0
|
|
|
|
|
|
$class->collection() |
130
|
|
|
|
|
|
|
->replace_one( { _id => $self->id() }, $document, { upsert => 1 } ); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
return $self; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub remove { |
137
|
0
|
|
|
0
|
1
|
|
my $self = shift(); |
138
|
0
|
|
|
|
|
|
my $class = blessed( $self ); |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
141
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
142
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
die( 'No identifier provided!' ) |
144
|
|
|
|
|
|
|
unless( defined( $self->id() ) ); |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
$class->collection() |
147
|
|
|
|
|
|
|
->delete_one( { _id => $self->id() } ); |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
return $self; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub drop { |
153
|
0
|
|
|
0
|
1
|
|
my $class = shift(); |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
die( 'No collection defined!' ) |
156
|
|
|
|
|
|
|
unless( defined( $class->collection() ) ); |
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
$class->collection() |
159
|
|
|
|
|
|
|
->drop(); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub to_object { |
163
|
0
|
|
|
0
|
1
|
|
my ( $class, $document ) = @_; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$document->{id} = delete( $document->{_id} ); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
return $class->unpack( $document ); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
2
|
|
|
2
|
|
10
|
no Moose::Role; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
13
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
__END__ |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=pod |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 NAME |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Mongol::Roles::Core - Core MongoDB actions and configuration |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head1 SYNOPSIS |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
package Models::Person { |
185
|
|
|
|
|
|
|
use Moose; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
extends 'Mongol::Model'; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
with 'Mongol::Roles::Core'; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
has 'first_name' => ( |
192
|
|
|
|
|
|
|
is => 'ro', |
193
|
|
|
|
|
|
|
isa => 'Str', |
194
|
|
|
|
|
|
|
required => 1, |
195
|
|
|
|
|
|
|
); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
has 'last_name' => ( |
198
|
|
|
|
|
|
|
is => 'ro', |
199
|
|
|
|
|
|
|
isa => 'Str', |
200
|
|
|
|
|
|
|
required => 1, |
201
|
|
|
|
|
|
|
); |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
has 'age' => ( |
204
|
|
|
|
|
|
|
is => 'rw', |
205
|
|
|
|
|
|
|
isa => 'Int', |
206
|
|
|
|
|
|
|
default => 0, |
207
|
|
|
|
|
|
|
); |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
__PACKAGE__->meta()->make_immutable(); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
... |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
my $person = Models::Person->new( |
215
|
|
|
|
|
|
|
{ |
216
|
|
|
|
|
|
|
first_name => 'Steve', |
217
|
|
|
|
|
|
|
last_name => 'Rogers', |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
); |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
$person->save(); |
222
|
|
|
|
|
|
|
printf( "User id: %s\n", $person->id()->to_string() ) |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
$person->age( 70 ); |
225
|
|
|
|
|
|
|
$person->save(); |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 DESCRIPTION |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Mongol core functionality, this takes care of all the basic actions. This role should |
230
|
|
|
|
|
|
|
be applied to master models. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 collection |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
my $collection = Models::Person->collection(); |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
my $collection = MongoDB->connect(...) |
239
|
|
|
|
|
|
|
->get_namespace( 'db.collection' ); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Models::Person->collection( $collection ); |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head2 id |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
my $id = $object->id(); |
246
|
|
|
|
|
|
|
$object->id( $id ); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 METHODS |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
=head2 find |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
my $cursor = Models::Person->find( $query, $options ); |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head2 find_one |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
my $object = Models::Person->find_one( $query, $options ); |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 retrieve |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
my $object = Models::Person->retrieve( $id ); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
Using the provided C<id> values searches for the document in the collection and |
263
|
|
|
|
|
|
|
returns an instance of this model if found or C<undef> otherwise. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head2 count |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
my $count = Models::Person->count( $query, $options ); |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head2 exists |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
my $bool = Models::Person->exists( $id ); |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Checks weather the document with C<id> exists in the collection. Returns a boolean |
274
|
|
|
|
|
|
|
value indicating if the document exists or not. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=head2 update |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
my $count = Models::Person->update( $query, $update, $options ); |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head2 delete |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
my $count = Models::Person->delete( $query ); |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
Removes documents that match the C<$query> form the associated collection. |
285
|
|
|
|
|
|
|
Returns the number of the documents removed or C<undef>. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head2 save |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
$object->save(); |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
Inserts or updates the instance model. |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head2 remove |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
$object->remove(); |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Deletes the current object from the collection using the C<id> property. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=head2 drop |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Models::Person->drop(); |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
Drops the MongoDB collection for this model. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head2 to_object |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
my $object = Models::Person->to_object( $hashref ); |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
Creates a model instance from a hashref document. |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
=head1 SEE ALSO |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=over 4 |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=item * |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
L<MongoDB::Collection> |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=back |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=cut |