File Coverage

blib/lib/Data/Sah/Coerce/perl/To_bool/From_str/common_words.pm
Criterion Covered Total %
statement 17 17 100.0
branch 2 2 100.0
condition 4 4 100.0
subroutine 5 5 100.0
pod 0 2 0.0
total 28 30 93.3


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::To_bool::From_str::common_words;
2              
3 2     2   44 use 5.010001;
  2         7  
4 2     2   11 use strict;
  2         4  
  2         48  
5 2     2   10 use warnings;
  2         4  
  2         466  
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 4     4 0 27 v => 4,
15             summary => 'Convert common true/false words (e.g. "yes","true","on","1" to "1", and "no","false","off","0" to "")',
16             prio => 50,
17             args => {
18             # mostly for testing of args
19             ci => {
20             schema => 'bool*',
21             default => 1,
22             },
23             },
24             };
25             }
26              
27             sub coerce {
28 4     4 0 19 my %cargs = @_;
29              
30 4         9 my $dt = $cargs{data_term};
31 4   100     14 my $gen_args = $cargs{args} // {};
32              
33 4         8 my $res = {};
34              
35 4         9 $res->{expr_match} = join(
36             " && ",
37             "1",
38             );
39              
40 4 100 100     17 my $modifier = ($gen_args->{ci} // 1) ? "i" : "";
41 4         14 $res->{expr_coerce} = "$dt =~ /\\A(yes|true|on)\\z/$modifier ? 1 : $dt =~ /\\A(no|false|off|0)\\z/$modifier ? '' : $dt";
42              
43 4         21 $res;
44             }
45              
46             1;
47             # ABSTRACT: Convert common true/false words (e.g. "yes","true","on","1" to "1", and "no","false","off","0" to "")
48              
49             __END__
50              
51             =pod
52              
53             =encoding UTF-8
54              
55             =head1 NAME
56              
57             Data::Sah::Coerce::perl::To_bool::From_str::common_words - Convert common true/false words (e.g. "yes","true","on","1" to "1", and "no","false","off","0" to "")
58              
59             =head1 VERSION
60              
61             This document describes version 0.052 of Data::Sah::Coerce::perl::To_bool::From_str::common_words (from Perl distribution Data-Sah-Coerce), released on 2021-11-28.
62              
63             =head1 SYNOPSIS
64              
65             To use in a Sah schema:
66              
67             ["bool",{"x.perl.coerce_rules"=>["From_str::common_words"]}]
68              
69             =head1 DESCRIPTION
70              
71             This coercion rule converts "true", "yes", "on" (matched case-insensitively) to
72             "1"; and "false", "no", "off", "0" (matched case-insensitively) to "". All other
73             strings are left untouched.
74              
75             B<Note that this rule is incompatible with Perl's notion of true/false.> Perl
76             regards all non-empty string that isn't "0" (including "no", "false", "off") as
77             true. But this might be useful in CLI's or other places.
78              
79             =for Pod::Coverage ^(meta|coerce)$
80              
81             =head1 HOMEPAGE
82              
83             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce>.
84              
85             =head1 SOURCE
86              
87             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce>.
88              
89             =head1 AUTHOR
90              
91             perlancar <perlancar@cpan.org>
92              
93             =head1 CONTRIBUTING
94              
95              
96             To contribute, you can send patches by email/via RT, or send pull requests on
97             GitHub.
98              
99             Most of the time, you don't need to build the distribution yourself. You can
100             simply modify the code, then test via:
101              
102             % prove -l
103              
104             If you want to build the distribution (e.g. to try to install it locally on your
105             system), you can install L<Dist::Zilla>,
106             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
107             Dist::Zilla plugin and/or Pod::Weaver::Plugin. Any additional steps required
108             beyond that are considered a bug and can be reported to me.
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This software is copyright (c) 2021, 2020, 2019, 2018, 2017, 2016 by perlancar <perlancar@cpan.org>.
113              
114             This is free software; you can redistribute it and/or modify it under
115             the same terms as the Perl 5 programming language system itself.
116              
117             =head1 BUGS
118              
119             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Sah-Coerce>
120              
121             When submitting a bug or request, please include a test-file or a
122             patch to an existing test-file that illustrates the bug or desired
123             feature.
124              
125             =cut