Data/Sah/Compiler/perl/TH/date.pm

filter: Statementstmt 41.4% 41 / 99 Branchbran 5.4% 4 / 74 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 3 totaltotal 29.7% 56 / 188 CC 39 SCAR 55.3
CC
39
Cov
27%
superclause_sortable
411.9
superclause_comparable
132.0
handle_type
8.1
CRAP 251.0
↑ Summary Data/Sah/Compiler/perl/TH/timeofday.pm »
1 package Data::Sah::Compiler::perl::TH::date;
2  
3 8 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any '$log';
7  
8 8 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 extends 'Data::Sah::Compiler::perl::TH';
13 8 with 'Data::Sah::Type::date';
14  
15 8 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
16 8 our $DATE = '2024-02-16'; # DATE
17 8 our $DIST = 'Data-Sah'; # DIST
18 8 our $VERSION = '0.917'; # VERSION
19  
20 44 ▶sub handle_type {
Subroutine handle_type: called CC=5 SCAR=20.9
Pod undocumented
21 44 my ($self, $cd) = @_;
22 44 my $c = $self->compiler;
23 44 my $dt = $cd->{data_term};
24  
25 44 ▶ $cd->{coerce_to} = $cd->{nschema}[1]{"x.perl.coerce_to"} // 'float(epoch)';
Condition100%2/2
$cd->{"nschema"}[1]{"x.perl.coerce_to"} // "float(epoch)"
l!l
143
MC/DC100%1/1
$cd->{"nschema"}[1]{"x.perl.coerce_to"} // "float(epoch)"
$cd->{"nschema"}[1]{"x.perl.coerce_to"}
Truth table100%2/2
A
A= $cd->{"nschema"}[1]{"x.perl.coerce_to"}
Aresult
11
00
26  
27 44 my $coerce_to = $cd->{coerce_to};
28  
29 44 ▶ if ($coerce_to eq 'float(epoch)') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(epoch)') { } 44 0 44
elsif ($coerce_to eq 'DateTime') { } 0 0 0
elsif ($coerce_to eq 'Time::Moment') { } 0 0 0
30 44 $cd->{_ccl_check_type} = "!ref($dt) && $dt =~ /\\A[0-9]+\\z/";
31 } elsif ($coerce_to eq 'DateTime') {
32 0 $c->add_runtime_module($cd, 'Scalar::Util');
33 0 $cd->{_ccl_check_type} = "Scalar::Util::blessed($dt) && $dt\->isa('DateTime')";
34 } elsif ($coerce_to eq 'Time::Moment') {
35 0 $c->add_runtime_module($cd, 'Scalar::Util');
36 0 $cd->{_ccl_check_type} = "Scalar::Util::blessed($dt) && $dt\->isa('Time::Moment')";
37 } else {
38 0 die "BUG: Unknown coerce_to value '$coerce_to', use either ".
39 "float(epoch), DateTime, or Time::Moment";
40 }
41 }
42  
43 0 ▶sub superclause_comparable {
Subroutine superclause_comparable: not called CC=11 SCAR=48.8
Pod undocumented
44 0 my ($self, $which, $cd) = @_;
45 0 my $c = $self->compiler;
46 0 my $cv = $cd->{cl_value};
47 0 my $ct = $cd->{cl_term};
48 0 my $dt = $cd->{data_term};
49  
50 0 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"cl_is_expr"}) 0 0 0
51 # i'm lazy, technical debt
52 0 $c->_die($cd, "date's comparison with expression not yet supported");
53 }
54  
55 0 my $coerce_to = $cd->{coerce_to};
56 0 ▶ if ($coerce_to eq 'float(epoch)') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(epoch)') { } 0 0 0
elsif ($coerce_to eq 'DateTime') { } 0 0 0
elsif ($coerce_to eq 'Time::Moment') { } 0 0 0
57 0 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 0 0 0
elsif ($which eq 'in') { } 0 0 0
58 0 $c->add_ccl($cd, "$dt == $ct");
59 } elsif ($which eq 'in') {
60 0 $c->add_runtime_module($cd, 'List::Util');
61 0 $c->add_ccl($cd, "List::Util::first(sub{$dt == \$_}, $ct)");
62 }
63 } elsif ($coerce_to eq 'DateTime') {
64 # we need to encode this because otherwise just dumping DateTime object
65 # $cv will be unwieldy
66 0 my $ect = "DateTime->from_epoch(epoch=>".$cv->epoch.")";
67  
68 0 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 0 0 0
elsif ($which eq 'in') { } 0 0 0
69 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect)==0");
70 } elsif ($which eq 'in') {
71 0 $c->add_runtime_module($cd, 'List::Util');
72 0 $c->add_ccl($cd, "List::Util::first(sub{DateTime->compare($dt, \$_)==0}, $ect)");
73 }
74 } elsif ($coerce_to eq 'Time::Moment') {
75 # we need to encode this because otherwise just dumping DateTime object
76 # $cv will be unwieldy
77 0 my $ect = "Time::Moment->from_epoch(".$cv->epoch.")";
78  
79 0 ▶ if ($which eq 'is') {
Branch: 2 branches
BranchTrueFalseTotal
if ($which eq 'is') { } 0 0 0
elsif ($which eq 'in') { } 0 0 0
80 0 $c->add_ccl($cd, "$dt\->compare($ect)==0");
81 } elsif ($which eq 'in') {
82 0 $c->add_runtime_module($cd, 'List::Util');
83 0 $c->add_ccl($cd, "List::Util::first(sub{$dt\->compare(\$_)==0}, $ect)");
84 }
85 }
86 }
87  
88 17 ▶sub superclause_sortable {
Subroutine superclause_sortable: called CC=25 SCAR=60.2
Pod undocumented
89 17 my ($self, $which, $cd) = @_;
90 17 my $c = $self->compiler;
91 17 my $cv = $cd->{cl_value};
92 17 my $ct = $cd->{cl_term};
93 17 my $dt = $cd->{data_term};
94  
95 17 ▶ if ($cd->{cl_is_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"cl_is_expr"}) 0 17 17
96 # i'm lazy, technical debt
97 0 $c->_die($cd, "date's comparison with expression not yet supported");
98 }
99  
100 17 my $coerce_to = $cd->{coerce_to};
101 17 ▶ if ($coerce_to eq 'float(epoch)') {
Branch: 3 branches
BranchTrueFalseTotal
if ($coerce_to eq 'float(epoch)') { } 17 0 17
elsif ($coerce_to eq 'DateTime') { } 0 0 0
elsif ($coerce_to eq 'Time::Moment') { } 0 0 0
102 17 ▶ if ($which eq 'min') {
Branch: 6 branches
BranchTrueFalseTotal
if ($which eq 'min') { } 17 0 17
elsif ($which eq 'xmin') { } 0 0 0
elsif ($which eq 'max') { } 0 0 0
elsif ($which eq 'xmax') { } 0 0 0
elsif ($which eq 'between') { } 0 0 0
elsif ($which eq 'xbetween') { } 0 0 0
103 17 $c->add_ccl($cd, "$dt >= $cv");
104 } elsif ($which eq 'xmin') {
105 0 $c->add_ccl($cd, "$dt > $cv");
106 } elsif ($which eq 'max') {
107 0 $c->add_ccl($cd, "$dt <= $cv");
108 } elsif ($which eq 'xmax') {
109 0 $c->add_ccl($cd, "$dt < $cv");
110 } elsif ($which eq 'between') {
111 0 $c->add_ccl($cd, "$dt >= $cv->[0] && $dt <= $cv->[1]");
112 } elsif ($which eq 'xbetween') {
113 0 $c->add_ccl($cd, "$dt > $cv->[0] && $dt < $cv->[1]");
114 }
115 } elsif ($coerce_to eq 'DateTime') {
116 # we need to encode this because otherwise just dumping DateTime object
117 # $cv will be unwieldy
118 0 my ($ect, $ect0, $ect1);
119 0 ▶ if (ref($cv) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cv eq 'ARRAY') { } 0 0 0
120 0 $ect0 = "DateTime->from_epoch(epoch=>".$cv->[0]->epoch.")";
121 0 $ect1 = "DateTime->from_epoch(epoch=>".$cv->[1]->epoch.")";
122 } else {
123 0 $ect = "DateTime->from_epoch(epoch=>".$cv->epoch.")";
124 }
125  
126 0 ▶ if ($which eq 'min') {
Branch: 6 branches
BranchTrueFalseTotal
if ($which eq 'min') { } 0 0 0
elsif ($which eq 'xmin') { } 0 0 0
elsif ($which eq 'max') { } 0 0 0
elsif ($which eq 'xmax') { } 0 0 0
elsif ($which eq 'between') { } 0 0 0
elsif ($which eq 'xbetween') { } 0 0 0
127 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect) >= 0");
128 } elsif ($which eq 'xmin') {
129 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect) > 0");
130 } elsif ($which eq 'max') {
131 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect) <= 0");
132 } elsif ($which eq 'xmax') {
133 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect) < 0");
134 } elsif ($which eq 'between') {
135 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect0) >= 0 && DateTime->compare($dt, $ect1) <= 0");
136 } elsif ($which eq 'xbetween') {
137 0 $c->add_ccl($cd, "DateTime->compare($dt, $ect0) > 0 && DateTime->compare($dt, $ect1) < 0");
138 }
139 } elsif ($coerce_to eq 'Time::Moment') {
140 # we need to encode this because otherwise just dumping DateTime object
141 # $cv will be unwieldy
142 0 my ($ect, $ect0, $ect1);
143 0 ▶ if (ref($cv) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cv eq 'ARRAY') { } 0 0 0
144 0 $ect0 = "Time::Moment->from_epoch(".$cv->[0]->epoch.")";
145 0 $ect1 = "Time::Moment->from_epoch(".$cv->[1]->epoch.")";
146 } else {
147 0 $ect = "Time::Moment->from_epoch(".$cv->epoch.")";
148 }
149  
150 0 ▶ if ($which eq 'min') {
Branch: 6 branches
BranchTrueFalseTotal
if ($which eq 'min') { } 0 0 0
elsif ($which eq 'xmin') { } 0 0 0
elsif ($which eq 'max') { } 0 0 0
elsif ($which eq 'xmax') { } 0 0 0
elsif ($which eq 'between') { } 0 0 0
elsif ($which eq 'xbetween') { } 0 0 0
151 0 $c->add_ccl($cd, "$dt\->compare($ect) >= 0");
152 } elsif ($which eq 'xmin') {
153 0 $c->add_ccl($cd, "$dt\->compare($ect) > 0");
154 } elsif ($which eq 'max') {
155 0 $c->add_ccl($cd, "$dt\->compare($ect) <= 0");
156 } elsif ($which eq 'xmax') {
157 0 $c->add_ccl($cd, "$dt\->compare($ect) < 0");
158 } elsif ($which eq 'between') {
159 0 $c->add_ccl($cd, "$dt\->compare($ect0) >= 0 && $dt\->compare($ect1) <= 0");
160 } elsif ($which eq 'xbetween') {
161 0 $c->add_ccl($cd, "$dt\->compare($ect0) > 0 && $dt\->compare($ect1) < 0");
162 }
163 }
164 }
165  
166 8 1;
167 # ABSTRACT: perl's type handler for type "date"
168  
169 __END__
↑ Summary Data/Sah/Compiler/perl/TH/timeofday.pm »