File Coverage

blib/lib/Data/Sah/Coerce/perl/To_duration/From_str/Alami_EN.pm
Criterion Covered Total %
statement 17 17 100.0
branch 3 4 75.0
condition 2 4 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 27 32 84.3


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_duration::From_str::Alami_EN;
2              
3             # AUTHOR
4             our $DATE = '2019-11-28'; # DATE
5             our $DIST = 'Data-Sah-Coerce-perl-To_duration-From_str-Alami_EN'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 1     1   430925 use 5.010001;
  1         3  
9 1     1   6 use strict;
  1         2  
  1         17  
10 1     1   4 use warnings;
  1         3  
  1         192  
11              
12             sub meta {
13             +{
14 2     2 0 7662 v => 4,
15             summary => 'Coerce duration from string parsed by DateTime::Format::Alami::EN',
16             might_fail => 1,
17             prio => 60, # a bit lower than normal
18             precludes => [qr/\Astr_alami(_.+)?\z/, 'str_human'],
19             };
20             }
21              
22             sub coerce {
23 2     2 0 20 my %args = @_;
24              
25 2         4 my $dt = $args{data_term};
26 2   50     6 my $coerce_to = $args{coerce_to} // 'float(secs)';
27              
28 2         5 my $res = {};
29              
30 2         5 $res->{expr_match} = "!ref($dt)";
31 2   50     12 $res->{modules}{"DateTime::Format::Alami::EN"} //= 0;
32 2 50       10 $res->{expr_coerce} = join(
    100          
33             "",
34             "do { my \$res; eval { \$res = DateTime::Format::Alami::EN->new->parse_datetime_duration($dt, {format=>'combined'}) }; ",
35             ($coerce_to eq 'float(secs)' ? "if (\$@) { ['Invalid duration syntax'] } else { [undef, \$res->{seconds}] } " :
36             $coerce_to eq 'DateTime::Duration' ? "if (\$@) { ['Invalid duration syntax'] } else { [undef, \$res->{Duration}] } " :
37             (die "BUG: Unknown coerce_to '$coerce_to'")),
38             "}",
39             );
40 2         20 $res;
41             }
42              
43             1;
44             # ABSTRACT: Coerce duration from string parsed by DateTime::Format::Alami::EN
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             Data::Sah::Coerce::perl::To_duration::From_str::Alami_EN - Coerce duration from string parsed by DateTime::Format::Alami::EN
55              
56             =head1 VERSION
57              
58             This document describes version 0.006 of Data::Sah::Coerce::perl::To_duration::From_str::Alami_EN (from Perl distribution Data-Sah-Coerce-perl-To_duration-From_str-Alami_EN), released on 2019-11-28.
59              
60             =head1 SYNOPSIS
61              
62             To use in a Sah schema:
63              
64             ["duration",{"x.perl.coerce_rules"=>["From_str::Alami_EN"]}]
65              
66             =for Pod::Coverage ^(meta|coerce)$
67              
68             =head1 HOMEPAGE
69              
70             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce-perl-To_duration-From_str-Alami_EN>.
71              
72             =head1 SOURCE
73              
74             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-To_duration-From_str-Alami_EN>.
75              
76             =head1 BUGS
77              
78             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce-perl-To_duration-From_str-Alami_EN>
79              
80             When submitting a bug or request, please include a test-file or a
81             patch to an existing test-file that illustrates the bug or desired
82             feature.
83              
84             =head1 AUTHOR
85              
86             perlancar <perlancar@cpan.org>
87              
88             =head1 COPYRIGHT AND LICENSE
89              
90             This software is copyright (c) 2019, 2018, 2016 by perlancar@cpan.org.
91              
92             This is free software; you can redistribute it and/or modify it under
93             the same terms as the Perl 5 programming language system itself.
94              
95             =cut