File Coverage

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