blib/lib/DBIO/Timestamp.pm

filter: Statementstmt 100% 61 / 61 Branchbran 90.0% 18 / 20 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 94.8% 110 / 116 CC 17 SCAR 11.5
CC
17
Cov
96%
add_columns
5.0
_timestamp_for_column
4.1
prepare_update
4.0
CRAP 3.2
« blib/lib/DBIO/AccessBroker.pm ↑ Summary blib/lib/DBIO/Base.pm »
1 package DBIO::Timestamp;
2 # ABSTRACT: Automatically set and update timestamp columns
3  
4 378 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 378 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 378 our $VERSION = '0.900007';
8  
9 378 ▶use base qw/DBIO::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 378 ▶use DateTime::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 378 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 378 __PACKAGE__->mk_classdata('timestamp_class' => 'DateTime::Tiny');
16  
17  
18 12055 ▶sub add_columns {
Subroutine add_columns: called CC=5 SCAR=16.1
Pod documented
19 12055 my ($self, @cols) = @_;
20 12055 my @columns;
21  
22 12055 ▶ while (my $col = shift @cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = shift @cols) 37230 12055 49285
23 37230 ▶ my $info = ref $cols[0] ? shift @cols : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $cols[0] ? : 37173 57 37230
24  
25 37230 ▶ if (delete $info->{set_on_create}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $info->{"set_on_create"}) 27 37203 37230
26 27 $info->{_timestamp_on_create} = 1;
27 }
28  
29 37230 ▶ if (delete $info->{set_on_update}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $info->{"set_on_update"}) 13 37217 37230
30 13 $info->{_timestamp_on_update} = 1;
31 }
32  
33 37230 push @columns, $col => $info;
34 }
35  
36 12055 return $self->next::method(@columns);
37 }
38  
39  
40 3238 ▶sub prepare_insert {
Subroutine prepare_insert: called CC=4 SCAR=13.9
Pod documented
41 3238 my $self = shift;
42  
43 3238 my $columns_info = $self->result_source->columns_info;
44 3238 for my $col (keys %$columns_info) {
45 13737 my $info = $columns_info->{$col};
46 13737 ▶ next unless $info->{_timestamp_on_create};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"_timestamp_on_create"} 13716 21 13737
47 21 ▶ next if defined $self->get_column($col);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_column($col) 1 20 21
48 20 $self->store_column($col => $self->_timestamp_for_column($info));
49 }
50  
51 3238 return $self->next::method(@_);
52 }
53  
54  
55 128 ▶sub prepare_update {
Subroutine prepare_update: called CC=4 SCAR=13.9
Pod documented
56 128 my ($self, $upd) = @_;
57  
58 128 my $columns_info = $self->result_source->columns_info;
59 128 for my $col (keys %$columns_info) {
60 530 my $info = $columns_info->{$col};
61 530 ▶ next unless $info->{_timestamp_on_update};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"_timestamp_on_update"} 519 11 530
62 11 ▶ delete $upd->{$col} if $upd;
Branch: 1 branch
BranchTrueFalseTotal
if $upd 11 0 11
63 11 $self->set_inflated_columns({ $col => $self->_timestamp_for_column($info) });
64 }
65  
66 128 return $self->next::method($upd);
67 }
68  
69  
70 7 ▶sub get_timestamp {
Subroutine get_timestamp: called CC=2 SCAR=7.0
Pod documented
71 7 my $self = shift;
72 7 my $class = $self->timestamp_class;
73 7 ▶ $self->ensure_class_loaded($class) unless $class->can('now');
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("now") 0 7 7
74 7 return $class->now;
75 }
76  
77 # Resolve the value to store into a timestamp column. Columns managed by
78 # InflateColumn::DateTime deflate through the storage datetime formatter
79 # (format_datetime), which needs a full DateTime object — a DateTime::Tiny
80 # would break there. For such columns inflate the Tiny to a real DateTime (the
81 # lazy ->DateTime path, which loads DateTime on demand) so the deflate behaves
82 # exactly as a hand-set DateTime would. Every other column keeps the value
83 # get_timestamp returned as-is.
84 31 ▶sub _timestamp_for_column {
Subroutine _timestamp_for_column: called CC=4 SCAR=14.0
85 31 my ($self, $info) = @_;
86  
87 31 my $ts = $self->get_timestamp;
88  
89 return $ts->DateTime
90 if $info->{_ic_dt_method}
91 31 ▶ and Scalar::Util::blessed($ts)
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"_ic_dt_method"} and builtin::blessed($ts) and $ts->isa("DateTime::Tiny") 3 28 31
Condition66%4/6
$info->{"_ic_dt_method"} and builtin::blessed($ts) and $ts->isa("DateTime::Tiny")
!ll&&!rl&&r
2803
$info->{"_ic_dt_method"} and builtin::blessed($ts)
!ll&&!rl&&r
2803
MC/DC33%1/3
$info->{"_ic_dt_method"} and builtin::blessed($ts) and $ts->isa("DateTime::Tiny")
$info->{"_ic_dt_method"} builtin::blessed($ts) $ts->isa("DateTime::Tiny")
Truth table50%2/4
A and B and C
A= $info->{"_ic_dt_method"}B= builtin::blessed($ts)C= $ts->isa("DateTime::Tiny")
ABCresult
0XX0
10X0
1100
1111
92 and $ts->isa('DateTime::Tiny');
93  
94 28 return $ts;
95 }
96  
97  
98 4 ▶sub col_created {
Subroutine col_created: called CC=2 SCAR=6.9
Pod documented
99 4 my ($self, $name) = @_;
100 4 ▶ $name ||= 'created_at';
Condition100%2/2
$name ||= "created_at"
l!l
22
MC/DC100%1/1
$name ||= "created_at"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
101 4 $self->add_columns($name => {
102 data_type => 'timestamp',
103 set_on_create => 1,
104 is_nullable => 0,
105 });
106 }
107  
108  
109 4 ▶sub col_updated {
Subroutine col_updated: called CC=2 SCAR=6.9
Pod documented
110 4 my ($self, $name) = @_;
111 4 ▶ $name ||= 'updated_at';
Condition100%2/2
$name ||= "updated_at"
l!l
22
MC/DC100%1/1
$name ||= "updated_at"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
112 4 $self->add_columns($name => {
113 data_type => 'timestamp',
114 set_on_create => 1,
115 set_on_update => 1,
116 is_nullable => 0,
117 });
118 }
119  
120  
121 2 ▶sub cols_updated_created {
Subroutine cols_updated_created: called CC=1 SCAR=0.0
Pod documented
122 2 my ($self) = @_;
123 2 $self->col_created;
124 2 $self->col_updated;
125 }
126  
127 378 1;
128  
129 __END__
« blib/lib/DBIO/AccessBroker.pm ↑ Summary blib/lib/DBIO/Base.pm »