lib/Magpie/Resource/Kioku.pm

filter: Statementstmt 48.7% 94 / 193 Branchbran 32.0% 16 / 50 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 44.7% 17 / 38 Podpod 0.0% 0 / 3 totaltotal 44.1% 129 / 292 CC 32 SCAR 28.6
CC
32
Cov
45%
PUT
90.0
POST
19.0
DELETE
12.0
CRAP 17.4
« blib/lib/Magpie/Plugin/DBI.pm ↑ Summary blib/lib/Magpie/Matcher.pm »
1 package Magpie::Resource::Kioku;
2 2 $Magpie::Resource::Kioku::VERSION = '1.141660';
3 # ABSTRACT: INCOMPLETE - Resource implementation for KiokuDB datastores.
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 extends 'Magpie::Resource';
7 2 ▶use Magpie::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use KiokuDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 has data_source => (
13 is => 'ro',
14 isa => 'KiokuDB',
15 lazy_build => 1,
16 );
17  
18 2 has wrapper_class => (
19 isa => "Str",
20 is => "ro",
21 required => 1,
22 default => 'MagpieGenericWrapper',
23 );
24  
25 2 has dsn => (
26 isa => "Str",
27 is => "ro",
28 predicate => "has_dsn",
29 );
30  
31 2 has extra_args => (
32 isa => "HashRef|ArrayRef",
33 is => "ro",
34 predicate => "has_extra_args",
35 );
36  
37 2 has typemap => (
38 isa => "KiokuDB::TypeMap",
39 is => "ro",
40 predicate => "has_typemap",
41 );
42  
43 2 has _kioku_scope => (
44 is => 'rw',
45 isa => 'KiokuDB::LiveObjects::Scope',
46 );
47  
48 2 has username => (
49 is => 'ro',
50 isa => 'Maybe[Str]',
51 predicate => 'has_username',
52 );
53  
54 2 has password => (
55 is => 'ro',
56 isa => 'Maybe[Str]',
57 predicate => 'has_password',
58 );
59  
60 sub _connect_args {
61 1 ▶ my $self = shift;
Subroutine _connect_args: called CC=7 SCAR=22.8
62 1 ▶ my @args = ( $self->dsn || die "dsn is required" );
Condition50%1/2
$self->dsn || die('dsn is required')
l!l
10
MC/DC0%0/1
$self->dsn || die('dsn is required')
$self->dsn
Truth table50%1/2
A
A= $self->dsn
Aresult
11
00
63  
64 1 ▶ if ( $self->has_username ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_username) 0 1 1
65 0 push @args, user => $self->username;
66 }
67  
68 1 ▶ if ( $self->has_password ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_password) 0 1 1
69 0 push @args, password => $self->password;
70 }
71  
72 1 ▶ if ( $self->has_typemap ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_typemap) 0 1 1
73 0 push @args, typemap => $self->typemap;
74 }
75  
76 1 ▶ if ( $self->has_extra_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_extra_args) 1 0 1
77 1 my $extra = $self->extra_args;
78  
79 1 ▶ if ( ref($extra) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extra eq 'ARRAY') { } 0 1 1
80 0 push @args, @$extra;
81 }
82 else {
83 1 push @args, %$extra;
84 }
85 }
86  
87 1 \@args;
88 }
89  
90 sub _build_data_source {
91 3 ▶ my $self = shift;
Subroutine _build_data_source: called CC=2 SCAR=7.3
92 3 my $k = undef;
93  
94 try {
95 3 ▶ $k = $self->resolve_asset( service => 'kioku_dir' );
Subroutine __ANON__: called CC=1 SCAR=0.0
96 }
97 catch {
98 try {
99 1 ▶ $k = KiokuDB->connect( @{ $self->_connect_args } );
Subroutine __ANON__: called CC=1 SCAR=0.0
100 }
101 catch {
102 0 ▶ my $error = "Could not connect to Kioku data source: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 warn $error;
104 0 $self->set_error( { status_code => 500, reason => $error } );
105 1 ▶ };
Subroutine __ANON__: called CC=1 SCAR=0.0
106 3 };
107  
108 3 ▶ return undef if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 3 3
109 3 $self->_kioku_scope( $k->new_scope );
110 3 return $k;
111 }
112  
113 sub GET {
114 2 ▶ my $self = shift;
Subroutine GET: called CC=5 SCAR=18.6
Pod undocumented
115 2 my $ctxt = shift;
116 2 $self->parent_handler->resource($self);
117 2 my $req = $self->request;
118  
119 2 my $path = $req->path_info;
120 2 my $id = $self->get_entity_id($ctxt);
121  
122 2 ▶ if ($path =~ /\/$/ && !$id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/$] and not $id) 0 2 2
Condition33%1/3
$path =~ m[/$] and not $id
!ll&&!rl&&r
200
MC/DC0%0/2
$path =~ m[/$] and not $id
$path =~ m[/$] not $id
Truth table33%1/3
A and not(B)
A= $path =~ m[/$]B= not $id
ABresult
0X0
100
111
123 0 $self->state('prompt');
124 0 return OK;
125 }
126  
127 2 my $data = undef;
128  
129 try {
130 2 ▶ ($data) = $self->data_source->lookup($id);
Subroutine __ANON__: called CC=1 SCAR=0.0
131 }
132 catch {
133 0 ▶ my $trace = $_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
134 0 ▶ if (ref($trace)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trace) 0 0 0
135 0 $trace = $trace->as_string;
136 }
137 0 my $error = "Could not GET data from Kioku data source: $trace\n";
138 0 $self->set_error( { status_code => 500, reason => $error } );
139 2 };
140  
141 2 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 2 2
142  
143 2 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 1 2
144 1 $self->set_error({ status_code => 404, reason => 'Resource not found.'});
145 1 return OK;
146 }
147  
148 1 $self->data($data);
149 1 return OK;
150 }
151  
152 sub POST {
153 1 ▶ my $self = shift;
Subroutine POST: called CC=10 SCAR=29.4
Pod undocumented
154 1 $self->parent_handler->resource($self);
155 1 my $req = $self->request;
156  
157 1 my $to_store = undef;
158  
159 1 my $wrapper_class = $self->wrapper_class;
160  
161 # XXX should check for a content body first.
162 1 my %args = ();
163  
164 1 ▶ if ( $self->has_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_data) { } 0 1 1
165 0 %args = %{ $self->data };
166 0 $self->clear_data;
167 }
168 else {
169 1 for ( $req->param ) {
170 2 $args{$_} = $req->param($_);
171 }
172 }
173  
174 # permit POST to update if there's an entity ID.
175 # XXX: Should this go in an optional Role?
176 1 ▶ if (my $existing_id = $self->get_entity_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing_id = $self->get_entity_id) 1 0 1
177 1 my $existing = undef;
178 try {
179 1 ▶ ($existing) = $self->data_source->lookup($existing_id);
Subroutine __ANON__: called CC=1 SCAR=0.0
180 }
181 catch {
182 0 ▶ my $error = "Could not fetch data from Kioku data source for POST editing if entity with ID $existing_id: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 $self->set_error( { status_code => 500, reason => $error } );
184 1 };
185  
186 1 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
187  
188 1 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 0 1 1
189 0 foreach my $key (keys(%args)) {
190 0 $existing->$key( $args{$key} );
191 }
192  
193 try {
194 $self->data_source->txn_do( sub {
195 0 ▶ $self->data_source->store($existing);
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 ▶ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 }
198 catch {
199 0 ▶ my $error = "Error updating data entity with ID $existing_id: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
200 0 $self->set_error( { status_code => 500, reason => $error } );
201 0 };
202  
203 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
204  
205 # finally, if it all went OK, say so.
206 0 $self->state('updated');
207 0 $self->response->status(204);
208 0 return OK;
209 }
210 }
211  
212 # if we make it here there is no existing record, so make a new one.
213 try {
214 1 ▶ Class::Load::load_class($wrapper_class);
Subroutine __ANON__: called CC=1 SCAR=0.0
215 1 $to_store = $wrapper_class->new(%args);
216 }
217 catch {
218 0 ▶ my $error
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 = "Could not create instance of wrapper class '$wrapper_class': $_\n";
220 0 warn $error;
221 0 $self->set_error( { status_code => 500, reason => $error } );
222 1 };
223  
224 1 ▶ return DECLINED if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
225  
226 1 my $id = undef;
227  
228 try {
229 $self->data_source->txn_do( sub {
230 1 ▶ $id = $self->data_source->store($to_store);
Subroutine __ANON__: called CC=1 SCAR=0.0
231 1 ▶ });
Subroutine __ANON__: called CC=1 SCAR=0.0
232 }
233 catch {
234 0 ▶ my $error = "Could not store POST data in Kioku data source: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
235 0 warn $error;
236 0 $self->set_error( { status_code => 500, reason => $error } );
237 1 };
238  
239 1 ▶ return DECLINED if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
240  
241 # XXX: all of this needs to go in an abstract object downstream serializer
242 # can figure stuff out
243 1 my $path = $req->path_info;
244 1 $path =~ s|^/||;
245 1 $path =~ s|/$||;
246 1 $self->state('created');
247 1 $self->response->status(201);
248 1 $self->response->header( 'Location' => $req->base . $path . "/$id" );
249 1 return OK;
250 }
251  
252 sub DELETE {
253 0 ▶ my $self = shift;
Subroutine DELETE: not called CC=3 SCAR=24.8
254 0 my $ctxt = shift;
255 0 $self->parent_handler->resource( $self );
256 0 my $req = $self->request;
257  
258 0 my $path = $req->path_info;
259  
260 0 ▶ if ( $path =~ /\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/$]) 0 0 0
261 0 $self->state('prompt');
262 0 return OK;
263 }
264  
265 0 my $id = $self->get_entity_id($ctxt);
266  
267 # should we do a separate lookup to make sure the data is there?
268 try {
269 $self->data_source->txn_do( sub {
270 0 ▶ $self->data_source->delete( $id );
Subroutine __ANON__: not called CC=1 SCAR=6.9
271 0 ▶ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
272 }
273 catch {
274 0 ▶ my $error = "Could not delete data from Kioku data source: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 0 $self->set_error({ status_code => 500, reason => $error });
276 0 };
277  
278 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
279 0 $self->state('deleted');
280 0 $self->response->status(204);
281 0 return OK;
282 }
283  
284  
285 sub PUT {
286 0 ▶ my $self = shift;
Subroutine PUT: not called CC=9 SCAR=45.0
Pod undocumented
287 0 $self->parent_handler->resource($self);
288 0 my $req = $self->request;
289  
290 0 my $to_store = undef;
291  
292 0 my $wrapper_class = $self->wrapper_class;
293  
294 # XXX should check for a content body first.
295 0 my %args = ();
296  
297 0 ▶ if ( $self->has_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_data) { } 0 0 0
298 0 %args = %{ $self->data };
299 0 $self->clear_data;
300 }
301 else {
302 0 for ( $req->param ) {
303 0 $args{$_} = $req->param($_);
304 }
305 }
306  
307 0 my $existing_id = $self->get_entity_id;
308  
309 0 ▶ unless ($existing_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing_id) 0 0 0
310 0 $self->set_error({
311 status_code => 400,
312 reason => "Attempt to PUT without a definable entity ID."
313 });
314 0 return DONE;
315 }
316  
317  
318 0 my $existing = undef;
319 try {
320 0 ▶ ($existing) = $self->data_source->lookup($existing_id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 }
322 catch {
323 0 ▶ my $error = "Could not fetch data from Kioku data source for PUT editing if entity with ID $existing_id: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 $self->set_error( { status_code => 500, reason => $error } );
325 0 };
326  
327 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
328  
329 0 ▶ unless ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing) 0 0 0
330 0 $self->set_error(404);
331 0 return DONE;
332 }
333  
334 0 foreach my $key (keys(%args)) {
335 try {
336 0 ▶ $existing->$key( $args{$key} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
337 }
338 catch {
339 0 ▶ my $error = "Error updating property '$key' of Resource ID $existing_id: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 $self->set_error( { status_code => 500, reason => $error } );
341 0 last;
342 0 };
343 }
344  
345  
346 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
347  
348 try {
349 $self->data_source->txn_do(sub {
350 0 ▶ my $scope = $self->data_source->new_scope;
Subroutine __ANON__: not called CC=1 SCAR=6.9
351 0 $self->data_source->store($existing);
352 0 ▶ });
Subroutine __ANON__: not called CC=1 SCAR=6.9
353 }
354 catch {
355 0 ▶ my $error = "Error updating data entity with ID $existing_id: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
356 0 $self->set_error( { status_code => 500, reason => $error } );
357 0 };
358  
359 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
360  
361 # finally, if it all went OK, say so.
362 0 $self->state('updated');
363 0 $self->response->status(204);
364 0 return OK;
365 }
366  
367  
368 package MagpieGenericWrapper;
369 2 $MagpieGenericWrapper::VERSION = '1.141660';
370 sub new {
371 0 ▶ my $proto = shift;
Subroutine new: not called CC=1 SCAR=6.9
372 0 my %args = @_;
373 0 return bless \%args, $proto;
374 }
375  
376 2 1;
377  
378  
379  
380 =pod
381  
382 =head1 NAME
383  
384 Magpie::Resource::Kioku - INCOMPLETE - Resource implementation for KiokuDB datastores.
385  
386 =head1 VERSION
387  
388 version 1.141660
389  
390 # SEEALSO: Magpie, Magpie::Resource
391  
392 =head1 AUTHORS
393  
394 =over 4
395  
396 =item *
397  
398 Kip Hampton <kip.hampton@tamarou.com>
399  
400 =item *
401  
402 Chris Prather <chris.prather@tamarou.com>
403  
404 =back
405  
406 =head1 COPYRIGHT AND LICENSE
407  
408 This software is copyright (c) 2011 by Tamarou, LLC.
409  
410 This is free software; you can redistribute it and/or modify it under
411 the same terms as the Perl 5 programming language system itself.
412  
413 =cut
414  
415  
416 __END__
417  
« blib/lib/Magpie/Plugin/DBI.pm ↑ Summary blib/lib/Magpie/Matcher.pm »