| 1 | package Mandel::Relationship::HasOne; | ||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Mandel::Relationship'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use Mojo::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Mango::BSON 'bson_dbref'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | sub monkey_patch { | ||||||||||||||||||
|
Subroutine
monkey_patch: called CC=1 SCAR=2.7
Pod
documented
| |||||||||||||||||||||
| 7 | 1 | my $self = shift; | |||||||||||||||||||
| 8 | 1 | my $foreign_field = $self->foreign_field; | |||||||||||||||||||
| 9 | 1 | my $accessor = $self->accessor; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | Mojo::Util::monkey_patch( | ||||||||||||||||||||
| 12 | $self->document_class, | ||||||||||||||||||||
| 13 | $accessor, | ||||||||||||||||||||
| 14 | 1 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
cat: called CC=9 SCAR=37.2
| |||||||||||||||||||||
| 15 | 1 | ▶ | my $cb = ref $_[-1] eq 'CODE' ? pop : undef; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | 1 | my $doc = shift; | |||||||||||||||||||
| 17 | 1 | my $obj = shift; | |||||||||||||||||||
| 18 | 1 | my $related_model = $self->_related_model; | |||||||||||||||||||
| 19 | 1 | my $related_collection = $related_model->new_collection($doc->connection); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | ▶ | if ($obj) { # set =========================================================== | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 22 | 1 | ▶ | if (ref $obj eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 1 | $obj = $related_collection->create($obj); | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | $obj->data->{$foreign_field} = bson_dbref $doc->model->collection_name, $doc->id; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | # Blocking | ||||||||||||||||||||
| 29 | 1 | ▶ | unless ($cb) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | $related_collection->search({sprintf('%s.$id', $foreign_field), $doc->id})->remove(); | |||||||||||||||||||
| 31 | 0 | $obj->save; | |||||||||||||||||||
| 32 | 0 | $doc->save; | |||||||||||||||||||
| 33 | 0 | $doc->_cache($accessor => $obj); | |||||||||||||||||||
| 34 | 0 | return $obj; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # Non-blocking | ||||||||||||||||||||
| 38 | Mojo::IOLoop->delay( | ||||||||||||||||||||
| 39 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 40 | 0 | my ($delay) = @_; | |||||||||||||||||||
| 41 | 0 | $related_collection->search({sprintf('%s.$id', $foreign_field), $doc->id})->remove($delay->begin); | |||||||||||||||||||
| 42 | }, | ||||||||||||||||||||
| 43 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 44 | 0 | my ($delay, $err) = @_; | |||||||||||||||||||
| 45 | 0 | ▶ | return $delay->begin(0)->($err) if $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | $doc->save($delay->begin); | |||||||||||||||||||
| 47 | 0 | $obj->save($delay->begin); | |||||||||||||||||||
| 48 | }, | ||||||||||||||||||||
| 49 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 50 | 0 | my ($delay, $o_err, $d_err) = @_; | |||||||||||||||||||
| 51 | 0 | ▶ | my $err = $o_err || $d_err; | ||||||||||||||||||
|
Condition0%0/3
$o_err || $d_err
MC/DC0%0/2
$o_err || $d_err
$o_err
$d_err
Truth table0%0/3
A || B
A= $o_errB= $d_err
| |||||||||||||||||||||
| 52 | 0 | ▶ | $doc->_cache($accessor => $obj) unless $err; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | $doc->$cb($err, $obj); | |||||||||||||||||||
| 54 | }, | ||||||||||||||||||||
| 55 | 1 | ); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | ▶ | elsif (!delete $doc->{fresh} and my $cached = $doc->_cache($accessor)) { # get cached | |||||||||||||||||||
|
Condition0%0/3
not delete $doc->{"fresh"} and my $cached = $doc->_cache($accessor)
MC/DC0%0/2
not delete $doc->{"fresh"} and my $cached = $doc->_cache($accessor)
not delete $doc->{"fresh"}
my $cached = $doc->_cache($accessor)
Truth table0%0/3
not(A) and B
A= not delete $doc->{"fresh"}B= my $cached = $doc->_cache($accessor)
| |||||||||||||||||||||
| 58 | 0 | ▶ | return $cached unless $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $self->$cb('', $cached); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | else { # get ============================================================= | ||||||||||||||||||||
| 62 | 0 | my $cursor = $related_collection->search({sprintf('%s.$id', $foreign_field), $doc->id}); | |||||||||||||||||||
| 63 | 0 | ▶ | return $cursor->single unless $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | ▶ | $cursor->single(sub { $doc->$cb(@_[1, 2]) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 0 | return $doc; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 1 | ); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | return $self; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | 1; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =encoding utf8 | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | Mandel::Relationship::HasOne - A field relates to another mongodb document | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | L<Mandel::Relationship::HasOne> is a class used to describe the relationship | ||||||||||||||||||||
| 85 | between one document that has a relationship to one other documents. | ||||||||||||||||||||
| 86 | The connection between the documents is described in the database using | ||||||||||||||||||||
| 87 | L<DBRef|http://docs.mongodb.org/manual/reference/database-references/>. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 DATABASE STRUCTURE | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | A "dinosaur" that I<has one> "cat" will look like this in the database: | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | mongodb# db.dinosaurs.find({ }) | ||||||||||||||||||||
| 94 | { "_id" : ObjectId("5352b4d8c5483e4502010000") } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | mongodb# db.cats.find({ "dinosaur.$id": ObjectId("53529f28c5483e4977020000") }) | ||||||||||||||||||||
| 97 | { | ||||||||||||||||||||
| 98 | "_id" : ObjectId("5352b4d8c5483e4502040000"), | ||||||||||||||||||||
| 99 | "dinosaur" : DBRef("dinosaurs", ObjectId("5352b4d8c5483e4502010000")) | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =head1 SYNOPSIS | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head2 Using DSL | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | package MyModel::Dinosaur; | ||||||||||||||||||||
| 107 | use Mandel::Document; | ||||||||||||||||||||
| 108 | has_one cat => 'MyModel::Cat'; | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =head2 Using object oriented interface | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | MyModel::Dinosaur->model->relationship( | ||||||||||||||||||||
| 113 | "has_one", | ||||||||||||||||||||
| 114 | "cat", | ||||||||||||||||||||
| 115 | "MyModel::Cat", | ||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head2 Methods generated | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | $cat = MyModel::Dinosaur->new->cat(\%args, $cb); | ||||||||||||||||||||
| 121 | $cat = MyModel::Dinosaur->new->cat($person_obj, $cb); | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | $person_obj = MyModel::Dinosaur->new->cat(\%args); | ||||||||||||||||||||
| 124 | $person_obj = MyModel::Dinosaur->new->cat($person_obj); | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | $person = MyModel::Dinosaur->new->cat; | ||||||||||||||||||||
| 127 | $self = MyModel::Dinosaur->new->cat(sub { my($self, $err, $person) = @_; }); | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | See also L<Mandel::Model/relationship>. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =head1 METHODS | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head2 monkey_patch | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | Add methods to L<Mandel::Relationship/document_class>. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 SEE ALSO | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | L<Mojolicious>, L<Mango>, L<Mandel::Relationship> | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =head1 AUTHOR | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||