File Coverage

blib/lib/Data/Sah/Coerce/perl/To_date/From_obj/datetime.pm
Criterion Covered Total %
statement 21 22 95.4
branch 5 6 83.3
condition 3 6 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 34 41 82.9


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_date::From_obj::datetime;
2              
3 4     4   1072 use 5.010001;
  4         17  
4 4     4   25 use strict;
  4         8  
  4         99  
5 4     4   20 use warnings;
  4         8  
  4         1214  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2021-11-28'; # DATE
9             our $DIST = 'Data-Sah-Coerce'; # DIST
10             our $VERSION = '0.052'; # VERSION
11              
12             sub meta {
13             +{
14 14     14 0 66 v => 4,
15             summary => 'Coerce date from DateTime object',
16             prio => 50,
17             };
18             }
19              
20             sub coerce {
21 14     14 0 53 my %args = @_;
22              
23 14         40 my $dt = $args{data_term};
24 14   50     43 my $coerce_to = $args{coerce_to} // 'float(epoch)';
25              
26 14         30 my $res = {};
27              
28 14   50     89 $res->{modules}{'Scalar::Util'} //= 0;
29              
30 14         68 $res->{expr_match} = join(
31             " && ",
32             "Scalar::Util::blessed($dt)",
33             "$dt\->isa('DateTime')",
34             );
35              
36 14 100       58 if ($coerce_to eq 'float(epoch)') {
    100          
    50          
37 7         17 $res->{expr_coerce} = "$dt\->epoch";
38             } elsif ($coerce_to eq 'DateTime') {
39 4         13 $res->{expr_coerce} = $dt;
40             } elsif ($coerce_to eq 'Time::Moment') {
41 3   50     20 $res->{modules}{'Time::Moment'} //= 0;
42 3         12 $res->{expr_coerce} = "Time::Moment->from_object($dt)";
43             } else {
44 0         0 die "BUG: Unknown coerce_to value '$coerce_to', ".
45             "please use float(epoch), DateTime, or Time::Moment";
46             }
47              
48 14         47 $res;
49             }
50              
51             1;
52             # ABSTRACT: Coerce date from DateTime object
53              
54             __END__
55              
56             =pod
57              
58             =encoding UTF-8
59              
60             =head1 NAME
61              
62             Data::Sah::Coerce::perl::To_date::From_obj::datetime - Coerce date from DateTime object
63              
64             =head1 VERSION
65              
66             This document describes version 0.052 of Data::Sah::Coerce::perl::To_date::From_obj::datetime (from Perl distribution Data-Sah-Coerce), released on 2021-11-28.
67              
68             =head1 SYNOPSIS
69              
70             To use in a Sah schema:
71              
72             ["date",{"x.perl.coerce_rules"=>["From_obj::datetime"]}]
73              
74             =head1 DESCRIPTION
75              
76             This rule coerces date from a L<DateTime> object. If C<coerce_to> is "DateTime"
77             that this rule does not do anything. Otherwise, it converts the DateTime object
78             to epoch (if C<coerce_to>="float(epoch)") or L<Time::Moment> object (if
79             C<coerce_to>="Time::Moment").
80              
81             =for Pod::Coverage ^(meta|coerce)$
82              
83             =head1 HOMEPAGE
84              
85             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce>.
86              
87             =head1 SOURCE
88              
89             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce>.
90              
91             =head1 AUTHOR
92              
93             perlancar <perlancar@cpan.org>
94              
95             =head1 CONTRIBUTING
96              
97              
98             To contribute, you can send patches by email/via RT, or send pull requests on
99             GitHub.
100              
101             Most of the time, you don't need to build the distribution yourself. You can
102             simply modify the code, then test via:
103              
104             % prove -l
105              
106             If you want to build the distribution (e.g. to try to install it locally on your
107             system), you can install L<Dist::Zilla>,
108             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
109             Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
110             beyond that are considered a bug and can be reported to me.
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             This software is copyright (c) 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
115              
116             This is free software; you can redistribute it and/or modify it under
117             the same terms as the Perl 5 programming language system itself.
118              
119             =head1 BUGS
120              
121             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce>
122              
123             When submitting a bug or request, please include a test-file or a
124             patch to an existing test-file that illustrates the bug or desired
125             feature.
126              
127             =cut