EntityModel/Storage/PerlAsync.pm

filter: Statementstmt 18.4% 12 / 65 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 18.7% 3 / 16 Podpod 100% 6 / 6 totaltotal 21.0% 21 / 100 CC 8 SCAR 14.6
CC
8
Cov
16%
setup
6.0
read
6.0
__ANON__
6.0
CRAP 4.3
« EntityModel/Query/Join.pm ↑ Summary EntityModel/Query/Insert.pm »
1 package EntityModel::Storage::PerlAsync;
2 {
3 1 $EntityModel::Storage::PerlAsync::VERSION = '0.102';
4 }
5 1 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 _isa => [qw{EntityModel::Storage::Perl}],
7 loop => { type => 'IO::Async::Loop' }
8 1 };
9  
10 =head1 NAME
11  
12 EntityModel::Storage::PerlAsync - 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 Wrapper around the Perl storage module to defer responses until idle point in an L<IO::Async> loop.
25  
26 =cut
27  
28 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 =head1 METHODS
31  
32 =cut
33  
34 =head2 new
35  
36 Subclassed instantiation, requires an L<IO::Async::Loop> passed as the C<loop> named parameter.
37  
38 =cut
39  
40 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod documented
41 0 my $self = shift;
42 0 my %args = %{+shift};
43 0 ▶ my $loop = delete $args{loop} or die "No IO::Async::Loop provided?";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = delete $args{"loop"} or die "No IO::Async::Loop provided?" 0 0 0
44 0 $self->SUPER::setup(\%args);
45 0 Scalar::Util::weaken($self->{loop} = $loop);
46 0 return $self;
47 }
48  
49 =head2 read
50  
51 Reads the data for the given entity and returns hashref with the appropriate data.
52  
53 Parameters:
54  
55 =over 4
56  
57 =item * entity - L<EntityModel::Entity>
58  
59 =item * id - ID to read data from
60  
61 =back
62  
63 Callbacks (included in parameter list above):
64  
65 =over 4
66  
67 =item * on_complete - called when the value has been read, includes the value
68  
69 =item * on_not_found - called if entry not found
70  
71 =back
72  
73 Returns $self.
74  
75 =cut
76  
77 1 ▶use Carp qw/cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 0 ▶sub read {
Subroutine read: not called CC=2 SCAR=17.9
Pod documented
79 0 my $self = shift;
80 0 my %args = @_;
81 0 ▶ return unless exists $args{on_complete};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"on_complete"} 0 0 0
82 # cluck "Request for id " . ($args{id} // 'undef') . " from " . $args{entity}->name;
83  
84 0 my $complete = delete $args{on_complete};
85 0 ▶ $self->loop->later($self->sap(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my $self = shift;
87 0 my $v = $self->SUPER::read(%args);
88 0 ▶ warn "Reading " . ($v // "undef") . "\n";
Condition0%0/2
$v // "undef"
l!l
00
MC/DC0%0/1
$v // "undef"
$v
Truth table0%0/2
A
A= $v
Aresult
11
00
89 0 $complete->($v);
90 0 }));
91 0 return $self;
92 }
93  
94 =head2 create
95  
96 Creates new entry for the given L<EntityModel::Entity>.
97  
98 Parameters:
99  
100 =over 4
101  
102 =item * entity - L<EntityModel::Entity>
103  
104 =item * data - actual data values
105  
106 =back
107  
108 Callbacks (included in parameter list above):
109  
110 =over 4
111  
112 =item * on_complete - called when the value has been created, will be passed the assigned ID
113  
114 =back
115  
116 Returns $self.
117  
118 =cut
119  
120 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
121 0 my $self = shift;
122 0 my %args = @_;
123 0 my $complete = delete $args{on_complete};
124 0 ▶ $self->loop->later($self->sap(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
125 0 my $self = shift;
126 0 my $v = $self->SUPER::create(%args);
127 # warn "Creating $v\n";
128 0 ▶ $complete->($v) if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 0 0 0
129 0 }));
130 0 return $self;
131 }
132  
133 =head2 store
134  
135 Stores data to the given entity and ID.
136  
137 Parameters:
138  
139 =over 4
140  
141 =item * entity - L<EntityModel::Entity>
142  
143 =item * id - ID to store data to
144  
145 =item * data - actual data values
146  
147 =back
148  
149 Callbacks (included in parameter list above):
150  
151 =over 4
152  
153 =item * on_complete - called when the value has been stored, will be passed the assigned ID
154  
155 =back
156  
157 Returns $self.
158  
159 =cut
160  
161 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 my %args = @_;
164 0 my $complete = delete $args{on_complete};
165 0 ▶ $self->loop->later($self->sap(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
166 0 my $self = shift;
167 0 my $v = $self->SUPER::store(%args);
168 0 ▶ $complete->($args{id}) if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 0 0 0
169 0 }));
170 0 return $self;
171 }
172  
173 =head2 remove
174  
175 Removes given ID from storage.
176  
177 Parameters:
178  
179 =over 4
180  
181 =item * entity - L<EntityModel::Entity>
182  
183 =item * id - ID to store data to
184  
185 =back
186  
187 Callbacks (included in parameter list above):
188  
189 =over 4
190  
191 =item * on_complete - called when the value has been removed
192  
193 =back
194  
195 Returns $self.
196  
197 =cut
198  
199 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
200 0 my $self = shift;
201 0 my %args = @_;
202 0 my $complete = delete $args{on_complete};
203 0 ▶ $self->loop->later($self->sap(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
204 0 my $self = shift;
205 0 my $v = $self->SUPER::remove(%args);
206 0 ▶ $complete->($args{id}) if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 0 0 0
207 0 }));
208 0 return $self;
209 }
210  
211 =head2 find
212  
213 Callbacks (included in parameter list above):
214  
215 =over 4
216  
217 =item * on_item - called for each item
218  
219 =item * on_not_found - called once if no items were found
220  
221 =item * on_complete - called when no more items are forthcoming (regardless of whether any
222 were found or not)
223  
224 =item * on_fail - called if there was an error
225  
226 =back
227  
228 Returns $self.
229  
230 =cut
231  
232 0 ▶sub find {
Subroutine find: not called CC=1 SCAR=6.9
Pod documented
233 0 my $self = shift;
234 0 my %args = @_;
235  
236 0 ▶ $self->loop->later($self->sap(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
237 0 my $self = shift;
238  
239 # Defer all the events
240 0 foreach my $k (grep /^on_/, keys %args) {
241 0 my $orig = $args{$k};
242 0 ▶ $args{$k} = $self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 my ($self, @param) = @_;
244 0 ▶ $self->loop->later(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 $orig->(@param);
246 0 });
247 0 });
248 }
249 0 $self->SUPER::find(%args);
250 0 }));
251 0 return $self;
252 }
253  
254 1 1;
255  
256 __END__
« EntityModel/Query/Join.pm ↑ Summary EntityModel/Query/Insert.pm »