line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::EntityModel; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Test::EntityModel::VERSION = '0.102'; |
4
|
|
|
|
|
|
|
} |
5
|
1
|
|
|
1
|
|
28385
|
use EntityModel::Class {}; |
|
1
|
|
|
|
|
263965
|
|
|
1
|
|
|
|
|
9
|
|
6
|
1
|
|
|
1
|
|
2479
|
use parent qw(Exporter); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
7
|
1
|
|
|
1
|
|
1051
|
use EntityModel; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
1591
|
use IO::Async::Loop; |
|
1
|
|
|
|
|
41278
|
|
|
1
|
|
|
|
|
48
|
|
9
|
1
|
|
|
1
|
|
1179
|
use Test::Refcount; |
|
1
|
|
|
|
|
2935
|
|
|
1
|
|
|
|
|
13
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @EXPORT = qw(with_model); |
12
|
|
|
|
|
|
|
our @EXPORT_OK = @EXPORT; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my %model_data = ( |
15
|
|
|
|
|
|
|
simple => { |
16
|
|
|
|
|
|
|
name => 'simple', |
17
|
|
|
|
|
|
|
entity => [ { |
18
|
|
|
|
|
|
|
name => 'first_entity', |
19
|
|
|
|
|
|
|
keyfield => 'name', |
20
|
|
|
|
|
|
|
auto_primary => 1, |
21
|
|
|
|
|
|
|
field => [ |
22
|
|
|
|
|
|
|
{ name => 'name', type => 'text', }, |
23
|
|
|
|
|
|
|
], |
24
|
|
|
|
|
|
|
}, { |
25
|
|
|
|
|
|
|
name => 'second_entity', |
26
|
|
|
|
|
|
|
keyfield => 'name', |
27
|
|
|
|
|
|
|
auto_primary => 1, |
28
|
|
|
|
|
|
|
field => [ |
29
|
|
|
|
|
|
|
{ name => 'name', type => 'text', }, |
30
|
|
|
|
|
|
|
], |
31
|
|
|
|
|
|
|
} ], |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
books => { |
34
|
|
|
|
|
|
|
name => 'books', |
35
|
|
|
|
|
|
|
entity => [ { |
36
|
|
|
|
|
|
|
name => 'author', |
37
|
|
|
|
|
|
|
auto_primary => 1, |
38
|
|
|
|
|
|
|
field => [ |
39
|
|
|
|
|
|
|
{ name => 'name', type => 'text', }, |
40
|
|
|
|
|
|
|
{ name => 'born', type => 'date', }, |
41
|
|
|
|
|
|
|
{ name => 'died', type => 'date', }, |
42
|
|
|
|
|
|
|
], |
43
|
|
|
|
|
|
|
}, { |
44
|
|
|
|
|
|
|
name => 'book', |
45
|
|
|
|
|
|
|
auto_primary => 1, |
46
|
|
|
|
|
|
|
field => [ |
47
|
|
|
|
|
|
|
{ name => 'title', type => 'text', }, |
48
|
|
|
|
|
|
|
{ name => 'published', type => 'date', }, |
49
|
|
|
|
|
|
|
{ name => 'pages', type => 'date', }, |
50
|
|
|
|
|
|
|
], |
51
|
|
|
|
|
|
|
}, { |
52
|
|
|
|
|
|
|
name => 'book_author', |
53
|
|
|
|
|
|
|
primary => [qw(idbook idauthor)], |
54
|
|
|
|
|
|
|
field => [ |
55
|
|
|
|
|
|
|
{ name => 'book', type => 'text', }, |
56
|
|
|
|
|
|
|
{ name => 'author', type => '', }, |
57
|
|
|
|
|
|
|
], |
58
|
|
|
|
|
|
|
} ], |
59
|
|
|
|
|
|
|
}, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub with_model(&;@) { |
63
|
1
|
|
|
1
|
0
|
21
|
my $code = shift; |
64
|
1
|
|
|
|
|
3
|
my %args = @_; |
65
|
1
|
50
|
50
|
|
|
14
|
my $def = $model_data{$args{model} || 'simple'} or die 'unknown model - ' . $args{model}; |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
3
|
my $weak_model; |
68
|
|
|
|
|
|
|
{ |
69
|
1
|
|
|
|
|
3
|
my $model = EntityModel->default_model; |
|
1
|
|
|
|
|
12
|
|
70
|
1
|
|
|
|
|
6
|
Scalar::Util::weaken($weak_model = $model); |
71
|
1
|
|
|
|
|
13
|
$model->add_storage( |
72
|
|
|
|
|
|
|
# TODO extract this |
73
|
|
|
|
|
|
|
'PerlAsync' => { loop => my $loop = IO::Async::Loop->new }, |
74
|
|
|
|
|
|
|
); |
75
|
0
|
|
|
|
|
|
$model->name($def->{name}); |
76
|
0
|
0
|
|
|
|
|
foreach my $entity_def (@{$def->{entity} || []}) { |
|
0
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$model->create_entity(%$entity_def); |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
$code->($model); |
80
|
0
|
|
|
|
|
|
$model->remove_entity($_) for $model->entity->list; |
81
|
0
|
|
|
|
|
|
EntityModel->default_model(undef); |
82
|
|
|
|
|
|
|
} |
83
|
0
|
0
|
|
|
|
|
is_refcount($weak_model, 0, 'have no remaining refs') if $weak_model; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|