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