blib/lib/DBIx/QuickORM/Type/DateTime/Lazy.pm

filter: Statementstmt 100% 41 / 41 Branchbran 91.6% 11 / 12 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 98.7% 80 / 81 CC 9 SCAR 8.6
CC
9
Cov
98%
_compare
4.0
string
4.0
__ANON__
3.0
CRAP 2.4
« blib/lib/DBIx/QuickORM/Exception/DB.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/JSON.pm »
1 package DBIx::QuickORM::Type::DateTime::Lazy;
2 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 51 our $VERSION = '0.000031';
6  
7 51 ▶use DateTime();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use DateTime::TimeZone();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 51 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 51 ▶use overload(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶ '""' => sub { $_[0]->string },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 3 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 51 '<=>' => \&_compare,
15 'cmp' => \&_compare,
16 51 );
17  
18 =pod
19  
20 =encoding UTF-8
21  
22 =head1 NAME
23  
24 DBIx::QuickORM::Type::DateTime::Lazy - A DateTime built on first use.
25  
26 =head1 DESCRIPTION
27  
28 What L<DBIx::QuickORM::Type::DateTime> inflates to. It holds the value as the
29 database returned it and builds the L<DateTime> the first time one is needed.
30  
31 It has three methods: C<datetime> (a copy to use and change), C<datetime_ref>
32 (the DateTime itself, read only), and C<string>. Call DateTime methods on one
33 of the first two:
34  
35 my $year = $at->datetime_ref->year; # read: no copy
36 my $next = $at->datetime->add(days => 1); # change a copy
37 $row->update({created => $next});
38  
39 Comparisons work as DateTime's do, against a DateTime or another lazy value:
40 C<==>, C<< < >>, C<eq>, C<lt>, C<sort>, and the rest. Comparing with
41 anything else, such as a plain string, croaks.
42  
43 With a DateTime on the left, DateTime's own comparison runs, and it croaks on
44 a lazy value. Put the lazy value first, or compare its C<datetime_ref>.
45  
46 It stringifies to C<string>, an ISO 8601 form for display. It is always true.
47  
48 A row hands out the same cached lazy value on every read (see
49 L<DBIx::QuickORM::Role::Type/Cached inflated values>), so never change the
50 DateTime C<datetime_ref> returns.
51  
52 The object is a blessed closure, so dumps show only a code reference. A
53 fetched value that does not parse croaks when the DateTime is built, not when
54 it is fetched.
55  
56 =head1 SYNOPSIS
57  
58 my $at = $row->field('created'); # nothing parsed yet
59 my $year = $at->datetime_ref->year; # builds the DateTime once
60 my $text = "$at"; # 2024-02-03T04:05:06
61 my $soon = $at < DateTime->now; # compares the dates
62  
63 =head1 PUBLIC METHODS
64  
65 =over 4
66  
67 =item $lazy = DBIx::QuickORM::Type::DateTime::Lazy->new(\&build, $kind)
68  
69 A lazy value. C<build> returns the DateTime, and is called once, on first
70 need. C<$kind> is the field's: C<date>, C<timestamp>, or C<timestamptz>. It
71 picks the C<string> form.
72  
73 =back
74  
75 =cut
76  
77 38 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
78 38 my $class = shift;
79 38 my ($build, $kind) = @_;
80  
81 38 my $dt;
82 38 ▶ return bless(sub { $_[0] ? $kind : ($dt //= $build->()) }, $class);
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 17 69 86
Condition100%3/3
$dt //= &$build()
l!l&&r!l&&!r
33333
MC/DC100%2/2
$dt //= &$build()
$dt &$build()
Truth table100%3/3
A //= B
A= $dtB= &$build()
ABresult
1X1
011
000
83 }
84  
85 =pod
86  
87 =over 4
88  
89 =item $dt = $lazy->datetime
90  
91 The DateTime: a copy, so changing it does not change this value.
92  
93 =item $dt = $lazy->datetime_ref
94  
95 The DateTime itself, without copying it. Fast, but discouraged: changing it
96 changes every reader's value. Use it only to read.
97  
98 =back
99  
100 =cut
101  
102 2 ▶sub datetime { $_[0]->()->clone }
Subroutine datetime: called CC=1 SCAR=0.0
Pod documented
103 29 ▶sub datetime_ref { $_[0]->() }
Subroutine datetime_ref: called CC=1 SCAR=0.0
Pod documented
104  
105 =pod
106  
107 =over 4
108  
109 =item $text = $lazy->string
110  
111 The value in ISO 8601, for display. It is never sent to the database.
112  
113 =over 4
114  
115 =item *
116  
117 A timestamp with time zone: C<YYYY-MM-DDTHH:MM:SS+HH:MM>.
118  
119 =item *
120  
121 A timestamp without one: the same without the offset.
122  
123 =item *
124  
125 A date: C<YYYY-MM-DD>.
126  
127 =back
128  
129 Fractional seconds follow the seconds when there are any. Croaks, as
130 C<datetime_ref> does, when the value does not parse.
131  
132 =back
133  
134 =cut
135  
136 18 ▶sub string {
Subroutine string: called CC=4 SCAR=13.9
Pod documented
137 18 my $self = shift;
138 18 my $dt = $self->();
139 17 my $kind = $self->('kind');
140 17 ▶ return $dt->ymd if $kind eq 'date';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "date" 3 14 17
141  
142 14 my $out = $dt->ymd . 'T' . $dt->hms;
143 14 ▶ $out .= sprintf('.%09d', $dt->nanosecond) =~ s/0+$//r if $dt->nanosecond;
Branch: 1 branch
BranchTrueFalseTotal
if $dt->nanosecond 7 7 14
144 14 ▶ $out .= DateTime::TimeZone->offset_as_string($dt->offset, ':') if $kind eq 'timestamptz';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "timestamptz" 2 12 14
145 14 return $out;
146 }
147  
148 =pod
149  
150 =head1 PRIVATE METHODS
151  
152 =over 4
153  
154 =item $cmp = $lazy->_compare($other, $swapped)
155  
156 The overloaded C<< <=> >> and C<cmp>: L<DateTime>'s C<compare>, which croaks
157 when C<$other> is not a DateTime. A lazy C<$other> counts as its DateTime.
158  
159 =back
160  
161 =cut
162  
163 13 ▶sub _compare {
Subroutine _compare: called CC=4 SCAR=13.9
164 13 my ($self, $other, $swap) = @_;
165 13 ▶ $other = $other->() if blessed($other) && $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($other) and $other->isa(__PACKAGE__) 7 6 13
Condition100%3/3
builtin::blessed($other) and $other->isa(__PACKAGE__)
!ll&&!rl&&r
247
MC/DC100%2/2
builtin::blessed($other) and $other->isa(__PACKAGE__)
builtin::blessed($other) $other->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
166 13 my $c = DateTime->compare($self->(), $other);
167 11 ▶ return $swap ? -$c : $c;
Branch: 1 branch
BranchTrueFalseTotal
$swap ? : 0 11 11
168 }
169  
170 51 1;
171  
172 __END__
« blib/lib/DBIx/QuickORM/Exception/DB.pm ↑ Summary blib/lib/DBIx/QuickORM/Type/JSON.pm »