Mandel/Relationship/BelongsTo.pm

filter: Statementstmt 50.0% 26 / 52 Branchbran 13.6% 3 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 6 / 9 Podpod 100% 1 / 1 totaltotal 38.3% 36 / 94 CC 14 SCAR 36.9
CC
14
Cov
36%
person
75.2
__ANON__
12.0
__ANON__
6.0
CRAP 40.1
↑ Summary Mandel/Relationship/ListOf.pm »
1 package Mandel::Relationship::BelongsTo;
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 ▶has foreign_field => sub { shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
7  
8 1 ▶sub monkey_patch {
Subroutine monkey_patch: called CC=1 SCAR=3.5
Pod documented
9 1 my $self = shift;
10 1 my $foreign_field = $self->foreign_field;
11 1 my $accessor = $self->accessor;
12  
13 Mojo::Util::monkey_patch(
14 $self->document_class,
15 $accessor,
16 1 ▶ sub {
Subroutine person: called CC=11 SCAR=43.2
17 1 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 1 1
18 1 my $doc = shift;
19 1 my $obj = shift;
20 1 my $related_model = $self->_related_model;
21 1 my $related_collection = $related_model->new_collection($doc->connection);
22  
23 1 ▶ if ($obj) { # set
Branch: 2 branches
BranchTrueFalseTotal
if ($obj) { } 1 0 1
elsif (not delete $doc->{'fresh'} and my $cached = $doc->_cache($accessor)) { } 0 0 0
24 1 ▶ if (UNIVERSAL::isa($obj, 'Mango::BSON::ObjectID')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($obj, "Mango::BSON::ObjectID")) 1 0 1
25 1 $doc->data->{$foreign_field} = bson_dbref $related_model->collection_name, $obj;
26 1 return $obj;
27 }
28 0 ▶ if (ref $obj eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH") 0 0 0
29 0 $obj = $related_collection->create($obj);
30 }
31  
32 0 $doc->data->{$foreign_field} = bson_dbref $related_model->collection_name, $obj->id;
33 0 ▶ $doc->dirty->{$foreign_field} = 1 if $doc->dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $doc->dirty 0 0 0
34  
35 # Blocking
36 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
37 0 $obj->save;
38 0 $doc->save;
39 0 $doc->_cache($accessor => $obj);
40 0 return $obj;
41 }
42  
43 # Non-blocking
44 Mojo::IOLoop->delay(
45 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my ($delay) = @_;
47 0 $obj->save($delay->begin);
48 0 $doc->save($delay->begin);
49 },
50 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
51 0 my ($delay, $o_err, $d_err) = @_;
52 0 ▶ my $err = $o_err || $d_err;
Condition0%0/3
$o_err || $d_err
l!l&&r!l&&!r
000
MC/DC0%0/2
$o_err || $d_err
$o_err $d_err
Truth table0%0/3
A || B
A= $o_errB= $d_err
ABresult
1X1
011
000
53 0 ▶ $doc->_cache($accessor => $obj) unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
54 0 $doc->$cb($err, $obj);
55 },
56 0 );
57 }
58 ▶ 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)
!ll&&!rl&&r
000
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)
ABresult
0X0
100
111
59 0 ▶ return $cached unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
60 0 $self->$cb('', $cached);
61 }
62 else { # get
63 0 my $cursor = $related_collection->search({_id => $doc->data->{$foreign_field}{'$id'}});
64 0 ▶ return $doc->_cache($accessor => $cursor->single) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
65 $cursor->single(
66 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
67 0 my ($cursor, $err, $obj) = @_;
68 0 ▶ $doc->_cache($accessor => $obj) unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
69 0 $doc->$cb($err, $obj);
70 }
71 0 );
72 }
73  
74 0 $doc;
75 }
76 1 );
77  
78 1 return $self;
79 }
80  
81 1 1;
82  
83 =encoding utf8
84  
85 =head1 NAME
86  
87 Mandel::Relationship::BelongsTo - A document is owned by another mongodb document
88  
89 =head1 DESCRIPTION
90  
91 L<Mandel::Relationship::BelongsTo> is a class used to describe the relationship
92 between one document that belongs to another document. The connection
93 between the documents is described in the database using
94 L<DBRef|http://docs.mongodb.org/manual/reference/database-references/>.
95  
96 =head1 DATABASE STRUCTURE
97  
98 A "cat" that I<belongs to> a "person" will look like this in the database:
99  
100 mongodb# db.persons.find();
101 { "_id" : ObjectId("5352abb0c5483e591a010000") }
102  
103 mongodb# db.cats.find({ "person.$id": ObjectId("5352abb0c5483e591a010000") })
104 {
105 "_id" : ObjectId("5352abb0c5483e591a020000"),
106 "person" : DBRef("persons", ObjectId("5352abb0c5483e591a010000"))
107 }
108  
109 =head1 SYNOPSIS
110  
111 =head2 Using DSL
112  
113 package MyModel::Cat;
114 use Mandel::Document;
115 belongs_to owner => 'MyModel::Person';
116  
117 =head2 Using object oriented interface
118  
119 MyModel::Cat
120 ->model
121 ->relationship(belongs_to => owner => 'MyModel::Person');
122  
123 See also L<Mandel::Model/relationship>.
124  
125 =head2 Methods generated
126  
127 # non-blocking set
128 $cat = MyModel::Cat->new->owner(\%args, sub {
129 my($cat, $err, $person_obj) = @_;
130 # ...
131 });
132  
133 $cat = MyModel::Cat->new->owner($person_obj, sub {
134 my($cat, $err, $person_obj) = @_;
135 # ...
136 });
137  
138 # non-blocking get
139 $cat = MyModel::Cat->new->owner(sub {
140 my($cat, $err, $person_obj) = @_;
141 # ...
142 });
143  
144  
145 # blocking set
146 $person_obj = MyModel::Cat->new->owner(\%args);
147 $person_obj = MyModel::Cat->new->owner($person_obj);
148 $person_obj = MyModel::Cat->new->owner($bson_oid);
149  
150 # blocking get
151 $person = MyModel::Cat->new->owner;
152  
153 =head1 ATTRIBUTES
154  
155 L<Mandel::Relationship::BelongsTo> inherits all attributes from
156 L<Mandel::Relationship> and implements the following new ones.
157  
158 =head2 foreign_field
159  
160 The name of the field in this class which hold the "_id" to the related doc.
161  
162 =head1 METHODS
163  
164 L<Mandel::Relationship::BelongsTo> inherits all methods from
165 L<Mandel::Relationship> and implements the following new ones.
166  
167 =head2 monkey_patch
168  
169 Add methods to L<Mandel::Relationship/document_class>.
170  
171 =head1 SEE ALSO
172  
173 L<Mojolicious>, L<Mango>, L<Mandel::Relationship>
174  
175 =head1 AUTHOR
176  
177 Jan Henning Thorsen - C<jhthorsen@cpan.org>
178  
179 =cut
↑ Summary Mandel/Relationship/ListOf.pm »