File Coverage

blib/lib/Data/Sah/Coerce/perl/To_duration/From_obj/datetime_duration.pm
Criterion Covered Total %
statement 19 20 95.0
branch 3 4 75.0
condition 2 4 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 29 35 82.8


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