EntityModel/Storage/Perl.pm

filter: Statementstmt 13.6% 16 / 117 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 19.0% 4 / 21 Podpod 100% 14 / 14 totaltotal 16.6% 34 / 204 CC 26 SCAR 29.5
CC
26
Cov
10%
find
56.0
adjacent
30.0
store
20.0
CRAP 19.1
« EntityModel/Support/Perl.pm ↑ Summary EntityModel/Storage.pm »
1 package EntityModel::Storage::Perl;
2 {
3 2 $EntityModel::Storage::Perl::VERSION = '0.102';
4 }
5 2 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 _isa => [qw{EntityModel::Storage}],
7 2 };
8 2 ▶no if $] >= 5.017011, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 EntityModel::Storage::Perl - backend storage interface for L<EntityModel>
13  
14 =head1 VERSION
15  
16 version 0.102
17  
18 =head1 SYNOPSIS
19  
20 See L<EntityModel>.
21  
22 =head1 DESCRIPTION
23  
24 See L<EntityModel>.
25  
26 This does not really qualify as a 'storage' module, since it's intended purely for use in
27 testing, providing an ephemeral backing store for entities which will disappear on program
28 termination.
29  
30 =cut
31  
32 2 ▶use List::MoreUtils qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Used for holding any entities that have been created
35 2 my %EntityMap;
36  
37 # Max ID information, used for sequences
38 my %EntityMaxID;
39  
40 =head1 METHODS
41  
42 =cut
43  
44 =head2 setup
45  
46 =cut
47  
48 0 ▶sub setup {
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
49 0 my $self = shift;
50 $self->add_handler_for_event(
51 0 ▶ backend_ready => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 my $self = shift;
53 0 $self->{backend_ready} = 1;
54 0 0 # one-shot
55 }
56 0 );
57 # my %args = %{+shift};
58 0 $self->invoke_event(backend_ready =>);
59 0 return $self;
60 }
61  
62 =head2 apply_entity
63  
64 Set up this entity in storage, by adding it to the list of keys and clearing the existing max_id.
65  
66 =cut
67  
68 0 ▶sub apply_entity {
Subroutine apply_entity: not called CC=3 SCAR=24.8
Pod documented
69 0 my $self = shift;
70 0 my $entity = shift;
71 0 my %args = @_;
72 0 ▶ die "Entity exists already: " . $entity->name if exists $EntityMap{$entity->name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $EntityMap{$entity->name} 0 0 0
73  
74 0 $self->entity->push($entity);
75 0 $EntityMap{$entity->name} = {
76 entity => $entity,
77 max_id => 0,
78 };
79 0 ▶ $args{on_complete}->() if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
80 0 return $self;
81 }
82  
83 =head2 read_primary
84  
85 Get the primary keys for a table.
86  
87 =cut
88  
89 0 ▶sub read_primary {
Subroutine read_primary: not called CC=1 SCAR=6.9
Pod documented
90 0 my $self = shift;
91 0 my $entity = shift;
92 0 return $EntityMap{$entity->name}->{entity}->primary->list;
93 }
94  
95 =head2 read_fields
96  
97 Read all fields for a given entity.
98  
99 =cut
100  
101 0 ▶sub read_fields {
Subroutine read_fields: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self = shift;
103 0 my $entity = shift;
104 0 return $EntityMap{$entity->name}->{entity}->field->list;
105 }
106  
107 =head2 table_list
108  
109 Get a list of all the existing tables in the schema.
110  
111 =cut
112  
113 0 ▶sub table_list {
Subroutine table_list: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 return map { $_->{entity} } values %EntityMap;
116 }
117  
118 =head2 field_list
119  
120 Returns a list of all fields for the given table.
121  
122 =cut
123  
124 0 ▶sub field_list {
Subroutine field_list: not called CC=1 SCAR=6.9
Pod documented
125 0 my $self = shift;
126 0 my $entity = shift;
127 0 return $EntityMap{$entity->name}->{entity}->field->list;
128 }
129  
130 =head2 read
131  
132 Reads the data for the given entity and returns hashref with the appropriate data.
133  
134 Parameters:
135  
136 =over 4
137  
138 =item * entity - L<EntityModel::Entity>
139  
140 =item * id - ID to read data from
141  
142 =back
143  
144 Callbacks (included in parameter list above):
145  
146 =over 4
147  
148 =item * on_complete - called when the value has been read, includes the value
149  
150 =back
151  
152 =cut
153  
154 0 ▶sub read {
Subroutine read: not called CC=3 SCAR=24.8
Pod documented
155 0 my $self = shift;
156 0 my %args = @_;
157 0 ▶ die "Entity not found" unless exists $EntityMap{$args{entity}->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$args{"entity"}->name} 0 0 0
158 0 my $v = $EntityMap{$args{entity}->name}->{store}->{$args{id}};
159 0 ▶ $args{on_complete}->($v) if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
160 0 return $v;
161 }
162  
163 =head2 _next_id
164  
165 Returns the next ID for the given entity. Not intended to be called outside this package;
166 returns the value immediately rather than asynchronously.
167  
168 =cut
169  
170 0 ▶sub _next_id {
Subroutine _next_id: not called CC=3 SCAR=24.8
171 0 my $self = shift;
172 0 my %args = @_;
173 0 ▶ die "Entity " . $args{entity}->name . " not found, we have: " . join(',', sort keys %EntityMap) unless exists $EntityMap{$args{entity}->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$args{"entity"}->name} 0 0 0
174 0 ▶ $EntityMaxID{$args{entity}->name} ||= 0;
Condition0%0/2
$EntityMaxID{$args{"entity"}->name} ||= 0
l!l
00
MC/DC0%0/1
$EntityMaxID{$args{"entity"}->name} ||= 0
$EntityMaxID{$args{"entity"}->name}
Truth table0%0/2
A
A= $EntityMaxID{$args{"entity"}->name}
Aresult
11
00
175 0 return ++$EntityMaxID{$args{entity}->name};
176 }
177  
178 =head2 create
179  
180 Creates new entry for the given L<EntityModel::Entity>.
181  
182 Parameters:
183  
184 =over 4
185  
186 =item * entity - L<EntityModel::Entity>
187  
188 =item * data - actual data values
189  
190 =back
191  
192 Callbacks (included in parameter list above):
193  
194 =over 4
195  
196 =item * on_complete - called when the value has been created, will be passed the assigned ID
197  
198 =back
199  
200 =cut
201  
202 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
203 0 my $self = shift;
204 0 my %args = @_;
205 0 my $id = $self->_next_id(%args);
206 0 $args{id} = $id;
207 $args{data} = {
208 0 %{$args{data}},
209 0 $args{entity}->primary => $id
210 };
211 0 $self->store(%args);
212 0 ▶ $args{on_complete}->($id) if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
213 0 return $id;
214 }
215  
216 =head2 store
217  
218 Stores data to the given entity and ID.
219  
220 Parameters:
221  
222 =over 4
223  
224 =item * entity - L<EntityModel::Entity>
225  
226 =item * id - ID to store data to
227  
228 =item * data - actual data values
229  
230 =back
231  
232 Callbacks (included in parameter list above):
233  
234 =over 4
235  
236 =item * on_complete - called when the value has been stored, will be passed the assigned ID
237  
238 =back
239  
240 =cut
241  
242 0 ▶sub store {
Subroutine store: not called CC=4 SCAR=30.0
Pod documented
243 0 my $self = shift;
244 0 my %args = @_;
245 0 ▶ die "Entity not found" unless exists $EntityMap{$args{entity}->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$args{"entity"}->name} 0 0 0
246 0 ▶ die "No ID given" unless defined $args{id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"id"} 0 0 0
247  
248 0 $EntityMap{$args{entity}->name}->{store}->{$args{id}} = $args{data};
249 0 ▶ $args{on_complete}->($args{id}) if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
250 0 return $self;
251 }
252  
253 =head2 remove
254  
255 Removes given ID from storage.
256  
257 Parameters:
258  
259 =over 4
260  
261 =item * entity - L<EntityModel::Entity>
262  
263 =item * id - ID to store data to
264  
265 =back
266  
267 Callbacks (included in parameter list above):
268  
269 =over 4
270  
271 =item * on_complete - called when the value has been removed
272  
273 =back
274  
275 =cut
276  
277 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
278 0 my $self = shift;
279 0 my %args = @_;
280 0 ▶ die "Entity not found" unless exists $EntityMap{$args{entity}->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$args{"entity"}->name} 0 0 0
281 0 delete $EntityMap{$args{entity}->name}->{store}->{$args{id}};
282 0 ▶ $args{on_complete}->($args{id}) if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
283 0 return $self;
284 }
285  
286 =head2 find
287  
288 Callbacks (included in parameter list above):
289  
290 =over 4
291  
292 =item * on_item - called for each item
293  
294 =item * on_not_found - called once if no items were found
295  
296 =item * on_complete - called when no more items are forthcoming (regardless of whether any
297 were found or not)
298  
299 =back
300  
301 =cut
302  
303 0 ▶sub find {
Subroutine find: not called CC=7 SCAR=40.3
Pod documented
304 0 my $class = shift;
305 0 my %args = @_;
306  
307 0 my @rslt;
308 0 my $seen = 0;
309 ENTRY:
310 0 foreach my $entry (sort values %{$EntityMap{$args{entity}->name}->{store}}) {
311 0 ▶ next ENTRY unless all { $entry->{$_} ~~ $args{data}->{$_} } keys %{$args{data}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $entry->{$_} ~~ $args{"data"}{$_}; } , keys %{$args{"data"};}) 0 0 0
312 0 ++$seen;
313 0 ▶ $args{on_item}->($entry) if exists $args{on_item};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_item"} 0 0 0
314 0 push @rslt, $entry;
315 }
316 0 ▶ $args{on_not_found}->() if !$seen && exists $args{on_not_found};
Branch: 1 branch
BranchTrueFalseTotal
if not $seen and exists $args{"on_not_found"} 0 0 0
Condition0%0/3
not $seen and exists $args{"on_not_found"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $seen and exists $args{"on_not_found"}
not $seen exists $args{"on_not_found"}
Truth table0%0/3
not(A) and B
A= not $seenB= exists $args{"on_not_found"}
ABresult
0X0
100
111
317 0 ▶ $args{on_complete}->() if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
318 0 return @rslt;
319 }
320  
321 =head2 adjacent
322  
323 Returns the adjacent values for the given ID.
324  
325 Callbacks (included in parameter list above):
326  
327 =over 4
328  
329 =item * on_prev - called with the value of the previous item
330  
331 =item * on_next - called with the value of the next item
332  
333 =item * on_complete - called when both next and previous values have been identified
334  
335 =back
336  
337 =cut
338  
339 0 ▶sub adjacent {
Subroutine adjacent: not called CC=5 SCAR=34.0
Pod documented
340 0 my $self = shift;
341 0 my %args = @_;
342 0 ▶ die "Entity not found" unless exists $EntityMap{$args{entity}->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$args{"entity"}->name} 0 0 0
343  
344 0 my $entity = $args{entity};
345 0 my $id = $args{id};
346  
347 # Inefficient? Sure. You shouldn't be using this module in production code anyway.
348 my ($prev) = reverse grep {
349 0 $_ < $id
350 0 } sort keys %{$EntityMap{$entity->name}->{store}};
351 my ($next) = grep {
352 0 $_ > $id
353 0 } sort keys %{$EntityMap{$entity->name}->{store}};
354  
355 0 ▶ $args{on_prev}->($prev) if exists $args{on_prev};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_prev"} 0 0 0
356 0 ▶ $args{on_next}->($next) if exists $args{on_next};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_next"} 0 0 0
357 0 ▶ $args{on_complete}->($prev, $next) if exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_complete"} 0 0 0
358 0 return ($prev, $next);
359 }
360  
361 =head2 outer
362  
363 Returns the first and last values for the given ID.
364  
365 Callbacks (included in parameter list above):
366  
367 =over 4
368  
369 =item * on_first - called with the value of the previous item
370  
371 =item * on_last - called with the value of the next item
372  
373 =item * on_complete - called when both next and previous values have been identified
374  
375 =back
376  
377 =cut
378  
379 0 ▶sub outer {
Subroutine outer: not called CC=4 SCAR=30.0
Pod documented
380 0 my $self = shift;
381 0 my %args = @_;
382 0 my $entity = $args{entity};
383 0 ▶ die "Entity not found" unless exists $EntityMap{$entity->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $EntityMap{$entity->name} 0 0 0
384  
385 0 my ($first, $last) = (sort keys %{$EntityMap{$entity->name}->{store}})[0,-1];
386 0 ▶ $args{on_first}->($first) if exists $args{on_first};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_first"} 0 0 0
387 0 ▶ $args{on_last}->($last) if exists $args{on_last};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_last"} 0 0 0
388 0 return ($first, $last);
389 }
390  
391 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=0.6
Pod documented
392 0 my $self = shift;
393 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
394 0 warn Dumper \%EntityMap;
395 }
396  
397 2 1;
398  
399 __END__
« EntityModel/Support/Perl.pm ↑ Summary EntityModel/Storage.pm »