blib/lib/DBIx/Class/Row/Delta.pm

filter: Statementstmt 76.9% 30 / 39 Branchbran 50.0% 2 / 4 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 81.8% 9 / 11 Podpod 16.6% 1 / 6 totaltotal 69.7% 46 / 66 CC 5 SCAR 8.2
CC
5
Cov
74%
changes
6.0
diff
3.0
delta_key_value
2.0
CRAP 2.3
↑ Summary
1 package DBIx::Class::Row::Delta;
2 BEGIN { ## no critic (RequireUseStrict, RequireUseWarnings)
3 2 ▶ $DBIx::Class::Row::Delta::VERSION = '0.0.4'; ## no critic (RequireUseStrict, RequireUseWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 BEGIN { ## no critic (RequireUseStrict, RequireUseWarnings)
6 2 ▶ $DBIx::Class::Row::Delta::DIST = 'DBIx-Class-Row-Delta'; ## no critic (RequireUseStrict, RequireUseWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
7 }
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 DBIx::Class::Row::Delta - Keep track of and report on changes to a
13 DBIC row object.
14  
15 =head1 DESCRIPTION
16  
17 Record an initial set of values for a DBIC row, and later on get a
18 string with the changed values.
19  
20 =head1 SYNOPSIS
21  
22 use DBIx::Class::Row::Delta;
23  
24 my $book = $book_rs->find(321);
25 my $book_notes_delta = DBIC::Row::Delta->new({
26 dbic_row => $book,
27 changes_sub => sub {
28 my ($row) = @_;
29 return {
30 "Book Type" => $row->book_type->type,
31 "Book Title" => $row->book_title->title // "N/A",
32 "Delivery Date" => $row->delivery_date->ymd,
33 };
34 },
35 });
36  
37 # ...
38 # Do stuff to $book, ->update(), etc.
39 # ...
40  
41 # Note: this will discard_changes on $book.
42 my $changes_string = $book_notes_delta->changes;
43 # e.g.
44 # Book SKU (1933021-002 => 1933023-001), Delivery Date (2012-01-18 => 2012-01-22)
45  
46 =cut
47  
48 2 has dbic_row => (is => "rw", required => 1);
49  
50 2 has changes_sub => (is => "ro", isa => "CodeRef", required => 1);
51  
52 4 ▶has _initial_key_value => (is => "rw", isa => "HashRef", default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 =head1 METHODS
57  
58 =head2 new({ $dbic_row!, &$changes_sub! }) : $new_object | die
59  
60 Create a new object. Start by taking a snapshot of the contents of
61 $dbic_row by calling $changes_sub->($dbic_row).
62  
63 The sub ref $changes_sub should return a hash ref with keys and values
64 that describe the state of the object. If the values look weird when
65 stringified, you're responsible for formatting them properly. All the
66 hash ref values should be strings or undef.
67  
68 Both dbic_row and changes_sub are required.
69  
70 =cut
71  
72 sub BUILD {
73 3 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
74 3 my ($args) = @_;
75 3 $self->_initial_key_value(
76 $self->changes_sub->( $self->dbic_row ),
77 );
78 }
79  
80 =head2 changes() : $delta_string | undef
81  
82 Return a string representation of the diff between the initial
83 snapshot and the current state of $dbic_row, or return undef if they
84 are the same. Only the changed values are reported.
85  
86 Example:
87  
88 Book SKU (1933021-002 => 1933023-001), Delivery Date (2012-01-18 => 2012-01-22)
89  
90 Note: This will start by calling $dbic_row->discard to refresh the
91 data properly.
92  
93 =cut
94  
95 sub changes {
96 0 ▶ my $self = shift;
Subroutine changes: not called CC=2 SCAR=17.9
Pod documented
97 0 my $delta_key_value = $self->delta_key_value();
98 0 ▶ keys %$delta_key_value or return undef; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
keys %$delta_key_value or return undef 0 0 0
99 0 return $self->changes_from_delta($delta_key_value);
100 }
101  
102 sub delta_key_value {
103 0 ▶ my $self = shift;
Subroutine delta_key_value: not called CC=1 SCAR=6.9
Pod undocumented
104  
105 0 my $dbic_row = $self->dbic_row;
106 0 $dbic_row->discard_changes();
107 0 my $current_key_value = $self->changes_sub->( $dbic_row );
108  
109 0 return $self->diff( $self->_initial_key_value, $current_key_value );
110 }
111  
112 sub diff {
113 1 ▶ my $self = shift;
Subroutine diff: called CC=3 SCAR=11.0
Pod undocumented
114 1 my ($before_key_value, $after_key_value) = @_;
115 return {
116 2 map { $_ => $after_key_value->{$_} }
117 1 ▶ grep { ($after_key_value->{$_} // "") ne ($before_key_value->{$_} // "") }
Condition75%3/4
$after_key_value->{$_} // ""
l!l
30
$before_key_value->{$_} // ""
l!l
21
MC/DC0%0/1
$after_key_value->{$_} // ""
$after_key_value->{$_}
MC/DC100%1/1
$before_key_value->{$_} // ""
$before_key_value->{$_}
Truth table50%1/2
A
A= $after_key_value->{$_}
Aresult
11
00
Truth table100%2/2
A
A= $before_key_value->{$_}
Aresult
11
00
118 keys %$after_key_value
119 };
120 }
121  
122 sub changes_from_delta {
123 1 ▶ my $self = shift;
Subroutine changes_from_delta: called CC=1 SCAR=0.0
Pod undocumented
124 1 my ($delta_key_value) = @_;
125 1 my $initial_key_value = $self->_initial_key_value;
126 return join(
127 ", ",
128 (
129 map {
130 1 "$_(" . empty($initial_key_value->{$_}) . " => "
131 2 . empty($delta_key_value->{$_}) . ")";
132 }
133 sort keys %$delta_key_value
134 ),
135 );
136 }
137  
138 sub empty {
139 4 ▶ my ($value) = @_;
Subroutine empty: called CC=2 SCAR=6.9
Pod undocumented
140 4 ▶ defined $value or return "''";
Branch: 1 branch
BranchTrueFalseTotal
defined $value or return "''" 1 3 4
141 3 return $value;
142 }
143  
144 2 __PACKAGE__->meta->make_immutable;
145  
146 2 1;
147  
148 __END__
149 =head1 BUGS
150  
151 Please report any bugs or feature requests to C<bug-dbix-class-row-delta at rt.cpan.org>, or through
152 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-Row-Delta>. I will be notified, and then you'll
153 automatically be notified of progress on your bug as I make changes.
154  
155 =head1 SUPPORT
156  
157 You can find documentation for this module with the perldoc command.
158  
159 perldoc DBIx::Class::Row::Delta
160  
161  
162 You can also look for information at:
163  
164 =over 4
165  
166 =item * RT: CPAN's request tracker
167  
168 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-Row-Delta>
169  
170 =item * AnnoCPAN: Annotated CPAN documentation
171  
172 L<http://annocpan.org/dist/DBIx-Class-Row-Delta>
173  
174 =item * CPAN Ratings
175  
176 L<http://cpanratings.perl.org/d/DBIx-Class-Row-Delta>
177  
178 =item * Search CPAN
179  
180 L<http://search.cpan.org/dist/DBIx-Class-Row-Delta/>
181  
182 =back
183  
184  
185 =head1 CONTRIBUTE
186  
187 The source for this module is on GitHub: https://github.com/jplindstrom/p5-DBIx-Class-Row-Delta
188  
189 Patches welcome, etc.
190  
191  
192 =head1 AUTHOR
193  
194 Johan Lindstrom - C<johanl@cpan.org> on behalf of
195 Net-A-Porter - L<http://www.net-a-porter.com/>
196  
197  
198  
199 =head1 LICENSE AND COPYRIGHT
200  
201 Copyright 2012- Net-A-Porter.
202  
203 This program is free software; you can redistribute it and/or modify it
204 under the terms of either: the GNU General Public License as published
205 by the Free Software Foundation; or the Artistic License.
206  
207 See http://dev.perl.org/licenses/ for more information.
208  
209  
210  
211 =head1 ACKNOWLEDGEMENTS
212  
213 Thanks to Net-A-Porter for providing time during one of the regular
214 Hack-days.
215  
216  
217 =cut
↑ Summary