| 1 | package DBIx::Class::Helper::Row::OnColumnChange; | ||||||||||||
| 2 | 56 | $DBIx::Class::Helper::Row::OnColumnChange::VERSION = '2.037000'; | |||||||||||
| 3 | # ABSTRACT: Do things when the values of a column change | ||||||||||||
| 4 | |||||||||||||
| 5 | 56 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 56 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 56 | ▶ | use parent 'DBIx::Class::Helper::Row::StorageValues', 'DBIx::Class::Row'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 56 | ▶ | use List::Util 'first'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 56 | ▶ | use DBIx::Class::Candy::Exports; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 56 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 56 | export_methods [qw(before_column_change around_column_change after_column_change)]; | |||||||||||
| 15 | |||||||||||||
| 16 | __PACKAGE__->mk_group_accessors(inherited => $_) | ||||||||||||
| 17 | 56 | for qw(_before_change _around_change _after_change); | |||||||||||
| 18 | |||||||||||||
| 19 | 57 | ▶ | sub before_column_change { | ||||||||||
|
Subroutine
before_column_change: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 20 | 57 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | unless @_ == 3; | ||||||||||||
| 22 | |||||||||||||
| 23 | 57 | my $self = shift; | |||||||||||
| 24 | |||||||||||||
| 25 | 57 | my $column = shift; | |||||||||||
| 26 | 57 | my $args = shift; | |||||||||||
| 27 | |||||||||||||
| 28 | 57 | ▶ | die 'method is a required parameter' unless $args->{method}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 57 | $args->{column} = $column; | |||||||||||
| 30 | 57 | $args->{txn_wrap} = !!$args->{txn_wrap}; | |||||||||||
| 31 | |||||||||||||
| 32 | 57 | ▶ | $self->_before_change([]) unless $self->_before_change; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 57 | push @{$self->_before_change}, $args; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 1 | ▶ | sub around_column_change { | ||||||||||
|
Subroutine
around_column_change: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 37 | 1 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | unless @_ == 3; | ||||||||||||
| 39 | |||||||||||||
| 40 | 1 | my $self = shift; | |||||||||||
| 41 | |||||||||||||
| 42 | 1 | my $column = shift; | |||||||||||
| 43 | 1 | my $args = shift; | |||||||||||
| 44 | |||||||||||||
| 45 | 1 | ▶ | die 'no method passed!' unless $args->{method}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 1 | $args->{column} = $column; | |||||||||||
| 47 | 1 | $args->{txn_wrap} = !!$args->{txn_wrap}; | |||||||||||
| 48 | |||||||||||||
| 49 | 1 | ▶ | $self->_around_change([]) unless $self->_around_change; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 1 | push @{$self->_around_change}, $args; | |||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 4 | ▶ | sub after_column_change { | ||||||||||
|
Subroutine
after_column_change: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 54 | 4 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | unless @_ == 3; | ||||||||||||
| 56 | |||||||||||||
| 57 | 3 | my $self = shift; | |||||||||||
| 58 | |||||||||||||
| 59 | 3 | my $column = shift; | |||||||||||
| 60 | 3 | my $args = shift; | |||||||||||
| 61 | |||||||||||||
| 62 | 3 | ▶ | die 'no method passed!' unless $args->{method}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 3 | $args->{column} = $column; | |||||||||||
| 64 | 3 | $args->{txn_wrap} = !!$args->{txn_wrap}; | |||||||||||
| 65 | |||||||||||||
| 66 | 3 | ▶ | $self->_after_change([]) unless $self->_after_change; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 3 | unshift @{$self->_after_change}, $args; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | 7 | ▶ | sub update { | ||||||||||
|
Subroutine
update: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||
| 71 | 7 | my ($self, $args) = @_; | |||||||||||
| 72 | |||||||||||||
| 73 | 7 | ▶ | $self->set_inflated_columns($args) if $args; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | |||||||||||||
| 75 | 7 | ▶ | my %dirty = $self->get_dirty_columns | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | or return $self; | ||||||||||||
| 77 | |||||||||||||
| 78 | 7 | ▶ | my @all_before = @{$self->_before_change || []}; | ||||||||||
|
Condition100%2/2
$self->_before_change || []
MC/DC100%1/1
$self->_before_change || []
$self->_before_change
Truth table100%2/2
A
A= $self->_before_change
| |||||||||||||
| 79 | 7 | ▶ | my @all_around = @{$self->_around_change || []}; | ||||||||||
|
Condition100%2/2
$self->_around_change || []
MC/DC100%1/1
$self->_around_change || []
$self->_around_change
Truth table100%2/2
A
A= $self->_around_change
| |||||||||||||
| 80 | 7 | ▶ | my @all_after = @{$self->_after_change || []}; | ||||||||||
|
Condition100%2/2
$self->_after_change || []
MC/DC100%1/1
$self->_after_change || []
$self->_after_change
Truth table100%2/2
A
A= $self->_after_change
| |||||||||||||
| 81 | |||||||||||||
| 82 | # prepare functions | ||||||||||||
| 83 | 7 | my @before = grep { defined $dirty{$_->{column}} } @all_before; | |||||||||||
| 84 | 7 | my @around = grep { defined $dirty{$_->{column}} } @all_around; | |||||||||||
| 85 | 7 | my @after = grep { defined $dirty{$_->{column}} } @all_after; | |||||||||||
| 86 | |||||||||||||
| 87 | 7 | my $inner = $self->next::can; | |||||||||||
| 88 | |||||||||||||
| 89 | my $final = $self->on_column_change_allow_override_args | ||||||||||||
| 90 | 1 | ▶ | ? sub { $self->$inner } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 91 | 7 | ▶ | : sub { $self->$inner($args) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 92 | |||||||||||||
| 93 | 7 | for ( reverse @around ) { | |||||||||||
| 94 | 2 | my $fn = $_->{method}; | |||||||||||
| 95 | 2 | my $old = $self->get_storage_value($_->{column}); | |||||||||||
| 96 | 2 | my $new = $dirty{$_->{column}}; | |||||||||||
| 97 | 2 | my $old_final = $final; | |||||||||||
| 98 | 2 | ▶ | $final = sub { $self->$fn($old_final, $old, $new) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | # do we wrap it in a transaction? | ||||||||||||
| 102 | 20 | ▶ | my $txn_wrap = first { defined $dirty{$_->{column}} && $_->{txn_wrap} } | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||
| 103 | 7 | @all_before, @all_around, @all_after; | |||||||||||
| 104 | |||||||||||||
| 105 | 7 | my $guard; | |||||||||||
| 106 | 7 | ▶ | $guard = $self->result_source->schema->txn_scope_guard if $txn_wrap; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | |||||||||||||
| 108 | 7 | for (@before) { | |||||||||||
| 109 | 5 | my $fn = $_->{method}; | |||||||||||
| 110 | 5 | my $old = $self->get_storage_value($_->{column}); | |||||||||||
| 111 | 5 | my $new = $dirty{$_->{column}}; | |||||||||||
| 112 | 5 | $self->$fn($old, $new); | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 7 | my $ret = $final->(); | |||||||||||
| 116 | |||||||||||||
| 117 | 7 | for (@after) { | |||||||||||
| 118 | 5 | my $fn = $_->{method}; | |||||||||||
| 119 | 5 | my $old = $self->get_storage_value($_->{column}); | |||||||||||
| 120 | 5 | my $new = $dirty{$_->{column}}; | |||||||||||
| 121 | 5 | $self->$fn($old, $new); | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | 5 | ▶ | $guard->commit if $txn_wrap; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | |||||||||||||
| 126 | 5 | $ret | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 2 | ▶ | sub on_column_change_allow_override_args { 0 } | ||||||||||
|
Subroutine
on_column_change_allow_override_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 130 | |||||||||||||
| 131 | 56 | 1; | |||||||||||
| 132 | |||||||||||||
| 133 | __END__ | ||||||||||||