AuditAny/AuditContext/Change.pm

filter: Statementstmt 35.1% 52 / 148 Branchbran 4.1% 1 / 24 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 34.2% 13 / 38 Podpod 100% 13 / 13 totaltotal 32.6% 79 / 242 CC 21 SCAR 27.3
CC
21
Cov
29%
__ANON__
72.0
arr_arr_ascii_table
30.0
__ANON__
12.0
CRAP 15.3
« AuditAny.pm ↑ Summary AuditAny/Collector/DBIC.pm »
1 package DBIx::Class::AuditAny::AuditContext::Change;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Default 'Change' context object class for DBIx::Class::AuditAny
6  
7 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use MooX::Types::MooseLike::Base 0.19 qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 extends 'DBIx::Class::AuditAny::AuditContext';
10  
11 12 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use DBIx::Class::AuditAny::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 DBIx::Class::AuditAny::AuditContext::Change - Default 'Change' context object for DBIC::AuditAny
17  
18 =head1 DESCRIPTION
19  
20 This is the class which represents a single captured change event, which could involve multiple
21 columns.
22  
23 =head1 ATTRIBUTES
24  
25 Docs regarding the API/purpose of the attributes and methods in this class still TBD...
26  
27 =head2 SourceContext
28  
29 The Source context
30  
31 =cut
32 12 has 'SourceContext', is => 'ro', isa => Object, required => 1;
33  
34 =head2 ChangeSetContext
35  
36 The parent ChangeSet
37  
38 =cut
39 0 ▶has 'ChangeSetContext', is => 'rw', isa => Maybe[Object], default => sub{undef};
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41  
42 =head2 action
43  
44 The type of action which triggered this change: insert, update or delete/
45  
46 =cut
47 12 has 'action', is => 'ro', isa => Enum[qw(insert update delete)], required => 1;
48  
49  
50 =head2 old_columns
51  
52 The column values of the row, -according to the db- *before* the change happens.
53 This should be an empty hashref in the case of 'insert'
54  
55 =cut
56 0 ▶has 'old_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
57  
58 =head2 to_columns
59  
60 The column changes specified -by the change- (specified by
61 the client/query). Note that this is different from 'new_columns' and
62 probably doesn't contain all the columns. This should be an empty
63 hashref in the case of 'delete'
64 (TODO: would 'change_columns' a better name than 'to_columns'?)
65  
66 =cut
67 0 ▶has 'to_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub{{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
68  
69 =head2 new_columns
70  
71 The column values of the row, -according to the db- *after* the change happens.
72 This should be an empty hashref in the case of 'delete'
73  
74 =cut
75 0 ▶has 'new_columns', is => 'ro', isa => HashRef, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
76  
77 =head2 condition
78  
79 The condition associated with this change, applies to 'update' and 'delete'
80  
81 =cut
82 0 ▶has 'condition', is => 'ro', isa => Ref, lazy => 1, default => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
83  
84 =head2 recorded
85  
86 Boolean flag set to true once the change data has been recorded
87  
88 =cut
89 4 ▶has 'recorded', is => 'rw', isa => Bool, default => sub{0}, init_arg => undef;
Subroutine __ANON__: called CC=1 SCAR=0.0
90  
91  
92 =head2 pri_key_value
93  
94 =cut
95 has 'pri_key_value', is => 'ro', isa => Maybe[Str], lazy => 1, default => sub {
96 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 $self->enforce_recorded;
98
99 # TEMP: this is a bridge for converting away from needing Row objects...
100 0 my $merge_cols = { %{$self->old_columns}, %{$self->new_columns} };
101 0 return $self->get_pri_key_value($merge_cols);
102
103 #my $Row = $self->Row || $self->origRow;
104 #return $self->get_pri_key_value($Row);
105 12 };
106  
107 =head2 orig_pri_key_value
108  
109 =cut
110 has 'orig_pri_key_value', is => 'ro', isa => Maybe[Str], lazy => 1, default => sub {
111 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
112
113 # TEMP: this is a bridge for converting away from needing Row objects...
114 0 my $merge_cols = { %{$self->new_columns},%{$self->old_columns} };
115 0 return $self->get_pri_key_value($merge_cols);
116
117 #return $self->get_pri_key_value($self->origRow);
118 12 };
119  
120  
121 =head2 change_ts
122  
123 =cut
124 has 'change_ts', is => 'ro', isa => InstanceOf['DateTime'], lazy => 1, default => sub {
125 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
126 4 $self->enforce_unrecorded;
127 4 return $self->get_dt;
128 12 };
129  
130 =head2 start_timeofday
131  
132 =cut
133 4 ▶has 'start_timeofday', is => 'ro', default => sub { [gettimeofday] };
Subroutine __ANON__: called CC=1 SCAR=0.0
134  
135 =head2 change_elapsed
136  
137 =cut
138 4 ▶has 'change_elapsed', is => 'rw', default => sub{undef};
Subroutine __ANON__: called CC=1 SCAR=0.0
139  
140 =head2 column_changes
141  
142 =cut
143 has 'column_changes', is => 'ro', isa => HashRef[Object], lazy => 1, default => sub {
144 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
145 0 $self->enforce_recorded;
146
147 0 my $old = $self->old_columns;
148 0 my $new = $self->new_columns;
149
150 # This logic is duplicated in DbicLink2. Not sure how to avoid it, though,
151 # and keep a clean API
152 0 my @changed = ();
153 0 foreach my $col (uniq(keys %$new,keys %$old)) {
154 0 ▶ next if (!(defined $new->{$col}) and !(defined $old->{$col}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new->{$col} or defined $old->{$col} 0 0 0
Condition0%0/3
defined $new->{$col} or defined $old->{$col}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $new->{$col} or defined $old->{$col}
defined $new->{$col} defined $old->{$col}
Truth table0%0/3
A or B
A= defined $new->{$col}B= defined $old->{$col}
ABresult
1X1
011
000
155 next if (
156 defined $new->{$col} and defined $old->{$col} and
157 0 ▶ $new->{$col} eq $old->{$col}
Branch: 1 branch
BranchTrueFalseTotal
if defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col} 0 0 0
Condition0%0/6
defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col}
!ll&&!rl&&r
000
defined $new->{$col} and defined $old->{$col}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $new->{$col} and defined $old->{$col} and $new->{$col} eq $old->{$col}
defined $new->{$col} defined $old->{$col} $new->{$col} eq $old->{$col}
Truth table0%0/4
A and B and C
A= defined $new->{$col}B= defined $old->{$col}C= $new->{$col} eq $old->{$col}
ABCresult
0XX0
10X0
1100
1111
158 );
159 0 push @changed, $col;
160 }
161
162 0 my %col_context = ();
163 0 my $class = $self->AuditObj->column_context_class;
164 0 foreach my $column (@changed) {
165 my $ColumnContext = $class->new(
166 AuditObj => $self->AuditObj,
167 ChangeContext => $self,
168 column_name => $column,
169 old_value => $old->{$column},
170 0 new_value => $new->{$column},
171 );
172 0 $col_context{$ColumnContext->column_name} = $ColumnContext;
173 }
174
175 0 return \%col_context;
176 12 };
177  
178 has 'column_datapoint_values', is => 'ro', isa => HashRef, lazy => 1, default => sub {
179 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 #my @Contexts = $self->all_column_changes;
181 0 my @Contexts = values %{$self->column_changes};
182 0 return { map { $_->column_name => $_->local_datapoint_data } @Contexts };
183 12 };
184  
185  
186 has 'column_changes_ascii', is => 'ro', isa => Str, lazy => 1, default => sub {
187 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 my $table = $self->column_changes_arr_arr_table;
189 0 return $self->arr_arr_ascii_table($table);
190 12 };
191  
192 has 'column_changes_json', is => 'ro', isa => Str, lazy => 1, default => sub {
193 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 my $table = $self->column_changes_arr_arr_table;
195 0 require JSON;
196 0 return JSON::encode_json($table);
197 12 };
198  
199  
200 has 'column_changes_arr_arr_table', is => 'ro', isa => ArrayRef,
201 lazy => 1, default => sub {
202 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
203 0 my @cols = $self->get_context_datapoint_names('column');
204
205 0 my @col_datapoints = values %{$self->column_datapoint_values};
206
207 0 my $table = [\@cols];
208 0 foreach my $col_data (@col_datapoints) {
209 0 ▶ my @row = map { $col_data->{$_} || undef } @cols;
Branch: 1 branch
BranchTrueFalseTotal
$col_data->{$_} or undef 0 0 0
210 0 push @$table, \@row;
211 }
212
213 0 return $table;
214 12 };
215  
216  
217  
218 =head1 METHODS
219  
220 =head2 class
221  
222 =head2 ResultSource
223  
224 =head2 source
225  
226 =head2 pri_key_column
227  
228 =head2 pri_key_count
229  
230 =head2 primary_columns
231  
232 =head2 get_pri_key_value
233  
234 =head2 record
235  
236 =head2 action_id
237  
238 =head2 enforce_recorded
239  
240 =head2 enforce_unrecorded
241  
242 =head2 all_column_changes
243  
244 =head2 arr_arr_ascii_table
245  
246 =cut
247 0 ▶sub class { (shift)->SourceContext->class }
Subroutine class: not called CC=1 SCAR=6.9
Pod documented
248 0 ▶sub ResultSource { (shift)->SourceContext->ResultSource }
Subroutine ResultSource: not called CC=1 SCAR=6.9
Pod documented
249 0 ▶sub source { (shift)->SourceContext->source }
Subroutine source: not called CC=1 SCAR=6.9
Pod documented
250 0 ▶sub pri_key_column { (shift)->SourceContext->pri_key_column }
Subroutine pri_key_column: not called CC=1 SCAR=6.9
Pod documented
251 0 ▶sub pri_key_count { (shift)->SourceContext->pri_key_column }
Subroutine pri_key_count: not called CC=1 SCAR=6.9
Pod documented
252 0 ▶sub primary_columns { (shift)->SourceContext->primary_columns }
Subroutine primary_columns: not called CC=1 SCAR=6.9
Pod documented
253 0 ▶sub get_pri_key_value { (shift)->SourceContext->get_pri_key_value(@_) }
Subroutine get_pri_key_value: not called CC=1 SCAR=6.9
Pod documented
254  
255 sub _build_tiedContexts {
256 0 ▶ my $self = shift;
Subroutine _build_tiedContexts: not called CC=2 SCAR=17.9
257 0 my @Contexts = ( $self->SourceContext );
258 0 ▶ unshift @Contexts, $self->ChangeSetContext if ($self->ChangeSetContext);
Branch: 1 branch
BranchTrueFalseTotal
if $self->ChangeSetContext 0 0 0
259 0 return \@Contexts;
260 }
261 sub _build_local_datapoint_data {
262 0 ▶ my $self = shift;
Subroutine _build_local_datapoint_data: not called CC=1 SCAR=6.9
263 0 $self->enforce_recorded;
264 0 return { map { $_->name => $_->get_value($self) } $self->get_context_datapoints('change') };
265 }
266  
267 sub record {
268 4 ▶ my $self = shift;
Subroutine record: called CC=3 SCAR=17.9
Pod documented
269 4 my $new_columns = shift;
270 4 $self->enforce_unrecorded;
271 4 $self->change_ts;
272 0 $self->change_elapsed(tv_interval($self->start_timeofday));
273
274 0 ▶ %{$self->new_columns} = %$new_columns if (
Branch: 1 branch
BranchTrueFalseTotal
if ref $new_columns eq "HASH" and scalar keys %$new_columns > 0 0 0 0
Condition0%0/3
ref $new_columns eq "HASH" and scalar keys %$new_columns > 0
!ll&&!rl&&r
000
MC/DC0%0/2
ref $new_columns eq "HASH" and scalar keys %$new_columns > 0
ref $new_columns eq "HASH" scalar keys %$new_columns > 0
Truth table0%0/3
A and B
A= ref $new_columns eq "HASH"B= scalar keys %$new_columns > 0
ABresult
0X0
100
111
275 ref($new_columns) eq 'HASH' and
276 scalar(keys %$new_columns) > 0
277 );
278
279 0 $self->recorded(1);
280 }
281  
282  
283 # action_id exists so collectors can store the action as a shorter id
284 # instead of the full name.
285 sub action_id {
286 0 ▶ my $self = shift;
Subroutine action_id: not called CC=3 SCAR=24.8
Pod documented
287 0 ▶ my $action = $self->action or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $action = $self->action or return undef 0 0 0
288 0 ▶ my $id = $self->_action_id_map->{$action} or die "Error looking up action_id";
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->_action_id_map->{$action} or die "Error looking up action_id" 0 0 0
289 0 return $id;
290 }
291  
292 has '_action_id_map', is => 'ro', default => sub {{
293 4 ▶ insert => 1,
Subroutine __ANON__: called CC=1 SCAR=0.0
294 update => 2,
295 delete => 3
296 12 }}, isa => HashRef[Int];
297  
298  
299  
300 sub enforce_unrecorded {
301 8 ▶ my $self = shift;
Subroutine enforce_unrecorded: called CC=2 SCAR=7.2
Pod documented
302 8 ▶ die "Error: Audit action already recorded!" if ($self->recorded);
Branch: 1 branch
BranchTrueFalseTotal
if $self->recorded 0 8 8
303 }
304  
305 sub enforce_recorded {
306 0 ▶ my $self = shift;
Subroutine enforce_recorded: not called CC=2 SCAR=17.9
Pod documented
307 0 ▶ die "Error: Audit action not recorded yet!" unless ($self->recorded);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->recorded 0 0 0
308 }
309  
310 0 ▶sub all_column_changes { values %{(shift)->column_changes} }
Subroutine all_column_changes: not called CC=1 SCAR=6.9
Pod documented
311  
312 sub arr_arr_ascii_table {
313 0 ▶ my $self = shift;
Subroutine arr_arr_ascii_table: not called CC=5 SCAR=34.0
Pod documented
314 0 my $table = shift;
315 0 ▶ die "Supplied table is not an arrayref" unless (ref($table) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $table eq "ARRAY" 0 0 0
316
317 0 require Text::TabularDisplay;
318 0 require Text::Wrap;
319
320 0 my $t = Text::TabularDisplay->new;
321
322 0 local $Text::Wrap::columns = 52;
323
324 0 my $header = shift @$table;
325 0 ▶ die "Encounted non-arrayref table row" unless (ref($header) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $header eq "ARRAY" 0 0 0
326
327 0 $t->add(@$header);
328 0 $t->add('');
329
330 0 foreach my $row (@$table) {
331 0 ▶ die "Encounted non-arrayref table row" unless (ref($row) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $row eq "ARRAY" 0 0 0
332 0 $t->add( map { Text::Wrap::wrap('','',$_) } @$row );
333 }
334
335 0 return $t->render;
336 }
337  
338 12 1;
339  
340 __END__
341  
342 =head1 SEE ALSO
343  
344 =over
345  
346 =item *
347  
348 L<DBIx::Class::AuditAny>
349  
350 =item *
351  
352 L<DBIx::Class>
353  
354 =back
355  
356 =head1 SUPPORT
357
358 IRC:
359
360 Join #rapidapp on irc.perl.org.
361  
362 =head1 AUTHOR
363  
364 Henry Van Styn <vanstyn@cpan.org>
365  
366 =head1 COPYRIGHT AND LICENSE
367  
368 This software is copyright (c) 2012-2015 by IntelliTree Solutions llc.
369  
370 This is free software; you can redistribute it and/or modify it under
371 the same terms as the Perl 5 programming language system itself.
372  
373 =cut
« AuditAny.pm ↑ Summary AuditAny/Collector/DBIC.pm »