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