lib/DBIx/EAV/Entity.pm

filter: Statementstmt 93.0% 200 / 215 Branchbran 70.1% 80 / 114 Conditioncond 64.1% 43 / 67 MC/DCmcdc 18.7% 6 / 32 Subroutinesub 92.8% 26 / 28 Podpod 84.6% 11 / 13 totaltotal 78.0% 366 / 469 SCAR 49.3
CC
89
Cov
82%
_set_related
14.7
_save_related
14.1
save
13.0
CRAP 138.6
« blib/lib/DBIx/EAV/Table.pm ↑ Summary lib/DBIx/EAV/Cursor.pm »
1 package DBIx::EAV::Entity;
2  
3 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 has 'eav', is => 'ro', required => 1;
10 10 has 'type', is => 'ro', required => 1, handles => [qw/ is_type /];
11 92 ▶has 'raw', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 203 ▶has '_modified', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 203 ▶has '_modified_related', is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15  
16  
17 sub in_storage {
18 189 ▶ my $self = shift;
Subroutine in_storage: called CC=2 SCAR=6.9
Pod documented
19 189 ▶ exists $self->raw->{id} && defined $self->raw->{id};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->raw->{"id"} and defined $self->raw->{"id"} 96 93 189
20 }
21  
22 sub id {
23 890 ▶ my $self = shift;
Subroutine id: called CC=2 SCAR=7.1
Pod documented
24 890 ▶ return unless exists $self->raw->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->raw->{"id"} 0 890 890
25 890 $self->raw->{id};
26 }
27  
28  
29  
30 sub get {
31 107 ▶ my $self = shift;
Subroutine get: called CC=7 SCAR=19.6
Pod documented
32 107 my $name = shift;
33 107 my $type = $self->type;
34  
35 107 ▶ return $self->raw->{$name}
Branch: 1 branch
BranchTrueFalseTotal
if $type->has_attribute($name) 87 20 107
36 if $type->has_attribute($name);
37  
38 20 ▶ if ($type->has_relationship($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_relationship($name)) 20 0 20
39 20 my $rel = $type->relationship($name);
40 20 my $rs = $self->_get_related($name, @_);
41 # return an Entity for has_one and belongs_to; return Cursor otherwise
42 return $rs->next if
43 20 ▶ $rel->{is_has_one} || ($rel->{is_has_many} && $rel->{is_right_entity});
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"} 4 16 20
Condition100%6/6
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
l!l&&r!l&&!r
3116
$rel->{"is_has_many"} and $rel->{"is_right_entity"}
!ll&&!rl&&r
971
MC/DC66%2/3
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
$rel->{"is_has_one"} $rel->{"is_has_many"} $rel->{"is_right_entity"}
Truth table75%3/4
A or B and C
A= $rel->{"is_has_one"}B= $rel->{"is_has_many"}C= $rel->{"is_right_entity"}
ABCresult
1XX1
0111
00X0
0100
44  
45 # *_many rel, return cursor or array of entities
46 16 ▶ return wantarray ? $rs->all : $rs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 16 16
47 }
48  
49 0 die sprintf "get() error: '%s' is not a valid attribute/relationship for '%s'", $name, $self->type->name;
50 }
51  
52 sub _get_related {
53 41 ▶ my ($self, $relname, $query, $options) = @_;
Subroutine _get_related: called CC=2 SCAR=6.9
54 41 ▶ $query //= {};
Condition100%2/2
$query //= {}
l!l
239
MC/DC100%1/1
$query //= {}
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
55 41 my $rel = $self->type->relationship($relname);
56 41 $query->{$rel->{incoming_name}} = $self;
57 41 $self->eav->resultset($rel->{entity})->search($query, $options);
58 }
59  
60  
61 sub load_attributes {
62 112 ▶ my ($self, @attrs) = @_;
Subroutine load_attributes: called CC=5 SCAR=16.2
Pod documented
63  
64 112 ▶ die "Can't load_attributes(): this entity has no id!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->id 0 112 112
65 unless defined $self->id;
66  
67 112 my $eav = $self->eav;
68 112 my $type = $self->type;
69  
70 112 ▶ @attrs = $type->attributes( no_static => 1, names => 1 )
Branch: 1 branch
BranchTrueFalseTotal
if @attrs == 0 112 0 112
71 if @attrs == 0;
72  
73 # build sql query: one aliases subselect for each attribute
74 my $sql_query = 'SELECT ' . join(', ', map {
75  
76 112 my $attr_spec = $type->attribute($_);
77 436 my $value_table = $eav->table('value_'. $attr_spec->{data_type} );
78 sprintf "(SELECT value FROM %s WHERE entity_id = %d AND attribute_id = %d) AS %s",
79 $value_table->name,
80 $self->id,
81 $attr_spec->{id},
82 436 $_;
83  
84 } @attrs);
85  
86 # fetch data
87 112 my ($rv, $sth) = $eav->dbh_do($sql_query);
88 112 my $data = $sth->fetchrow_hashref;
89  
90 112 ▶ die "load_attributes() failed! No data returned from database!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 112 112
91 unless ref $data eq 'HASH';
92  
93 112 my $raw = $self->raw;
94 112 my $total = 0;
95  
96 # adopt data
97 112 for (keys %$data) {
98 436 $raw->{$_} = $data->{$_};
99 436 $total++;
100 }
101  
102 # return the number os attrs loaded
103 112 $total;
104 }
105  
106 sub update {
107 0 ▶ my $self = shift;
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
108 0 $self->set(@_)->save;
109 }
110  
111 sub set {
112 98 ▶ my $self = shift;
Subroutine set: called CC=7 SCAR=20.2
Pod documented
113 98 my $numargs = scalar(@_);
114  
115 98 ▶ die 'Call set(\%data) or set($attr, $value)'
Branch: 1 branch
BranchTrueFalseTotal
if 1 > $numargs or $numargs > 2 0 98 98
Condition33%1/3
1 > $numargs or $numargs > 2
l!l&&r!l&&!r
0098
MC/DC0%0/2
1 > $numargs or $numargs > 2
1 > $numargs $numargs > 2
Truth table33%1/3
A or B
A= 1 > $numargsB= $numargs > 2
ABresult
1X1
011
000
116 if 1 > $numargs || $numargs > 2;
117  
118 98 ▶ if ($numargs == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($numargs == 2) { } 2 96 98
elsif ($numargs == 1) { } 96 0 96
119 2 $self->_set(@_);
120 }
121 elsif ($numargs == 1) {
122 96 ▶ die "You must pass a hashref set()" unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 96 96
123 96 ▶ while (my ($k, $v) = each %{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 151 96 247
124 151 $self->_set($k, $v);
125 }
126 }
127  
128 98 $self;
129 }
130  
131 sub _set {
132 153 ▶ my ($self, $attr_name, $value) = @_;
Subroutine _set: called CC=8 SCAR=20.9
133 153 my $type = $self->type;
134  
135 153 ▶ if ($type->has_relationship($attr_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_relationship($attr_name)) 16 137 153
136 16 return $self->_set_related($attr_name, $value);
137 }
138  
139 137 my $attr = $self->type->attribute($attr_name);
140  
141 137 ▶ die "Sorry, you can't set the 'id' attribute."
Branch: 1 branch
BranchTrueFalseTotal
if $attr_name eq "id" 0 137 137
142 if $attr_name eq 'id';
143  
144 # same value
145 return if defined $value &&
146 exists $self->raw->{$attr_name} &&
147 defined $self->raw->{$attr_name} &&
148 137 ▶ $value eq $self->raw->{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and exists $self->raw->{$attr_name} and defined $self->raw->{$attr_name} and $value eq $self->raw->{$attr_name} 0 137 137
Condition88%8/9
defined $value and exists $self->raw->{$attr_name} and defined $self->raw->{$attr_name} and $value eq $self->raw->{$attr_name}
!ll&&!rl&&r
13250
defined $value and exists $self->raw->{$attr_name} and defined $self->raw->{$attr_name}
!ll&&!rl&&r
13115
defined $value and exists $self->raw->{$attr_name}
!ll&&!rl&&r
11306
MC/DC0%0/4
defined $value and exists $self->raw->{$attr_name} and defined $self->raw->{$attr_name} and $value eq $self->raw->{$attr_name}
defined $value exists $self->raw->{$attr_name} defined $self->raw->{$attr_name} $value eq $self->raw->{$attr_name}
Truth table80%4/5
A and B and C and D
A= defined $valueB= exists $self->raw->{$attr_name}C= defined $self->raw->{$attr_name}D= $value eq $self->raw->{$attr_name}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
149  
150 # remember original value
151 $self->_modified->{$attr_name} = $self->raw->{$attr_name}
152 137 ▶ unless exists $self->_modified->{$attr_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_modified->{$attr_name} 135 2 137
153  
154  
155 # set
156 # TODO use type-specific deflator
157 137 $self->raw->{$attr_name} = $value;
158 }
159  
160 sub _set_related {
161 16 ▶ my ($self, $relname, $data) = @_;
Subroutine _set_related: called CC=9 SCAR=26.9
162 16 my $type = $self->type;
163 16 my $rel = $type->relationship($relname);
164  
165 16 ▶ die "You can only pass related data in the form of a hashref, blessed Entity object, or an arrayref of it."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" or ref $data eq "ARRAY" or builtin::blessed($data) and $data->isa("DBIx::EAV::Entity") 0 16 16
Condition33%3/9
ref $data eq "HASH" or ref $data eq "ARRAY" or builtin::blessed($data) and $data->isa("DBIx::EAV::Entity")
l!l&&r!l&&!r
1600
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
1150
builtin::blessed($data) and $data->isa("DBIx::EAV::Entity")
!ll&&!rl&&r
000
MC/DC0%0/4
ref $data eq "HASH" or ref $data eq "ARRAY" or builtin::blessed($data) and $data->isa("DBIx::EAV::Entity")
ref $data eq "HASH" ref $data eq "ARRAY" builtin::blessed($data) $data->isa("DBIx::EAV::Entity")
Truth table40%2/5
A or B or C and D
A= ref $data eq "HASH"B= ref $data eq "ARRAY"C= builtin::blessed($data)D= $data->isa("DBIx::EAV::Entity")
ABCDresult
1XXX1
01XX1
00111
000X0
00100
166 unless ref $data eq 'HASH' || ref $data eq 'ARRAY' || (blessed $data && $data->isa('DBIx::EAV::Entity'));
167  
168 die "You can't pass an arrayref for the '$rel->{name}' relationship."
169 16 ▶ if ref $data eq 'ARRAY' && ( $rel->{is_has_one} || ($rel->{is_has_many} && $rel->{is_right_entity}) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "ARRAY" and $rel->{"is_has_one"} || $rel->{"is_has_many"} && $rel->{"is_right_entity"} 0 16 16
Condition55%5/9
ref $data eq "ARRAY" and $rel->{"is_has_one"} || $rel->{"is_has_many"} && $rel->{"is_right_entity"}
!ll&&!rl&&r
1150
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
l!l&&r!l&&!r
0015
$rel->{"is_has_many"} and $rel->{"is_right_entity"}
!ll&&!rl&&r
1230
MC/DC0%0/4
ref $data eq "ARRAY" and $rel->{"is_has_one"} || $rel->{"is_has_many"} && $rel->{"is_right_entity"}
ref $data eq "ARRAY" $rel->{"is_has_one"} $rel->{"is_has_many"} $rel->{"is_right_entity"}
Truth table40%2/5
A and B or C and D
A= ref $data eq "ARRAY"B= $rel->{"is_has_one"}C= $rel->{"is_has_many"}D= $rel->{"is_right_entity"}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
170  
171 16 $self->raw->{$relname} = $data;
172 16 $self->_modified_related->{$relname} = 1;
173 }
174  
175  
176 sub save {
177 95 ▶ my $self = shift;
Subroutine save: called CC=13 SCAR=25.7
Pod documented
178 95 my $type = $self->type;
179 95 my $entities_table = $self->eav->table('entities');
180 95 my $is_new_entity = not $self->in_storage;
181 95 my $raw = $self->raw;
182  
183 # modified static attrs
184 3 my %modified_static_attributes = map { $_ => $self->raw->{$_} }
185 133 grep { $type->has_static_attribute($_) }
186 95 keys %{$self->_modified};
187  
188 # insert if its new entity
189 95 ▶ if ($is_new_entity) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new_entity) 91 4 95
190  
191 # TODO insert default values
192  
193 91 my $id = $entities_table->insert({
194 %modified_static_attributes,
195 entity_type_id => $type->id,
196 });
197  
198 91 ▶ die "Invalid ID returned ($id) while inserting new entity."
Branch: 1 branch
BranchTrueFalseTotal
unless $id > 0 0 91 91
199 unless $id > 0;
200  
201 91 my $static_attributes = $entities_table->select_one({ id => $id });
202  
203 die "Error: could not fetch the entity row I've just inserted!"
204 91 ▶ unless $static_attributes->{id} == $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $static_attributes->{"id"} == $id 0 91 91
205  
206 $raw->{$_} = $static_attributes->{$_}
207 91 for keys %$static_attributes;
208  
209 # undirty those attrs
210 91 delete $self->_modified->{$_} for keys %modified_static_attributes;
211 91 %modified_static_attributes = ();
212 }
213  
214 # upsert attributes
215 95 my $modified_count = 0;
216  
217 95 ▶ while (my ($attr_name, $old_value) = each %{$self->_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_modified;}) 132 95 227
218  
219 132 $modified_count++;
220 132 my $value = $raw->{$attr_name};
221 132 my $attr_spec = $self->type->attribute($attr_name);
222  
223 # save static attrs later
224 132 ▶ if ($attr_spec->{is_static}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_spec->{"is_static"}) 2 130 132
225 2 $modified_static_attributes{$attr_name} = $value;
226 2 next;
227 }
228  
229 130 my $values_table = $self->eav->table('value_'.$attr_spec->{data_type});
230  
231 my %attr_criteria = (
232 entity_id => $self->id,
233 attribute_id => $attr_spec->{id}
234 130 );
235  
236 # undefined value, delete attribute row
237 130 ▶ if (not defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 1 129 130
elsif (defined $old_value) { } 1 128 129
238 1 $values_table->delete(\%attr_criteria);
239 }
240  
241 # update or insert value
242 elsif (defined $old_value) {
243 1 $values_table->update({ value => $value }, \%attr_criteria);
244 }
245 else {
246 128 $values_table->insert({
247 %attr_criteria,
248 value => $value
249 });
250 }
251 }
252  
253 # upset related
254 95 foreach my $relname (keys %{$self->_modified_related}) {
255 16 $self->_save_related($relname, $self->raw->{$relname});
256 }
257  
258  
259 # update static attributes
260 95 ▶ if ($modified_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified_count > 0) 95 0 95
261  
262 95 ▶ $entities_table->update(\%modified_static_attributes, { id => $self->id })
Branch: 1 branch
BranchTrueFalseTotal
if keys %modified_static_attributes > 0 1 94 95
263 if keys(%modified_static_attributes) > 0;
264 }
265  
266 # undirty
267 95 %{$self->_modified} = ();
268  
269 95 $self;
270 }
271  
272 sub _save_related {
273 18 ▶ my ($self, $relname, $data, $options) = @_;
Subroutine _save_related: called CC=13 SCAR=26.4
274 18 ▶ $options //= {};
Condition100%2/2
$options //= {}
l!l
216
MC/DC100%1/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
275  
276 18 my $rel = $self->type->relationship($relname);
277 18 my $related_type = $self->eav->type($rel->{entity});
278 18 ▶ my ($our_side, $their_side) = $rel->{is_right_entity} ? qw/ right left / : qw/ left right /;
Branch: 1 branch
BranchTrueFalseTotal
$rel->{'is_right_entity'} ? : 0 18 18
279  
280 # delete any old links
281 18 my $relationship_table = $self->eav->table('entity_relationships');
282 $relationship_table->delete({
283 relationship_id => $rel->{id},
284 $our_side."_entity_id" => $self->id
285 18 ▶ }) unless $options->{keep_current_links};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"keep_current_links"} 16 2 18
286  
287 # link new objects
288 18 ▶ foreach my $entity (ref $data eq 'ARRAY' ? @$data : ($data)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 17 1 18
289  
290 # if is a blessed object, check its a entity from the correct type
291 51 ▶ if (blessed $entity) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($entity)) { } 22 29 51
elsif (ref $entity eq 'HASH') { } 29 0 29
292  
293 22 ▶ die "Can't save data for relationship '$relname': unknown data type: ". ref $entity
Branch: 1 branch
BranchTrueFalseTotal
unless $entity->isa("DBIx::EAV::Entity") 0 22 22
294 unless $entity->isa('DBIx::EAV::Entity');
295  
296 22 ▶ die sprintf("relationship '%s' requires '%s' objects, not '%s'", $relname, $related_type->name, $entity->type->name)
Branch: 1 branch
BranchTrueFalseTotal
unless $entity->type->id == $related_type->id 0 22 22
297 unless $entity->type->id == $related_type->id;
298  
299 22 ▶ die "Can't save data for relationship '$relname': related entity is not in_storage."
Branch: 1 branch
BranchTrueFalseTotal
unless $entity->in_storage 0 22 22
300 unless $entity->in_storage;
301  
302 # remove any links to it
303 $relationship_table->delete({
304 relationship_id => $rel->{id},
305 $their_side."_entity_id" => $entity->id
306  
307 22 ▶ }) unless $rel->{is_many_to_many};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel->{"is_many_to_many"} 1 21 22
308  
309 }
310 elsif (ref $entity eq 'HASH') {
311  
312 # insert new entity
313 29 $entity = $self->eav->resultset($related_type->name)->insert($entity);
314 }
315 else {
316 0 die "Can't save data for relationship '$relname': unknown data type: ". ref $entity;
317 }
318  
319 # create link
320 $relationship_table->insert({
321 relationship_id => $rel->{id},
322 51 ▶ $our_side."_entity_id" => $self->id,
Branch: 1 branch
BranchTrueFalseTotal
$relationship_table->insert({"relationship_id", $rel->{"id"}, $our_side . '_entity_id', $self->id, $their_side . '_entity_id', $entity->id}) or die "Error creating link for relationship '${relname}'" 0 51 51
323 $their_side."_entity_id" => $entity->id
324 }) or die "Error creating link for relationship '$relname'";
325 }
326 }
327  
328 sub add_related {
329 3 ▶ my ($self, $relname, $data) = @_;
Subroutine add_related: called CC=4 SCAR=14.0
Pod documented
330 3 my $rel = $self->type->relationship($relname);
331 die "Can't call add_related() for relationship '$rel->{name}'"
332 3 ▶ if $rel->{is_has_one} || ($rel->{is_has_many} && $rel->{is_right_entity});
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"} 1 2 3
Condition66%4/6
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
l!l&&r!l&&!r
102
$rel->{"is_has_many"} and $rel->{"is_right_entity"}
!ll&&!rl&&r
110
MC/DC33%1/3
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
$rel->{"is_has_one"} $rel->{"is_has_many"} $rel->{"is_right_entity"}
Truth table50%2/4
A or B and C
A= $rel->{"is_has_one"}B= $rel->{"is_has_many"}C= $rel->{"is_right_entity"}
ABCresult
1XX1
0111
00X0
0100
333  
334 2 $self->_save_related($relname, $data, { keep_current_links => 1 });
335 }
336  
337  
338 sub remove_related {
339 3 ▶ my ($self, $relname, $data) = @_;
Subroutine remove_related: called CC=10 SCAR=25.6
Pod documented
340 3 my $rel = $self->type->relationship($relname);
341  
342 die "Can't call add_related() for relationship '$rel->{name}'"
343 3 ▶ if $rel->{is_has_one} || ($rel->{is_has_many} && $rel->{is_right_entity});
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"} 1 2 3
Condition66%4/6
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
l!l&&r!l&&!r
102
$rel->{"is_has_many"} and $rel->{"is_right_entity"}
!ll&&!rl&&r
110
MC/DC33%1/3
$rel->{"is_has_one"} or $rel->{"is_has_many"} and $rel->{"is_right_entity"}
$rel->{"is_has_one"} $rel->{"is_has_many"} $rel->{"is_right_entity"}
Truth table50%2/4
A or B and C
A= $rel->{"is_has_one"}B= $rel->{"is_has_many"}C= $rel->{"is_right_entity"}
ABCresult
1XX1
0111
00X0
0100
344  
345 2 my $relationships_table = $self->eav->table('entity_relationships');
346 2 ▶ my ($our_side, $their_side) = $rel->{is_right_entity} ? qw/ right left / : qw/ left right /;
Branch: 1 branch
BranchTrueFalseTotal
$rel->{'is_right_entity'} ? : 0 2 2
347  
348 2 ▶ $data = [$data] unless ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "ARRAY" 2 0 2
349  
350 2 foreach my $entity (@$data) {
351  
352 die "remove_related() error: give me an instance of '$rel->{entity}' or an arrayref of it."
353 2 ▶ unless blessed $entity && $entity->isa('DBIx::EAV::Entity') && $entity->type->name eq $rel->{entity};
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($entity) and $entity->isa("DBIx::EAV::Entity") and $entity->type->name eq $rel->{"entity"} 0 2 2
Condition33%2/6
builtin::blessed($entity) and $entity->isa("DBIx::EAV::Entity") and $entity->type->name eq $rel->{"entity"}
!ll&&!rl&&r
002
builtin::blessed($entity) and $entity->isa("DBIx::EAV::Entity")
!ll&&!rl&&r
002
MC/DC0%0/3
builtin::blessed($entity) and $entity->isa("DBIx::EAV::Entity") and $entity->type->name eq $rel->{"entity"}
builtin::blessed($entity) $entity->isa("DBIx::EAV::Entity") $entity->type->name eq $rel->{"entity"}
Truth table25%1/4
A and B and C
A= builtin::blessed($entity)B= $entity->isa("DBIx::EAV::Entity")C= $entity->type->name eq $rel->{"entity"}
ABCresult
0XX0
10X0
1100
1111
354  
355 $relationships_table->delete({
356 relationship_id => $rel->{id},
357 2 $our_side ."_entity_id" => $self->id,
358 $their_side."_entity_id" => $entity->id
359 });
360 }
361 }
362  
363  
364 sub discard_changes {
365 0 ▶ my $self = shift;
Subroutine discard_changes: not called CC=2 SCAR=17.9
Pod documented
366  
367 0 ▶ while (my ($k, $v) = each %{$self->_modified}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->_modified;}) 0 0 0
368 0 $self->raw->{$k} = $v;
369 0 delete $self->raw->{$k};
370 }
371  
372 0 $self;
373 }
374  
375  
376 sub delete {
377 24 ▶ my $self = shift;
Subroutine delete: called CC=10 SCAR=25.0
Pod documented
378 24 ▶ die "Can't delete coz I'm not in storage!"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_storage 0 24 24
379 unless $self->in_storage;
380  
381 24 my $eav = $self->eav;
382 24 my $type = $self->type;
383  
384 # cascade delete child entities
385 24 foreach my $rel ($type->relationships) {
386  
387 next if $rel->{is_right_entity}
388 || $rel->{is_many_to_many}
389 53 ▶ || (exists $rel->{cascade_delete} && $rel->{cascade_delete} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $rel->{"is_right_entity"} or $rel->{"is_many_to_many"} or exists $rel->{"cascade_delete"} and $rel->{"cascade_delete"} == 0 6 21 27
Condition66%6/9
$rel->{"is_right_entity"} or $rel->{"is_many_to_many"} or exists $rel->{"cascade_delete"} and $rel->{"cascade_delete"} == 0
l!l&&r!l&&!r
32021
$rel->{"is_right_entity"} or $rel->{"is_many_to_many"}
l!l&&r!l&&!r
26621
exists $rel->{"cascade_delete"} and $rel->{"cascade_delete"} == 0
!ll&&!rl&&r
2100
MC/DC0%0/4
$rel->{"is_right_entity"} or $rel->{"is_many_to_many"} or exists $rel->{"cascade_delete"} and $rel->{"cascade_delete"} == 0
$rel->{"is_right_entity"} $rel->{"is_many_to_many"} exists $rel->{"cascade_delete"} $rel->{"cascade_delete"} == 0
Truth table40%2/5
A or B or C and D
A= $rel->{"is_right_entity"}B= $rel->{"is_many_to_many"}C= exists $rel->{"cascade_delete"}D= $rel->{"cascade_delete"} == 0
ABCDresult
1XXX1
01XX1
00111
000X0
00100
390  
391 21 my $rs = $self->_get_related($rel->{name});
392 21 ▶ while (my $related_entity = $rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $related_entity = $rs->next) 6 21 27
393 6 $related_entity->delete;
394 }
395 }
396  
397 24 ▶ unless ($eav->schema->database_cascade_delete) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($eav->schema->database_cascade_delete) 0 24 24
398  
399 # delete relationship links
400 0 $eav->table('entity_relationships')->delete([
401 { left_entity_id => $self->id },
402 { right_entity_id => $self->id }
403 ]);
404  
405 # delete attributes
406 0 my %data_types = map { $_->{data_type} => 1 }
407 $type->attributes( no_static => 1 );
408  
409 0 foreach my $data_type (keys %data_types) {
410 0 $eav->table('value_'.$data_type)->delete({ entity_id => $self->id });
411 }
412 }
413  
414 # delete entity
415 24 my $entities_table = $self->eav->table('entities');
416 24 my $rv = $entities_table->delete({ id => $self->id });
417 24 delete $self->raw->{id}; # not in_storage
418 24 $rv;
419 }
420  
421  
422 ## ##
423 ## Class Methods ##
424 ## ##
425  
426 sub is_custom_class {
427 20 ▶ my $class = shift;
Subroutine is_custom_class: called CC=2 SCAR=7.1
Pod undocumented
428 20 ▶ croak "is_custom_class() is a Class method." if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 20 20
429 20 $class ne __PACKAGE__;
430 }
431  
432 sub type_definition {
433 11 ▶ my $class = shift;
Subroutine type_definition: called CC=5 SCAR=16.1
Pod undocumented
434  
435 11 ▶ croak "type_definition() is a Class method." if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 11 11
436 11 ▶ croak "type_definition() must be called on DBIx::EAV::Entity subclasses."
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_custom_class 0 11 11
437 unless $class->is_custom_class;
438  
439 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
440 11 ▶ unless (defined *{"${class}::__TYPE_DEFINITION__"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined *{"${class}::__TYPE_DEFINITION__";}) 3 8 11
441  
442 3 my %definition;
443 # detect parent entity
444 3 my $parent_class = ${"${class}::ISA"}[0];
445 3 ▶ ($definition{extends}) = $parent_class =~ /::(\w+)$/
Branch: 1 branch
BranchTrueFalseTotal
if $parent_class ne __PACKAGE__ 1 2 3
446 if $parent_class ne __PACKAGE__;
447  
448 3 *{"${class}::__TYPE_DEFINITION__"} = \%definition;
449 }
450  
451  
452 11 \%{"${class}::__TYPE_DEFINITION__"};
453 }
454  
455 # install class methods for type definition
456 10 foreach my $stuff (qw/ attribute has_many has_one many_to_many /) {
457 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
458 40 *{$stuff} = sub {
459 7 ▶ my ($class, $spec) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
460  
461 7 ▶ croak "$stuff() is a Class method." if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 7 7
462 7 ▶ croak "$stuff() must be called on DBIx::EAV::Entity subclasses."
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_custom_class 0 7 7
463 unless $class->is_custom_class;
464  
465 7 ▶ my $key = $stuff eq 'attribute' ? 'attributes' : $stuff;
Branch: 1 branch
BranchTrueFalseTotal
$stuff eq 'attribute' ? : 6 1 7
466 7 push @{ $class->type_definition->{$key} }, $spec;
467 40 };
468 }
469  
470  
471  
472 10 1;
473  
474  
475 __END__
« blib/lib/DBIx/EAV/Table.pm ↑ Summary lib/DBIx/EAV/Cursor.pm »