File Coverage

blib/lib/Data/Sah/Coerce/perl/array/str_int_range.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod 0 2 0.0
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::array::str_int_range;
2              
3             our $DATE = '2019-07-26'; # DATE
4             our $VERSION = '0.004'; # VERSION
5              
6 1     1   1755 use 5.010001;
  1         3  
7 1     1   4 use strict;
  1         2  
  1         16  
8 1     1   4 use warnings;
  1         2  
  1         121  
9              
10             sub meta {
11             +{
12 1     1 0 12 v => 4,
13             might_fail => 1,
14             prio => 60, # a bit lower than normal
15             };
16             }
17              
18             sub coerce {
19 1     1 0 13 my %args = @_;
20              
21 1         3 my $dt = $args{data_term};
22              
23 1         1 my $res = {};
24              
25 1         3 $res->{expr_match} = "!ref($dt) && $dt =~ /\\A\\s*[+-]?\\d+\\s*(?:-|\\.\\.)\\s*[+-]?\\d+\\s*\\z/";
26              
27 1         4 $res->{expr_coerce} = join(
28             "",
29             "do { ",
30             "my (\$int1, \$int2) = $dt =~ /\\A\\s*([+-]?\\d+)\\s*(?:-|\\.\\.)\\s*([+-]?\\d+)\\s*\\z/; ",
31             "if (\$int2 - \$int1 > 1_000_000) { ['Range too big'] } ",
32             "else { [undef, [\$int1+0 .. \$int2+0]] } ",
33             "}",
34             );
35              
36 1         3 $res;
37             }
38              
39             1;
40             # ABSTRACT: Coerce array of ints from string in the form of "INT1-INT2"
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Data::Sah::Coerce::perl::array::str_int_range - Coerce array of ints from string in the form of "INT1-INT2"
51              
52             =head1 VERSION
53              
54             This document describes version 0.004 of Data::Sah::Coerce::perl::array::str_int_range (from Perl distribution Data-Sah-CoerceRule-array-str_int_range), released on 2019-07-26.
55              
56             =head1 DESCRIPTION
57              
58             The rule is not enabled by default. You can enable it in a schema using e.g.:
59              
60             ["array*", of=>"int", "x.coerce_rules"=>["str_int_range"]]
61              
62             =for Pod::Coverage ^(meta|coerce)$
63              
64             =head1 HOMEPAGE
65              
66             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-CoerceRule-array-str_int_range>.
67              
68             =head1 SOURCE
69              
70             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-CoerceRule-array-str_int_range>.
71              
72             =head1 BUGS
73              
74             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-CoerceRule-array-str_int_range>
75              
76             When submitting a bug or request, please include a test-file or a
77             patch to an existing test-file that illustrates the bug or desired
78             feature.
79              
80             =head1 AUTHOR
81              
82             perlancar <perlancar@cpan.org>
83              
84             =head1 COPYRIGHT AND LICENSE
85              
86             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
87              
88             This is free software; you can redistribute it and/or modify it under
89             the same terms as the Perl 5 programming language system itself.
90              
91             =cut