Helper/Row/ProxyResultSetUpdate.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 81.5% 31 / 38 CC 6 SCAR 16.3
CC
6
Cov
79%
update
7.2
BEGIN
1.0
BEGIN
1.0
CRAP 5.1
« Helper/ResultSet/Shortcut/OrderByMagic.pm ↑ Summary Helper/Row/JoinTable.pm »
1 package DBIx::Class::Helper::Row::ProxyResultSetUpdate;
2 54 $DBIx::Class::Helper::Row::ProxyResultSetUpdate::VERSION = '2.032002';
3 # ABSTRACT: Efficiently reuse ResultSet updates from results
4  
5 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 54 ▶use parent 'DBIx::Class::Helper::Row::SelfResultSet', 'DBIx::Class::Row';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub update {
11 1 ▶ my ($self, $upd) = @_;
Subroutine update: called CC=6 SCAR=19.7
Pod documented
12  
13 1 ▶ $self->set_inflated_columns($upd) if $upd;
Branch: 1 branch
BranchTrueFalseTotal
if $upd 1 0 1
14  
15 1 ▶ my %to_update = $self->get_dirty_columns
Branch: 1 branch
BranchTrueFalseTotal
my(%to_update) = $self->get_dirty_columns or return $self 0 1 1
16 or return $self;
17  
18 1 ▶ $self->throw_exception( "Not in database" ) unless $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_storage 0 1 1
19  
20 # copied directly from DBIx::Class::Row except for this line
21 1 my $rows = $self->self_rs->update(\%to_update);
22 1 ▶ if ($rows == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows == 0) { } 0 1 1
elsif ($rows > 1) { } 0 1 1
23 0 $self->throw_exception( "Can't update ${self}: row not found" );
24 } elsif ($rows > 1) {
25 0 $self->throw_exception("Can't update ${self}: updated more than one row");
26 }
27 1 $self->{_dirty_columns} = {};
28 1 $self->{related_resultsets} = {};
29 1 delete $self->{_column_data_in_storage};
30 1 return $self;
31 }
32  
33 54 1;
34  
35 __END__
36  
37 =pod
38  
39 =head1 NAME
40  
41 DBIx::Class::Helper::Row::ProxyResultSetUpdate - Efficiently reuse ResultSet updates from results
42  
43 =head1 SYNOPSIS
44  
45 ResultSet:
46  
47 package MyApp::Schema::ResultSet::Foo;
48  
49 use parent 'DBIx::Class::ResultSet';
50  
51 sub update {
52 my ($self, $data) = @_;
53  
54 die 'you fool!' if $data->{name} eq 'fool';
55  
56 return $self->next::method($data);
57 }
58  
59 Result:
60  
61 package MyApp::Schema::Result::Foo;
62  
63 use parent 'DBIx::Class::Core';
64  
65 __PACKAGE__->load_components(qw( Helper::Row::ProxyResultSetUpdate ));
66  
67 ...
68  
69 or with L<DBIx::Class::Candy>:
70  
71 package MyApp::Schema::Result::Foo;
72  
73 use DBIx::Class::Candy -components => ['Helper::Row::ProxyResultSetMethod'];
74  
75 ...
76  
77 =head1 DESCRIPTION
78  
79 This module makes reusing resultset updates from a result trivially easy.
80 Often the only way that people share update methods is by overriding update
81 in their resultset to use L<DBIx::Class::ResultSet/update_all>. Unfortunately,
82 that can end up being wildly inefficient. Instead, if you can write your
83 update in terms of the resultset, you can make your code much faster and more
84 efficient.
85  
86 =head1 AUTHOR
87  
88 Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
89  
90 =head1 COPYRIGHT AND LICENSE
91  
92 This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt.
93  
94 This is free software; you can redistribute it and/or modify it under
95 the same terms as the Perl 5 programming language system itself.
96  
97 =cut
« Helper/ResultSet/Shortcut/OrderByMagic.pm ↑ Summary Helper/Row/JoinTable.pm »