blib/lib/Magpie/Resource/DBIC.pm untested

filter: Statementstmt 0.0% 0 / 151 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 211 CC 19 SCAR 35.4
CC
19
Cov
0%
POST
72.0
_connect_args
42.0
GET
20.0
CRAP 34.5
1 package Magpie::Resource::DBIC;
2 $Magpie::Resource::DBIC::VERSION = '1.141660';
3 # ABSTRACT: Resource implementation for DBIx::Class ResultSources.
4  
5 use Moose;
6 extends 'Magpie::Resource';
7 with 'Magpie::Plugin::DBI';
8 use Class::Load;
9 use Magpie::Constants;
10 use Try::Tiny;
11  
12 has data_source => (
13 is => 'ro',
14 isa => 'KiokuDB',
15 lazy_build => 1,
16 );
17  
18 has wrapper_class => (
19 isa => "Str",
20 is => "ro",
21 required => 1,
22 default => 'MagpieGenericWrapper',
23 );
24  
25 has dsn => (
26 isa => "Str",
27 is => "ro",
28 predicate => "has_dsn",
29 );
30  
31 has extra_args => (
32 isa => "HashRef|ArrayRef",
33 is => "ro",
34 predicate => "has_extra_args",
35 );
36  
37 has typemap => (
38 isa => "KiokuDB::TypeMap",
39 is => "ro",
40 predicate => "has_typemap",
41 );
42  
43 has _kioku_scope => (
44 is => 'rw',
45 isa => 'KiokuDB::LiveObjects::Scope',
46 );
47  
48 has username => (
49 is => 'ro',
50 isa => 'Maybe[Str]',
51 predicate => 'has_username',
52 );
53  
54 has password => (
55 is => 'ro',
56 isa => 'Maybe[Str]',
57 predicate => 'has_password',
58 );
59  
60 sub _connect_args {
61 my $self = shift;
62 my @args = ( $self->dsn || die "dsn is required" );
63  
64 if ( $self->has_username ) {
65 push @args, user => $self->username;
66 }
67  
68 if ( $self->has_password ) {
69 push @args, password => $self->password;
70 }
71  
72 if ( $self->has_typemap ) {
73 push @args, typemap => $self->typemap;
74 }
75  
76 if ( $self->has_extra_args ) {
77 my $extra = $self->extra_args;
78  
79 if ( ref($extra) eq 'ARRAY' ) {
80 push @args, @$extra;
81 }
82 else {
83 push @args, %$extra;
84 }
85 }
86  
87 \@args;
88 }
89  
90 sub _build_data_source {
91 my $self = shift;
92 my $k = undef;
93  
94 try {
95 $k = $self->resolve_asset( service => 'kioku_dir' );
96 }
97 catch {
98 try {
99 $k = KiokuDB->connect( @{ $self->_connect_args } );
100 }
101 catch {
102 my $error = "Could not connect to Kioku data source: $_\n";
103 warn $error;
104 $self->set_error( { status_code => 500, reason => $error } );
105 };
106 };
107  
108 return undef if $self->has_error;
109 $self->_kioku_scope( $k->new_scope );
110 return $k;
111 }
112  
113 sub GET {
114 my $self = shift;
115 $self->parent_handler->resource($self);
116 my $req = $self->request;
117  
118 my $path = $req->path_info;
119 my $id = $self->get_entity_id;
120  
121 if ($path =~ /\/$/ && !$id ) {
122 $self->state('prompt');
123 return OK;
124 }
125  
126 my $data = undef;
127  
128 try {
129 ($data) = $self->data_source->lookup($id);
130 }
131 catch {
132 my $error = "Could not GET data from Kioku data source: $_\n";
133 $self->set_error( { status_code => 500, reason => $error } );
134 };
135  
136 return OK if $self->has_error;
137  
138 unless ($data) {
139 $self->set_error({ status_code => 404, reason => 'Resource not found.'});
140 return OK;
141 }
142  
143 #warn "got data " . Dumper($data);
144  
145 $self->data($data);
146 return OK;
147 }
148  
149 sub POST {
150 my $self = shift;
151 $self->parent_handler->resource($self);
152 my $req = $self->request;
153  
154 my $to_store = undef;
155  
156 my $wrapper_class = $self->wrapper_class;
157  
158 # XXX should check for a content body first.
159 my %args = ();
160  
161 if ( $self->has_data ) {
162 %args = %{ $self->data };
163 $self->clear_data;
164 }
165 else {
166 for ( $req->param ) {
167 $args{$_} = $req->param($_);
168 }
169 }
170  
171 # permit POST to update if there's an entity ID.
172 # XXX: Should this go in an optional Role?
173 if (my $existing_id = $self->get_entity_id) {
174 my $existing = undef;
175 try {
176 ($existing) = $self->data_source->lookup($existing_id);
177 }
178 catch {
179 my $error = "Could not fetch data from Kioku data source for POST editing if entity with ID $existing_id: $_\n";
180 $self->set_error( { status_code => 500, reason => $error } );
181 };
182  
183 return OK if $self->has_error;
184  
185 if ($existing) {
186 foreach my $key (keys(%args)) {
187 $existing->$key( $args{$key} );
188 }
189  
190 try {
191 $self->data_source->store($existing);
192 }
193 catch {
194 my $error = "Error updating data entity with ID $existing_id: $_\n";
195 $self->set_error( { status_code => 500, reason => $error } );
196 };
197  
198 return OK if $self->has_error;
199  
200 # finally, if it all went OK, say so.
201 $self->state('updated');
202 $self->response->status(204);
203 return OK;
204 }
205 }
206  
207 # if we make it here there is no existing record, so make a new one.
208 try {
209 Class::Load::load_class($wrapper_class);
210 $to_store = $wrapper_class->new(%args);
211 }
212 catch {
213 my $error
214 = "Could not create instance of wrapper class '$wrapper_class': $_\n";
215 warn $error;
216 $self->set_error( { status_code => 500, reason => $error } );
217 };
218  
219 return DECLINED if $self->has_error;
220  
221 my $id = undef;
222  
223 try {
224 $id = $self->data_source->store($to_store);
225 }
226 catch {
227 my $error = "Could not store POST data in Kioku data source: $_\n";
228 warn $error;
229 $self->set_error( { status_code => 500, reason => $error } );
230 };
231  
232 return DECLINED if $self->has_error;
233  
234 # XXX: all of this needs to go in an abstract object downstream serializer
235 # can figure stuff out
236 my $path = $req->path_info;
237 $path =~ s|^/||;
238 $path =~ s|/$||;
239 $self->state('created');
240 $self->response->status(201);
241 $self->response->header( 'Location' => $req->base . $path . "/$id" );
242 return OK;
243 }
244  
245 sub DELETE {
246 my $self = shift;
247 $self->parent_handler->resource( $self );
248 my $req = $self->request;
249  
250 my $path = $req->path_info;
251  
252 if ( $path =~ /\/$/ ) {
253 $self->state('prompt');
254 return OK;
255 }
256  
257 my @steps = split '/', $path;
258  
259 my $id = $req->param('id') || pop @steps;
260  
261 # should we do a separate lookup to make sure the data is there?
262 try {
263 $self->data_source->delete( $id );
264 }
265 catch {
266 my $error = "Could not delete data from Kioku data source: $_\n";
267 $self->set_error({ status_code => 500, reason => $error });
268 };
269  
270 return OK if $self->has_error;
271 $self->state('deleted');
272 $self->response->status(204);
273 return OK;
274 }
275  
276  
277 package MagpieGenericWrapper;
278 $MagpieGenericWrapper::VERSION = '1.141660';
279 sub new {
280 my $proto = shift;
281 my %args = @_;
282 return bless \%args, $proto;
283 }
284  
285 1;
286  
287  
288  
289 =pod
290  
291 =head1 NAME
292  
293 Magpie::Resource::DBIC - Resource implementation for DBIx::Class ResultSources.
294  
295 =head1 VERSION
296  
297 version 1.141660
298  
299 # SEEALSO: Magpie, Magpie::Resource
300  
301 =head1 AUTHORS
302  
303 =over 4
304  
305 =item *
306  
307 Kip Hampton <kip.hampton@tamarou.com>
308  
309 =item *
310  
311 Chris Prather <chris.prather@tamarou.com>
312  
313 =back
314  
315 =head1 COPYRIGHT AND LICENSE
316  
317 This software is copyright (c) 2011 by Tamarou, LLC.
318  
319 This is free software; you can redistribute it and/or modify it under
320 the same terms as the Perl 5 programming language system itself.
321  
322 =cut
323  
324  
325 __END__
326