| 1 | package DBIx::Class::Helper::Row::OnColumnChange; | ||||||||||
| 2 | 54 | $DBIx::Class::Helper::Row::OnColumnChange::VERSION = '2.033001'; | |||||||||
| 3 | # ABSTRACT: Do things when the values of a column change | ||||||||||
| 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::StorageValues', 'DBIx::Class::Row'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 54 | ▶ | use List::Util 'first'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 54 | ▶ | use DBIx::Class::Candy::Exports; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 54 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 54 | export_methods [qw(before_column_change around_column_change after_column_change)]; | |||||||||
| 15 | |||||||||||
| 16 | __PACKAGE__->mk_group_accessors(inherited => $_) | ||||||||||
| 17 | 54 | for qw(_before_change _around_change _after_change); | |||||||||
| 18 | |||||||||||
| 19 | sub before_column_change { | ||||||||||
| 20 | 55 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||
|
Subroutine
before_column_change: called CC=4 SCAR=13.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 21 | unless @_ == 3; | ||||||||||
| 22 | |||||||||||
| 23 | 55 | my $self = shift; | |||||||||
| 24 | |||||||||||
| 25 | 55 | my $column = shift; | |||||||||
| 26 | 55 | my $args = shift; | |||||||||
| 27 | |||||||||||
| 28 | 55 | ▶ | die 'method is a required parameter' unless $args->{method}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 55 | $args->{column} = $column; | |||||||||
| 30 | 55 | $args->{txn_wrap} = !!$args->{txn_wrap}; | |||||||||
| 31 | |||||||||||
| 32 | 55 | ▶ | $self->_before_change([]) unless $self->_before_change; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 55 | push @{$self->_before_change}, $args; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub around_column_change { | ||||||||||
| 37 | 1 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||
|
Subroutine
around_column_change: called CC=4 SCAR=14.1
Pod
documented
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 | sub after_column_change { | ||||||||||
| 54 | 4 | ▶ | die 'Invalid number of arguments. One $column => $args pair at a time.' | ||||||||
|
Subroutine
after_column_change: called CC=4 SCAR=13.9
Pod
documented
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 | sub update { | ||||||||||
| 71 | 7 | ▶ | my ($self, $args) = @_; | ||||||||
|
Subroutine
update: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||
| 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 || []}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 7 | ▶ | my @all_around = @{$self->_around_change || []}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 7 | ▶ | my @all_after = @{$self->_after_change || []}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 54 | 1; | |||||||||
| 132 | |||||||||||
| 133 | __END__ | ||||||||||
| 134 | |||||||||||
| 135 | =pod | ||||||||||
| 136 | |||||||||||
| 137 | =head1 NAME | ||||||||||
| 138 | |||||||||||
| 139 | DBIx::Class::Helper::Row::OnColumnChange - Do things when the values of a column change | ||||||||||
| 140 | |||||||||||
| 141 | =head1 SYNOPSIS | ||||||||||
| 142 | |||||||||||
| 143 | package MyApp::Schema::Result::Account; | ||||||||||
| 144 | |||||||||||
| 145 | use parent 'DBIx::Class::Core'; | ||||||||||
| 146 | |||||||||||
| 147 | __PACKAGE__->load_components(qw(Helper::Row::OnColumnChange)); | ||||||||||
| 148 | |||||||||||
| 149 | __PACKAGE__->table('Account'); | ||||||||||
| 150 | |||||||||||
| 151 | __PACKAGE__->add_columns( | ||||||||||
| 152 | id => { | ||||||||||
| 153 | data_type => 'integer', | ||||||||||
| 154 | is_auto_increment => 1, | ||||||||||
| 155 | }, | ||||||||||
| 156 | amount => { | ||||||||||
| 157 | data_type => 'float', | ||||||||||
| 158 | keep_storage_value => 1, | ||||||||||
| 159 | }, | ||||||||||
| 160 | ); | ||||||||||
| 161 | sub on_column_change_allow_override_args { 1 } | ||||||||||
| 162 | |||||||||||
| 163 | __PACKAGE__->before_column_change( | ||||||||||
| 164 | amount => { | ||||||||||
| 165 | method => 'bank_transfer', | ||||||||||
| 166 | txn_wrap => 1, | ||||||||||
| 167 | } | ||||||||||
| 168 | ); | ||||||||||
| 169 | |||||||||||
| 170 | sub bank_transfer { | ||||||||||
| 171 | my ($self, $old_value, $new_value) = @_; | ||||||||||
| 172 | |||||||||||
| 173 | my $delta = abs($old_value - $new_value); | ||||||||||
| 174 | if ($old_value < $new_value) { | ||||||||||
| 175 | Bank->subtract($delta) | ||||||||||
| 176 | } else { | ||||||||||
| 177 | Bank->add($delta) | ||||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 1; | ||||||||||
| 182 | |||||||||||
| 183 | or with L<DBIx::Class::Candy>: | ||||||||||
| 184 | |||||||||||
| 185 | package MyApp::Schema::Result::Account; | ||||||||||
| 186 | |||||||||||
| 187 | use DBIx::Class::Candy -components => ['Helper::Row::OnColumnChange']; | ||||||||||
| 188 | |||||||||||
| 189 | table 'Account'; | ||||||||||
| 190 | |||||||||||
| 191 | column id => { | ||||||||||
| 192 | data_type => 'integer', | ||||||||||
| 193 | is_auto_increment => 1, | ||||||||||
| 194 | }; | ||||||||||
| 195 | |||||||||||
| 196 | column amount => { | ||||||||||
| 197 | data_type => 'float', | ||||||||||
| 198 | keep_storage_value => 1, | ||||||||||
| 199 | }; | ||||||||||
| 200 | sub on_column_change_allow_override_args { 1 } | ||||||||||
| 201 | |||||||||||
| 202 | before_column_change amount => { | ||||||||||
| 203 | method => 'bank_transfer', | ||||||||||
| 204 | txn_wrap => 1, | ||||||||||
| 205 | }; | ||||||||||
| 206 | |||||||||||
| 207 | sub bank_transfer { | ||||||||||
| 208 | my ($self, $old_value, $new_value) = @_; | ||||||||||
| 209 | |||||||||||
| 210 | my $delta = abs($old_value - $new_value); | ||||||||||
| 211 | if ($old_value < $new_value) { | ||||||||||
| 212 | Bank->subtract($delta) | ||||||||||
| 213 | } else { | ||||||||||
| 214 | Bank->add($delta) | ||||||||||
| 215 | } | ||||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | 1; | ||||||||||
| 219 | |||||||||||
| 220 | =head1 DESCRIPTION | ||||||||||
| 221 | |||||||||||
| 222 | This module codifies a pattern that I've used in a number of projects, namely | ||||||||||
| 223 | that of doing B<something> when a column changes it's value in the database. | ||||||||||
| 224 | It leverages L<DBIx::Class::Helper::Row::StorageValues> for passing in the | ||||||||||
| 225 | C<$old_value>, which do not have to use. If you leave the | ||||||||||
| 226 | C<keep_storage_value> out of the column definition it will just pass C<undef> | ||||||||||
| 227 | in as the $old_value. Also note the C<txn_wrap> option. This allows you to | ||||||||||
| 228 | specify that you want the call to C<update> and the call to the method you | ||||||||||
| 229 | requested to be wrapped in a transaction. If you end up calling more than | ||||||||||
| 230 | one method due to multiple column change methods and more than one specify | ||||||||||
| 231 | C<txn_wrap> it will still only wrap once. | ||||||||||
| 232 | |||||||||||
| 233 | I've gone to great lengths to ensure that order is preserved, so C<before> | ||||||||||
| 234 | and C<around> changes are called in order of definition and C<after> changes | ||||||||||
| 235 | are called in reverse order. | ||||||||||
| 236 | |||||||||||
| 237 | To be clear, the change methods only get called if the value will be changed | ||||||||||
| 238 | after C<update> runs. It correctly looks at the current value of the column | ||||||||||
| 239 | as well as the arguments passed to C<update>. | ||||||||||
| 240 | |||||||||||
| 241 | =head1 CANDY EXPORTS | ||||||||||
| 242 | |||||||||||
| 243 | If used in conjunction with L<DBIx::Class::Candy> this component will export: | ||||||||||
| 244 | |||||||||||
| 245 | =over | ||||||||||
| 246 | |||||||||||
| 247 | =item before_column_change | ||||||||||
| 248 | |||||||||||
| 249 | =item around_column_change | ||||||||||
| 250 | |||||||||||
| 251 | =item after_column_change | ||||||||||
| 252 | |||||||||||
| 253 | =back | ||||||||||
| 254 | |||||||||||
| 255 | =head1 NO SURPRISE RACE CONDITIONS | ||||||||||
| 256 | |||||||||||
| 257 | One thing that should be made totally clear is that the column change callbacks | ||||||||||
| 258 | are in effect B<< only once >> in a given update. If you expect to be able to | ||||||||||
| 259 | do something weird like calling one of the callbacks which changes a value with | ||||||||||
| 260 | an accessor which calls a callback etc etc, you probably just need to write some | ||||||||||
| 261 | code to do that yourself. This helper is specifically made with the aim of | ||||||||||
| 262 | reacting to changes immediately before they hit the database. | ||||||||||
| 263 | |||||||||||
| 264 | =head1 METHODS | ||||||||||
| 265 | |||||||||||
| 266 | =head2 before_column_change | ||||||||||
| 267 | |||||||||||
| 268 | __PACKAGE__->before_column_change( | ||||||||||
| 269 | col_name => { | ||||||||||
| 270 | method => 'method', # <-- anything that can be called as a method | ||||||||||
| 271 | txn_wrap => 1, # <-- true if you want it to be wrapped in a txn | ||||||||||
| 272 | } | ||||||||||
| 273 | ); | ||||||||||
| 274 | |||||||||||
| 275 | Note: the arguments passed to C<method> will be | ||||||||||
| 276 | C<< $self, $old_value, $new_value >>. | ||||||||||
| 277 | |||||||||||
| 278 | =head2 after_column_change | ||||||||||
| 279 | |||||||||||
| 280 | __PACKAGE__->after_column_change( | ||||||||||
| 281 | col_name => { | ||||||||||
| 282 | method => 'method', # <-- anything that can be called as a method | ||||||||||
| 283 | txn_wrap => 1, # <-- true if you want it to be wrapped in a txn | ||||||||||
| 284 | } | ||||||||||
| 285 | ); | ||||||||||
| 286 | |||||||||||
| 287 | Note: the arguments passed to C<method> will be | ||||||||||
| 288 | C<< $self, $new_value, $new_value >>. (Because the old value has been changed.) | ||||||||||
| 289 | |||||||||||
| 290 | =head2 around_column_change | ||||||||||
| 291 | |||||||||||
| 292 | __PACKAGE__->around_column_change( | ||||||||||
| 293 | col_name => { | ||||||||||
| 294 | method => 'method', # <-- anything that can be called as a method | ||||||||||
| 295 | txn_wrap => 1, # <-- true if you want it to be wrapped in a txn | ||||||||||
| 296 | } | ||||||||||
| 297 | ); | ||||||||||
| 298 | |||||||||||
| 299 | Note: the arguments passed to C<method> will be | ||||||||||
| 300 | C<< $self, $next, $old_value, $new_value >>. | ||||||||||
| 301 | |||||||||||
| 302 | Around is subtly different than the other two callbacks. You B<must> call | ||||||||||
| 303 | C<$next> in your method or it will not work at all. A silly example of how | ||||||||||
| 304 | this is done could be: | ||||||||||
| 305 | |||||||||||
| 306 | sub around_change_name { | ||||||||||
| 307 | my ($self, $next, $old, $new) = @_; | ||||||||||
| 308 | |||||||||||
| 309 | my $govt_records = $self->govt_records; | ||||||||||
| 310 | |||||||||||
| 311 | $next->(); | ||||||||||
| 312 | |||||||||||
| 313 | $govt_records->update({ name => $new }); | ||||||||||
| 314 | } | ||||||||||
| 315 | |||||||||||
| 316 | Note: the above code implies a weird database schema. I haven't actually seen | ||||||||||
| 317 | a time when I've needed around yet, but it seems like there is a use-case. | ||||||||||
| 318 | |||||||||||
| 319 | Also Note: you don't get to change the args to C<$next>. If you think you | ||||||||||
| 320 | should be able to, you probably don't understand what this component is for. | ||||||||||
| 321 | That or you know something I don't (equally likely.) | ||||||||||
| 322 | |||||||||||
| 323 | =head2 on_column_change_allow_override_args | ||||||||||
| 324 | |||||||||||
| 325 | This is a method that allows a user to circumvent a strange bug in the initial | ||||||||||
| 326 | implementation. Basically, if the user wanted, she could use | ||||||||||
| 327 | L</before_column_change> to override the value of a given column before | ||||||||||
| 328 | C<update> gets called, thus replacing the value. Unfortunately this worked in | ||||||||||
| 329 | the case of accessors setting the value, but not if the user had used an | ||||||||||
| 330 | argument to C<update>. To be clear, if you want the following to actually | ||||||||||
| 331 | replace the value: | ||||||||||
| 332 | |||||||||||
| 333 | __PACKAGE__->before_column_change( | ||||||||||
| 334 | name => { | ||||||||||
| 335 | method => sub { | ||||||||||
| 336 | my ($self, $old, $new) = @_; | ||||||||||
| 337 | |||||||||||
| 338 | $self->name(uc $new); | ||||||||||
| 339 | }, | ||||||||||
| 340 | }, | ||||||||||
| 341 | ); | ||||||||||
| 342 | |||||||||||
| 343 | you will need to define this in your result class: | ||||||||||
| 344 | |||||||||||
| 345 | sub on_column_change_allow_override_args { 1 } | ||||||||||
| 346 | |||||||||||
| 347 | If for some reason you need the old style, a default of false is already set. | ||||||||||
| 348 | If you are painted in the corner and need both, you can create an accessor and | ||||||||||
| 349 | set it yourself to change the behavior: | ||||||||||
| 350 | |||||||||||
| 351 | __PACKAGE__->mk_group_accessors(inherited => 'on_column_change_allow_override_args'); | ||||||||||
| 352 | ... | ||||||||||
| 353 | $obj->on_column_change_allow_override_args(1); # works the new way | ||||||||||
| 354 | |||||||||||
| 355 | =head1 AUTHOR | ||||||||||
| 356 | |||||||||||
| 357 | Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com> | ||||||||||
| 358 | |||||||||||
| 359 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 360 | |||||||||||
| 361 | This software is copyright (c) 2016 by Arthur Axel "fREW" Schmidt. | ||||||||||
| 362 | |||||||||||
| 363 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 364 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 365 | |||||||||||
| 366 | =cut | ||||||||||