EntityModel/Model.pm

filter: Statementstmt 17.8% 38 / 213 Branchbran 12.5% 3 / 24 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.5% 8 / 39 Podpod 54.8% 17 / 31 totaltotal 20.7% 66 / 318 CC 29 SCAR 21.6
CC
29
Cov
17%
update_from
30.0
pending_entities
20.0
commit_pending_add
20.0
CRAP 8.7
« EntityModel/Query/Condition.pm ↑ Summary EntityModel/Query/Table.pm »
1 package EntityModel::Model;
2 {
3 25 $EntityModel::Model::VERSION = '0.102';
4 }
5 25 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 _isa => [qw{Mixin::Event::Dispatch}],
7 name => { type => 'string' },
8 handler => { type => 'hash' },
9 entity => { type => 'array', subclass => 'EntityModel::Entity' },
10 # Private mapping for entity name lookup
11 entity_map => { type => 'hash', scope => 'private', watch => {
12 entity => 'name'
13 } },
14 25 };
15 25 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 EntityModel::Model - base class for model definitions
20  
21 =head1 VERSION
22  
23 version 0.102
24  
25 =head1 SYNOPSIS
26  
27 see L<EntityModel>.
28  
29 =head1 DESCRIPTION
30  
31 see L<EntityModel>.
32  
33  
34  
35 =head1 METHODS
36  
37 =cut
38  
39 25 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 =pod
42  
43 =cut
44  
45 0 ▶sub table {
Subroutine table: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my $self = shift;
47 0 my $name = shift;
48 0 ▶ my ($e) = first { $_->name eq $name } $self->entity->list;
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 return $e;
50 }
51  
52 6 ▶sub entity_by_name {
Subroutine entity_by_name: called CC=1 SCAR=0.0
Pod undocumented
53 6 my $self = shift;
54 6 my $name = shift;
55 6 return $self->entity_map->{$name};
56 }
57  
58 =head2 pending_entities
59  
60 Returns a list of all pending entities for this model.
61  
62 These will be applied on L<commit>, or cleared on L<rollback>.
63  
64 =cut
65  
66 0 ▶sub pending_entities {
Subroutine pending_entities: not called CC=4 SCAR=30.0
Pod documented
67 0 my $self = shift;
68 0 ▶ my $type = shift || [ 'update', 'add', 'remove' ];
Condition0%0/2
shift() || ['update', 'add', 'remove']
l!l
00
MC/DC0%0/1
shift() || ['update', 'add', 'remove']
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
69 0 ▶ $type = [ $type ] unless ref $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $type eq "ARRAY" 0 0 0
70 0 my @lst;
71 0 foreach my $t (@$type) {
72 0 push @lst, values %{$self->{pending}->{$t}};
73 }
74 0 return @lst;
75 }
76  
77 =head2 rollback
78  
79 Revert any pending changes for this model.
80  
81 =cut
82  
83 0 ▶sub rollback {
Subroutine rollback: not called CC=1 SCAR=6.9
Pod documented
84 0 my $self = shift;
85 0 undef $self->{pending};
86 0 return $self;
87 }
88  
89 =head2 apply_fields
90  
91 Apply the field definitions for an entity.
92  
93 =cut
94  
95 0 ▶sub apply_fields {
Subroutine apply_fields: not called CC=3 SCAR=24.8
Pod documented
96 0 my $self = shift;
97 0 my $entity = shift;
98 0 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 logStack("FAILED HERE ===== $_[0]");
100 0 die @_;
101 0 };
102 0 my @fieldList = $self->read_fields($entity);
103 0 foreach my $details (@fieldList) {
104 0 my $field = $entity->new_field($details->{name});
105 0 foreach (sort keys %$details) {
106 0 $field->$_($details->{$_});
107 }
108 0 $entity->field->push($field);
109 }
110 0 return $self;
111 }
112  
113 =head2 load_model
114  
115 Populate the entity model from any information we can get from
116 the data source.
117  
118 =cut
119  
120 0 ▶sub load_model {
Subroutine load_model: not called CC=2 SCAR=17.9
Pod documented
121 0 my $self = shift;
122 0 $self->entity->clear;
123  
124 0 logDebug("Reading tables");
125 0 my @tableList = $self->read_tables;
126 0 logDebug("Import " . scalar(@tableList) . " tables");
127 0 foreach (@tableList) {
128 0 my $tbl = $self->add_table($_);
129 0 $self->apply_fields($tbl);
130 }
131 0 logDebug("Import complete");
132 0 return $self;
133 }
134  
135 =head2 update_from
136  
137 Update this entity model so that it matches the given model.
138  
139 =cut
140  
141 0 ▶sub update_from {
Subroutine update_from: not called CC=5 SCAR=34.0
Pod documented
142 0 my ($self, $src) = @_;
143 0 my %srcNames = map { $_->name => $_ } $src->entity->list;
144 0 foreach my $e ($self->entity->list) {
145 0 ▶ if(exists $srcNames{$e->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $srcNames{$e->name}) { } 0 0 0
146 0 my $es = delete $srcNames{$e->name};
147 # Mark this for update unless it's the same as the one we have already
148 0 ▶ if($es->matches($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($es->matches($e)) { } 0 0 0
149 0 logDebug("Should keep [%s]", $e->name);
150 } else {
151 0 logDebug("Should update [%s]", $e->name);
152 0 $self->{pending}->{update}->{$es->name} = $es;
153 }
154 } else {
155 0 logDebug("Should remove [%s]", $e->name);
156 0 $self->{pending}->{remove}->{$e->name} = $e;
157 }
158 }
159 0 foreach my $name (sort keys %srcNames) {
160 0 logDebug("Should add [%s]", $name);
161 0 $self->{pending}->{add}->{$name} = $srcNames{$name};
162 }
163 0 return $self;
164 }
165  
166 =head2 matches
167  
168 Returns true if this entity model has identical content to another given model.
169  
170 =cut
171  
172 0 ▶sub matches {
Subroutine matches: not called CC=1 SCAR=6.9
Pod documented
173 0 my ($self, $dst) = @_;
174 0 my @srcList = sort { $a->name cmp $b->name } $self->entity->list;
175 0 my @dstList = sort { $a->name cmp $b->name } $dst->entity->list;
176 0 logDebug("Check match: src " . scalar(@srcList) . ", dest " . scalar(@dstList));
177 0 return \@srcList ~~ \@dstList;
178 }
179  
180 =head2 read_tables
181  
182 Virtual method for reading table definitions.
183  
184 =cut
185  
186 0 ▶sub read_tables { }
Subroutine read_tables: not called CC=1 SCAR=0.0
Pod documented
187  
188 0 ▶sub dump {
Subroutine dump: not called CC=3 SCAR=24.8
Pod documented
189 0 my $self = shift;
190 0 ▶ my $out = shift || sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 0 print join(' ', @_) . "\n";
192 0 ▶ };
Condition0%0/3
shift() || sub { print join(' ', @_) . "\n"; }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { print join(' ', @_) . "\n"; }
shift() sub { print join(' ', @_) . "\n"; }
Truth table0%0/3
A || B
A= shift()B= sub { print join(' ', @_) . "\n"; }
ABresult
1X1
011
000
193  
194 0 $out->('Entity list for ' . $self->name);
195 0 foreach (sort { $a->name cmp $b->name } $self->entity) {
196 0 $out->($_->name);
197 0 $_->dump($out);
198 }
199 0 $self;
200 }
201  
202 0 ▶sub apply { }
Subroutine apply: not called CC=1 SCAR=0.0
Pod undocumented
203  
204 0 ▶sub pending {
Subroutine pending: not called CC=2 SCAR=17.9
Pod undocumented
205 0 my $self = shift;
206 0 ▶ return @_ ? ($self->{pending} = shift) : $self->{pending};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
207 }
208  
209 0 ▶sub hasPending {
Subroutine hasPending: not called CC=2 SCAR=17.9
Pod undocumented
210 0 my $self = shift;
211 0 ▶ return $self->pending ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->pending ? : 0 0 0
212 }
213  
214 =head2 new_entity
215  
216 Helper method to create a new entity.
217  
218 =cut
219  
220 0 ▶sub new_entity {
Subroutine new_entity: not called CC=1 SCAR=6.9
Pod documented
221 0 my $self = shift;
222 0 my $name = shift;
223  
224 0 my $entity = EntityModel::Entity->new($name);
225 0 return $entity;
226 }
227  
228 =head2 create_entity
229  
230 Helper method to create a new entity and add it to our list.
231  
232 Takes the following parameters:
233  
234 =over 4
235  
236 =item *
237  
238 =back
239  
240 Returns
241  
242 =cut
243  
244 0 ▶sub create_entity {
Subroutine create_entity: not called CC=1 SCAR=6.9
Pod documented
245 0 my $self = shift;
246 0 $self->add_entity(EntityModel::Entity->new(@_))
247 }
248  
249 =head2 add_table
250  
251 Generate an appropriate L<EntityModel::Entity> for the given table name.
252  
253 =cut
254  
255 0 ▶sub add_table {
Subroutine add_table: not called CC=3 SCAR=24.8
Pod documented
256 0 my ($self, $tbl) = @_;
257 0 my $entity = $self->new_entity($tbl->{name});
258 0 my @fieldList = $self->read_fields($entity);
259 0 my @primaryList = $self->read_primary($entity);
260 0 logDebug("Import " . scalar(@fieldList) . " fields for " . $tbl->{name});
261 0 my %fieldName;
262 0 foreach (@fieldList) {
263 0 my $name = delete $_->{name};
264 # logDebug("Set $name");
265 0 $fieldName{ $name } = $entity->new_field($name, $_);
266 }
267 0 ▶ $entity->primary(join('-', @primaryList)) if @primaryList;
Branch: 1 branch
BranchTrueFalseTotal
if @primaryList 0 0 0
268 # logDebug("Primary for $_ is " . $_->name) foreach $entity->list_primary;
269 0 $self->entity->push($entity);
270 0 return $entity;
271 }
272  
273 =head2 add_entity
274  
275 Add an L<EntityModel::Entity> to this model.
276  
277 =cut
278  
279 14 ▶sub add_entity {
Subroutine add_entity: called CC=1 SCAR=0.0
Pod documented
280 14 my $self = shift;
281 14 my $entity = shift;
282 14 $self->entity->push($entity);
283 14 return $self;
284 }
285  
286 6 ▶sub resolve_entity_dependencies {
Subroutine resolve_entity_dependencies: called CC=3 SCAR=11.0
Pod undocumented
287 6 my $self = shift;
288 6 foreach my $entity ($self->entity->list) {
289 13 my @deps = grep { $_->refer } $entity->field->list;
290 13 $_->refer->entity($self->entity_by_name($_->refer->table)) foreach @deps;
291 }
292 6 return $self;
293 }
294  
295 =head2 commit
296  
297 Apply the actions, starting with the longest names first for removal, and shortest
298 first for update and create.
299  
300 =cut
301  
302 0 ▶sub commit {
Subroutine commit: not called CC=1 SCAR=6.9
Pod documented
303 0 my $self = shift;
304  
305 0 logDebug("Commit $self");
306 0 $self->commit_pending_remove;
307 0 $self->commit_pending_add;
308 0 $self->commit_pending_update;
309 0 delete $self->{pending};
310 # Don't really want to commit here, since having everything in an uncommitted transaction can be useful.
311 # $self->postCommit if $self->can('postCommit');
312 0 return $self->load_model;
313 }
314  
315 =head2 commit_pending_update
316  
317 =cut
318  
319 0 ▶sub commit_pending_update {
Subroutine commit_pending_update: not called CC=2 SCAR=17.9
Pod documented
320 0 my $self = shift;
321 0 logInfo("Update " . join(',', map { $_->name } $self->pending_entities('update')));
322 0 $self->update_table($_) foreach $self->pending_entities('update');
323 0 return $self;
324 }
325  
326 =head2 commit_pending_remove
327  
328 =cut
329  
330 0 ▶sub commit_pending_remove {
Subroutine commit_pending_remove: not called CC=2 SCAR=17.9
Pod documented
331 0 my $self = shift;
332 0 logInfo("Remove " . join(',', map { $_->name } $self->pending_entities('remove')));
333 0 $self->remove_table($_) foreach $self->pending_entities('remove');
334 0 return $self;
335 }
336  
337 =head2 commit_pending_add
338  
339 Add all pending items, ordering to resolve dependencies as required.
340  
341 =cut
342  
343 0 ▶sub commit_pending_add {
Subroutine commit_pending_add: not called CC=4 SCAR=30.0
Pod documented
344 0 my $self = shift;
345 0 logInfo("Create " . join(',', map { $_->name } $self->pending_entities('add')));
346 0 my @pending = $self->pending_entities('add');
347 ITEM:
348 0 ▶ while(@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 0 0 0
349 0 my $e = shift(@pending);
350 # TODO Not hugely efficient, perhaps could do with a profile run here?
351 0 my @deps = map { $_->name } $e->dependencies;
352 0 my @pendingNames = map { $_->name } @pending;
353 0 my @unsatisfied = grep { $_ ~~ @deps } @pendingNames;
354 0 my @existing = map { $_->name } $self->entity->list;
355 # Include current entity in list of available entries, so that we can allow self-reference
356 0 my @unresolved = grep { !($_ ~~ [@pendingNames, @existing, $e->name]) } @deps;
357 0 ▶ if(@unresolved) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unresolved) 0 0 0
358 0 logError("%s unresolved (pending %s, deps %s for %s)", join(',', @unresolved), join(',', @pendingNames), join(',', @deps), $e->name);
359 0 die "Dependency error";
360 }
361 0 ▶ if(@unsatisfied) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unsatisfied) 0 0 0
362 0 logInfo("%s has %d unsatisfied deps, postponing: %s", $e->name, scalar @unsatisfied, join(',',@unsatisfied));
363 0 push @pending, $e;
364 0 next ITEM;
365 }
366 0 $self->create_table($e);
367 }
368 0 return $self;
369 }
370  
371 0 ▶sub remove_entity { shift->remove_table(@_) }
Subroutine remove_entity: not called CC=1 SCAR=6.9
Pod undocumented
372  
373 0 ▶sub remove_table {
Subroutine remove_table: not called CC=1 SCAR=6.9
Pod undocumented
374 0 my $self = shift;
375 0 my $tbl = shift;
376 0 logDebug("Remove table " . $tbl->name);
377 0 ▶ $self->entity->remove(sub { $_[0]->name ne $tbl->name });
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 return $self;
379 }
380  
381 0 ▶sub create_table {
Subroutine create_table: not called CC=1 SCAR=6.9
Pod undocumented
382 0 my $self = shift;
383 0 my $tbl = shift;
384 0 logDebug("Create table " . $tbl->name);
385 0 $self->entity->push($tbl);
386 0 return $self;
387 }
388  
389 0 ▶sub update_table {
Subroutine update_table: not called CC=3 SCAR=24.8
Pod undocumented
390 0 my $self = shift;
391 0 my $src = shift;
392 0 my ($e) = grep { $_->name eq $src->name } $self->entity->list;
393 0 logDebug("Found table [%s] for [%s]", $e->name, $src->name);
394 0 my $dst = $self->entity_map->get($src->name);
395 0 ▶ logDebug("Update table [%s], dest has fields: [%s]", $src->name, join(',', map { $_->name // "undef" } $dst->field->list));
Condition0%0/2
$_->name // "undef"
l!l
00
MC/DC0%0/1
$_->name // "undef"
$_->name
Truth table0%0/2
A
A= $_->name
Aresult
11
00
396 0 my @add = grep { !$dst->field_map->get($_->name) } $src->field->list;
397 0 logDebug("Want to add [%s]", join(',', map { $_->name } @add));
398 0 $self->add_field_to_table($dst, $_) foreach @add;
399 0 return $self;
400 }
401  
402 =head2 add_field_to_table
403  
404 =cut
405  
406 0 ▶sub add_field_to_table {
Subroutine add_field_to_table: not called CC=1 SCAR=6.9
Pod documented
407 0 my $self = shift;
408 0 my $entity = shift;
409 0 my $field = shift;
410 0 $entity->field->push($field->clone);
411 0 return $self;
412 }
413  
414 0 ▶sub handler_for {
Subroutine handler_for: not called CC=1 SCAR=6.9
Pod undocumented
415 0 my $self = shift;
416 0 my $name = shift;
417 0 logDebug("Check for handlers for [%s] node", $name);
418 0 return;
419 }
420  
421 1 ▶sub provide_handler_for {
Subroutine provide_handler_for: called CC=2 SCAR=6.9
Pod undocumented
422 1 my $self = shift;
423 1 my @args = @_;
424 1 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 1 1 2
425 1 my $k = shift(@args);
426 1 my $v = shift(@args);
427 1 $self->handler->set($k, $v);
428 }
429 1 return $self;
430 }
431  
432 1 ▶sub handle_item {
Subroutine handle_item: called CC=2 SCAR=7.1
Pod undocumented
433 1 my $self = shift;
434 1 my %args = @_;
435 1 ▶ if(my $code = $self->handler->get($args{item})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->handler->get($args{'item'})) { } 1 0 1
436 1 logDebug("Handling [%s] with plugin", $args{item});
437 1 $code->($self, item => $args{item}, data => $args{data});
438 } else {
439 0 logError("No handler for [%s]", $args{item});
440 }
441 1 return $self;
442 }
443  
444 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod undocumented
445 0 my $self = shift;
446 0 $self->commit;
447 }
448  
449 =head2 DESTROY
450  
451 Notify when there are pending uncommitted entries.
452  
453 =cut
454  
455 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
456 0 my $self = shift;
457 0 ▶ if($self->hasPending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hasPending) 0 0 0
458 0 logError("Had pending commits for $self");
459 }
460 }
461  
462 25 1;
463  
464 __END__
« EntityModel/Query/Condition.pm ↑ Summary EntityModel/Query/Table.pm »