Envoy/Storage/DBIC.pm

filter: Statementstmt 94.0% 143 / 152 Branchbran 70.0% 28 / 40 Conditioncond 61.5% 24 / 39 MC/DCmcdc 37.5% 9 / 24 Subroutinesub 96.0% 24 / 25 Podpod 87.5% 7 / 8 totaltotal 81.6% 235 / 288 CC 38 SCAR 14.2
CC
38
Cov
84%
_data_for_model
9.1
_db_save_relationship
5.9
_value_to_db
5.5
CRAP 4.2
« Envoy.pm ↑ Summary Envoy/Storage/Memory.pm »
1 package Model::Envoy::Storage::DBIC;
2  
3 10 our $VERSION = '0.5.3';
4  
5 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 extends 'Model::Envoy::Storage';
10  
11 =head1 Model::Envoy::Storage::DBIC
12  
13 A Moose Role that adds a DBIx::Class persistence layer to your Moose class
14  
15 =head2 Configuration
16  
17 with 'Model::Envoy' => { storage => {
18 'DBIC' => {
19 schema => sub {
20 ... connect to database here ...
21 }
22 }
23 } };
24  
25 The only configuration option for this plugin is a 'schema' method that returns a
26 C<DBIx::Class:Schema> based object with an open connection to the database. This method
27 will be passed a reference to your class as its only argument.
28  
29 =head3 C<dbic()>
30  
31 This is a method you will need to implement in each C<Model::Envoy> object
32 that use this storage plugin. It should return the name of the DBIx::Class ResultClass
33 that your Model uses for database storage.
34  
35 =head2 Traits
36  
37 This role implements one trait:
38  
39 =head3 DBIC
40  
41 Marking an attribute on your object with the 'DBIC' trait tells this role that it is
42 backed by a DBIx::Class ResultClass column of the same name. It also allows for
43 a few custom options you can apply to that attribute:
44  
45 =over
46  
47 =item primary_key => 1
48  
49 Indicates that this attribute corresponds to the primary key for the database record
50  
51 =item rel => 'rel_type'
52  
53 Indicates that the attribute is a relationship to another model or a list of models. Possible
54 values for this option are
55  
56 =over
57  
58 =item belongs_to
59  
60 =item has_many
61  
62 =item many_to_many
63  
64 =back
65  
66 =item mm_rel => 'bridge_name'
67  
68 For many-to-many relationships it is necessary to indicate what class
69 provides the linkage between the two ends of the relationship ( the linking class
70 maps to the join table in the database).
71  
72 =back
73  
74 =head2 Plugin Methods
75  
76 =head3 build( $dbic_result, [$no_rel] )
77  
78 Takes a DBIx::Class result object and, if it's class matches your class's dbic()
79 method, attempts to build a new instance of your class based on the $dbic_result
80 passed in.
81  
82 The `no_rel` boolean option prevents the creation process from traversing
83 attributes marked as relationships, minimizing the amount of data pulled
84 from the database and the number of new class instances created.
85  
86 Returns the class instance if successful.
87  
88 =head3 save()
89  
90 Performs either an insert or an update for the model, depending on whether
91 there is already a record for it in the database. This method will propogate
92 changes from your DBIx::Class record back to your model, to account for DBIC
93 plugins you may be using that fiddle with column values on insert or update.
94  
95 Returns the calling object for convenient chaining.
96  
97 =head3 delete()
98  
99 Deletes the persistent copy of the current model from the database, if has
100 been stored there.
101  
102 Returns nothing.
103  
104 =head3 in_storage()
105  
106 Uses DBIx::Class's internal mechanisms to determine if this model
107 is tied to a record in the database.
108  
109 Returns a true value if it is, otherwise returns a false value.
110  
111 =cut
112  
113 10 class_has 'schema' => (
114 is => 'rw',
115 isa => 'DBIx::Class::Schema',
116 );
117  
118 # The actual ResultClass for the model object is stored here:
119 has '_dbic_result',
120 is => 'rw',
121 isa => 'Maybe[Object]',
122 lazy => 1,
123 16 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 16 my ( $self ) = @_;
125  
126 16 return $self->schema->resultset( $self->model->dbic )->new({});
127 10 };
128  
129  
130 8 ▶sub configure {
Subroutine configure: called CC=2 SCAR=7.1
Pod documented
131 8 my ( $plugin_class, $envoy_class, $conf ) = @_;
132  
133 $plugin_class->schema(
134 ref $conf->{schema} eq 'CODE' ? $conf->{schema}->($envoy_class) : $conf->{schema}
135 8 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $conf->{'schema'} eq 'CODE' ? : 8 0 8
136  
137 8 $conf->{_configured} = 1;
138 }
139  
140  
141 28 ▶sub build {
Subroutine build: called CC=4 SCAR=15.5
Pod documented
142 28 my ( $class, $model_class, $db_result, $no_rel ) = @_;
143  
144 return undef
145 28 ▶ unless $db_result
Branch: 1 branch
BranchTrueFalseTotal
unless $db_result and builtin::blessed($db_result) and $db_result->isa($model_class->dbic) 0 28 28
Condition33%2/6
$db_result and builtin::blessed($db_result) and $db_result->isa($model_class->dbic)
!ll&&!rl&&r
0028
$db_result and builtin::blessed($db_result)
!ll&&!rl&&r
0028
MC/DC0%0/3
$db_result and builtin::blessed($db_result) and $db_result->isa($model_class->dbic)
$db_result builtin::blessed($db_result) $db_result->isa($model_class->dbic)
Truth table25%1/4
A and B and C
A= $db_resultB= builtin::blessed($db_result)C= $db_result->isa($model_class->dbic)
ABCresult
0XX0
10X0
1100
1111
146 && blessed $db_result
147 && $db_result->isa( $model_class->dbic );
148  
149 28 my $data = $class->_data_for_model( $model_class, $db_result, $no_rel );
150 28 my $model = $model_class->new( %$data );
151  
152 28 $model->get_storage( __PACKAGE__ )->_dbic_result( $db_result );
153  
154 28 return $model;
155 }
156  
157 9 ▶sub fetch {
Subroutine fetch: called CC=4 SCAR=13.9
Pod documented
158 9 my $self = shift;
159 9 my $model_class = shift;
160 9 my %params;
161  
162 9 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 9 9
163  
164 9 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 3 6 9
165  
166 3 my ( $id ) = @_;
167  
168 3 $params{id} = $id;
169 }
170 else {
171  
172 6 my ( $key, $value ) = @_;
173  
174 6 $params{$key} = $value;
175 }
176  
177 9 ▶ if ( my $result = ($self->schema->resultset( $model_class->dbic )
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = ($self->schema->resultset($model_class->dbic)->search(\%params))[0]) 5 3 8
178 ->search(\%params))[0] ) {
179  
180 5 return $model_class->build($result);
181 }
182  
183 3 return undef;
184 }
185  
186 7 ▶sub list {
Subroutine list: called CC=2 SCAR=7.0
Pod documented
187 7 my $self = shift;
188 7 my $model_class = shift;
189  
190 7 ▶ my $conditions = ref $_[0]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 7 7
191 ? $_[0]
192 : { @_ };
193  
194 return [
195 7 map { $model_class->build($_) }
196 $self->schema->resultset( $model_class->dbic )->search( $conditions )
197 ];
198 }
199  
200 9 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
201 9 my ( $self ) = @_;
202  
203 9 my $dbic_result = $self->_dbic_result;
204  
205 9 ▶ $self->schema->txn_do( sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
206  
207 # First update/insert non-relationships
208 9 $self->_populate_dbic_result;
209  
210 9 ▶ if ( $dbic_result->in_storage ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbic_result->in_storage) { } 0 9 9
elsif (my $copy = $dbic_result->get_from_storage) { } 1 8 9
211 0 $dbic_result->update;
212 }
213 # get_from_storage can be noisy due to
214 # https://rt.cpan.org/Public/Bug/Display.html?id=104839
215 elsif ( my $copy = $dbic_result->get_from_storage ) {
216 1 $dbic_result->in_storage(1);
217 1 $dbic_result->update();
218 }
219 else {
220 8 $dbic_result->insert;
221 }
222  
223 # Then, once we're sure the record exists, update relationships
224 9 for my $attr ( @{$self->_dbic_relationships} ) {
225  
226 5 $self->_db_save_relationship( $attr );
227 }
228  
229 9 $dbic_result->update;
230  
231 # Finally, propogate any storage-layer changes back to model
232 9 $self->update_model( $dbic_result );
233  
234 9 });
235  
236 9 return $self;
237 }
238  
239 41 ▶sub _data_for_model {
Subroutine _data_for_model: called CC=9 SCAR=22.1
240 41 my ( $class, $model_class, $db_result, $no_rel ) = @_;
241  
242 41 my $data = {};
243 41 my %relationships = map { $_->name => 1 } @{$class->_dbic_relationships($model_class)};
244  
245 41 foreach my $attr ( grep { defined $db_result->$_ } map { $_->name } @{$class->_dbic_attrs($model_class)} ) {
246  
247 114 ▶ next if $no_rel && exists $relationships{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if $no_rel and exists $relationships{$attr} 13 101 114
Condition100%3/3
$no_rel and exists $relationships{$attr}
!ll&&!rl&&r
772413
MC/DC100%2/2
$no_rel and exists $relationships{$attr}
$no_rel exists $relationships{$attr}
Truth table100%3/3
A and B
A= $no_relB= exists $relationships{$attr}
ABresult
0X0
100
111
248  
249 101 ▶ if ( blessed $db_result->$attr && $db_result->$attr->isa('DBIx::Class::ResultSet') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($db_result->$attr) and $db_result->$attr->isa('DBIx::Class::ResultSet')) { } 17 84 101
Condition100%3/3
builtin::blessed($db_result->$attr) and $db_result->$attr->isa("DBIx::Class::ResultSet")
!ll&&!rl&&r
79517
MC/DC100%2/2
builtin::blessed($db_result->$attr) and $db_result->$attr->isa("DBIx::Class::ResultSet")
builtin::blessed($db_result->$attr) $db_result->$attr->isa("DBIx::Class::ResultSet")
Truth table100%3/3
A and B
A= builtin::blessed($db_result->$attr)B= $db_result->$attr->isa("DBIx::Class::ResultSet")
ABresult
0X0
100
111
250 17 my $attribute = $model_class->meta->find_attribute_by_name($attr);
251 17 my $class_attr = $attribute->meta->find_attribute_by_name('moose_class');
252 17 ▶ my $factory = $class_attr ? $class_attr->get_value($attribute) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$class_attr ? : 17 0 17
253  
254 17 ▶ $factory ||= ( $attribute->type_constraint->name =~ / (?:ArrayRef|Maybe) \[ (.+?) \] /x )[0];
Condition33%1/3
$factory ||= ($attribute->type_constraint->name =~ / (?:ArrayRef|Maybe) \[ (.+?) \] /x)[0]
l!l&&r!l&&!r
1700
MC/DC0%0/2
$factory ||= ($attribute->type_constraint->name =~ / (?:ArrayRef|Maybe) \[ (.+?) \] /x)[0]
$factory ($attribute->type_constraint->name =~ / (?:ArrayRef|Maybe) \[ (.+?) \] /x)[0]
Truth table33%1/3
A ||= B
A= $factoryB= ($attribute->type_constraint->name =~ / (?:ArrayRef|Maybe) \[ (.+?) \] /x)[0]
ABresult
1X1
011
000
255  
256 17 ▶ if ( $factory ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($factory) 17 0 17
257  
258 17 $data->{$attr} = [ map { $factory->build( $_, 1 ) } $db_result->$attr ];
259 }
260 }
261 else {
262 84 $data->{$attr} = $db_result->$attr;
263 }
264  
265 }
266  
267 41 return $data;
268 }
269  
270 5 ▶sub _db_save_relationship {
Subroutine _db_save_relationship: called CC=5 SCAR=17.8
271 5 my ( $self, $attr ) = @_;
272  
273 5 my $dbic_result = $self->_dbic_result;
274  
275 5 my $name = $attr->name;
276 5 my $type = $attr->meta->get_attribute('rel')->get_value($attr);
277 5 my $value = $self->model->$name;
278  
279 5 ▶ if ( $type eq 'many_to_many' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'many_to_many') { } 0 5 5
elsif ($type eq 'has_many') { } 4 1 5
280 0 my $setter = 'set_' . $name;
281 0 my $records = $self->_value_to_db( $value );
282 0 $dbic_result->$setter( $self->_value_to_db( $value ) );
283  
284 0 ▶ for ( my $i=0; $i < @$value; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$value) 0 0 0
285 0 $value->[$i]->get_storage('DBIC')->update_model($records->[$i]);
286 }
287 }
288 elsif ( $type eq 'has_many' ) {
289  
290 4 foreach my $model ( @$value ) {
291 4 my $result = $self->_value_to_db( $model );
292  
293 # update_or_create_related can be noisy due to
294 # https://rt.cpan.org/Public/Bug/Display.html?id=104839
295 4 my $data = { $result->get_columns };
296 $result = $dbic_result->update_or_create_related( $name => {
297 7 map { $_ => $data->{$_} }
298 4 grep { defined $data->{$_} }
299 keys %$data
300 } );
301  
302 4 $model->get_storage('DBIC')->update_model($result);
303 }
304 }
305 }
306  
307 13 ▶sub update_model {
Subroutine update_model: called CC=1 SCAR=0.0
Pod undocumented
308 13 my ( $self, $dbic_result ) = @_;
309  
310 13 $self->model->update( $self->_data_for_model( ref $self->model, $dbic_result ) );
311 }
312  
313 3 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.0
Pod documented
314 3 my ( $self ) = @_;
315  
316 3 ▶ if ( $self->_dbic_result->in_storage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_dbic_result->in_storage) 3 0 3
317 3 $self->_dbic_result->delete;
318 }
319  
320 3 return;
321 }
322  
323 10 ▶sub in_storage {
Subroutine in_storage: called CC=1 SCAR=0.0
Pod documented
324 10 my ( $self ) = @_;
325  
326 10 return $self->_dbic_result->in_storage;
327 }
328  
329 class_has '_cached_dbic_attrs' => (
330 is => 'rw',
331 isa => 'HashRef',
332 10 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
333 10 );
334  
335 41 ▶sub _dbic_attrs {
Subroutine _dbic_attrs: called CC=4 SCAR=14.5
336 41 my ( $self, $model ) = @_;
337  
338 41 ▶ $model //= $self->model;
Condition33%1/3
$model //= $self->model
l!l&&r!l&&!r
4100
MC/DC0%0/2
$model //= $self->model
$model $self->model
Truth table33%1/3
A //= B
A= $modelB= $self->model
ABresult
1X1
011
000
339  
340 41 my $model_class = ref $model;
341 41 ▶ $model_class ||= $model;
Condition33%1/3
$model_class ||= $model
l!l&&r!l&&!r
0410
MC/DC0%0/2
$model_class ||= $model
$model_class $model
Truth table33%1/3
A ||= B
A= $model_classB= $model
ABresult
1X1
011
000
342  
343 41 ▶ if ( ! $self->_cached_dbic_attrs->{ $model_class } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_cached_dbic_attrs->{$model_class}) 11 30 41
344  
345 $self->_cached_dbic_attrs->{ $model_class } = [
346 11 grep { $_->does('DBIC') }
347 $model->meta->get_all_attributes
348 ];
349 }
350  
351 41 return $self->_cached_dbic_attrs->{ $model_class }
352  
353 }
354  
355 14 ▶sub _dbic_columns {
Subroutine _dbic_columns: called CC=3 SCAR=11.2
356 14 my ( $self, $model ) = @_;
357  
358 14 ▶ $model //= $self->model;
Condition33%1/3
$model //= $self->model
l!l&&r!l&&!r
0140
MC/DC0%0/2
$model //= $self->model
$model $self->model
Truth table33%1/3
A //= B
A= $modelB= $self->model
ABresult
1X1
011
000
359  
360 return [
361 14 ▶ grep { $_->does('DBIC') && ! $_->is_relationship }
Condition100%3/3
$_->does('DBIC') && !$_->is_relationship
!ll&&!rl&&r
22928
MC/DC100%2/2
$_->does('DBIC') && !$_->is_relationship
$_->does('DBIC') !$_->is_relationship
Truth table100%3/3
A && not(B)
A= $_->does('DBIC')B= !$_->is_relationship
ABresult
0X0
100
111
362 $model->meta->get_all_attributes
363 ];
364 }
365  
366 64 ▶sub _dbic_relationships {
Subroutine _dbic_relationships: called CC=3 SCAR=11.0
367 64 my ( $self, $model ) = @_;
368  
369 64 ▶ $model //= $self->model;
Condition66%2/3
$model //= $self->model
l!l&&r!l&&!r
41230
MC/DC0%0/2
$model //= $self->model
$model $self->model
Truth table66%2/3
A //= B
A= $modelB= $self->model
ABresult
1X1
011
000
370  
371 return [
372 64 ▶ grep { $_->does('DBIC') && $_->is_relationship }
Condition100%3/3
$_->does('DBIC') && $_->is_relationship
!ll&&!rl&&r
11412849
MC/DC100%2/2
$_->does('DBIC') && $_->is_relationship
$_->does('DBIC') $_->is_relationship
Truth table100%3/3
A && B
A= $_->does('DBIC')B= $_->is_relationship
ABresult
0X0
100
111
373 $model->meta->get_all_attributes
374 ];
375 }
376  
377 14 ▶sub _dbic_fk_relationships {
Subroutine _dbic_fk_relationships: called CC=2 SCAR=6.9
378 14 my ( $self, $model ) = @_;
379  
380 return [
381 grep {
382 9 my $type = $_->meta->get_attribute('rel')->get_value($_);
383 9 ▶ $type eq 'belongs_to' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'belongs_to' ? : 5 4 9
384 }
385 14 @{$self->_dbic_relationships($model)}
386 ];
387 }
388  
389 14 ▶sub _populate_dbic_result {
Subroutine _populate_dbic_result: called CC=2 SCAR=6.9
390 14 my ( $self ) = @_;
391  
392 14 for my $attr ( @{$self->_dbic_columns}, @{$self->_dbic_fk_relationships} ) {
393  
394 33 my $name = $attr->name;
395 33 my $value = $self->_value_to_db( $self->model->$name );
396  
397 33 $self->_dbic_result->$name( $value );
398 }
399 }
400  
401 37 ▶sub _value_to_db {
Subroutine _value_to_db: called CC=5 SCAR=17.1
402 37 my ( $self, $value ) = @_;
403  
404 37 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 37 37
elsif (builtin::blessed($value) and $value->can('does') and $value->does('Model::Envoy')) { } 5 64 69
405  
406 0 return [ map { $self->_value_to_db($_) } @$value ];
407 }
408 ▶ elsif ( blessed $value && $value->can('does') && $value->does('Model::Envoy') ) {
Condition66%4/6
builtin::blessed($value) and $value->can("does") and $value->does("Model::Envoy")
!ll&&!rl&&r
3205
builtin::blessed($value) and $value->can("does")
!ll&&!rl&&r
3205
MC/DC33%1/3
builtin::blessed($value) and $value->can("does") and $value->does("Model::Envoy")
builtin::blessed($value) $value->can("does") $value->does("Model::Envoy")
Truth table50%2/4
A and B and C
A= builtin::blessed($value)B= $value->can("does")C= $value->does("Model::Envoy")
ABCresult
0XX0
10X0
1100
1111
409  
410 5 my $dbic = $value->get_storage(ref $self);
411  
412 5 $dbic->_populate_dbic_result;
413 5 return $dbic->_dbic_result;
414 }
415  
416 32 return $value;
417 }
418  
419 package MooseX::Meta::Attribute::Trait::DBIC;
420 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
421 10 Moose::Util::meta_attribute_alias('DBIC');
422  
423 10 ▶use Moose::Util::TypeConstraints 'enum';
Subroutine BEGIN: called CC=1 SCAR=0.0
424  
425 10 has rel => (
426 is => 'ro',
427 isa => enum(['belongs_to','has_many','many_to_many']),
428 predicate => 'is_relationship'
429 );
430  
431 10 has mm_rel => (
432 is => 'ro',
433 isa => 'Str',
434 predicate => 'is_many_to_many',
435 );
436  
437 10 has primary_key => (
438 is => 'ro',
439 isa => 'Bool',
440 predicate => 'is_primary_key'
441 );
442  
443 package Moose::Meta::Attribute::Custom::Trait::EDBIC;
444 0 ▶ sub register_implementation {
Subroutine register_implementation: not called CC=1 SCAR=6.9
445 0 'MooseX::Meta::Attribute::Trait::DBIC'
446 };
447  
448 10 1;
« Envoy.pm ↑ Summary Envoy/Storage/Memory.pm »