File Coverage

blib/lib/Data/Sah/Coerce/perl/str/str_to_currency_pair.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition 1 2 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 22 25 88.0


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::str::str_to_currency_pair;
2              
3             our $DATE = '2019-07-25'; # DATE
4             our $VERSION = '0.002'; # VERSION
5              
6 1     1   1785 use 5.010001;
  1         4  
7 1     1   5 use strict;
  1         2  
  1         16  
8 1     1   5 use warnings;
  1         1  
  1         184  
9              
10             sub meta {
11             +{
12 1     1 0 11 v => 4,
13             might_fail => 1,
14             prio => 50,
15             };
16             }
17              
18             sub coerce {
19 1     1 0 14 my %args = @_;
20              
21 1         2 my $dt = $args{data_term};
22              
23 1         2 my $res = {};
24              
25 1         3 $res->{expr_match} = "!ref($dt)";
26 1   50     9 $res->{modules}{"Locale::Codes::Currency_Codes"} //= 0;
27 1         4 $res->{expr_coerce} = join(
28             "",
29             "do { my \$res; ",
30             " my (\$cur1, \$cur2) = $dt =~ m!\\A(\\S+)/(\\S+)\\z! or do { \$res = ['Invalid currency pair syntax, please use CUR1/CUR2 syntax']; goto RETURN_RES }; ",
31              
32             # check currency1 differs from currency2
33             " if (\$cur1 eq \$cur2) { \$res = ['Base currency and quote currency must differ']; goto RETURN_RES } ",
34              
35             # check currency1
36             " \$cur1 = uc \$cur1; ",
37             " unless (\$Locale::Codes::Data{currency}{code2id}{alpha}{\$cur1}) { ",
38             " \$res = ['Unknown base currency code: ' . \$cur1]; goto RETURN_RES; ",
39             " } ",
40              
41             # check currency2
42             " \$cur2 = uc \$cur2; ",
43             " unless (\$Locale::Codes::Data{currency}{code2id}{alpha}{\$cur2}) { ",
44             " \$res = ['Unknown quote currency code: ' . \$cur2]; goto RETURN_RES; ",
45             " } ",
46              
47             " \$res = [undef, \"\$cur1/\$cur2\"]; ",
48              
49             " RETURN_RES: \$res; ",
50             "}",
51             );
52              
53 1         4 $res;
54             }
55              
56             1;
57             # ABSTRACT: Coerce string into currency pair, e.g. USD/IDR
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             Data::Sah::Coerce::perl::str::str_to_currency_pair - Coerce string into currency pair, e.g. USD/IDR
68              
69             =head1 VERSION
70              
71             This document describes version 0.002 of Data::Sah::Coerce::perl::str::str_to_currency_pair (from Perl distribution Data-Sah-Coerce-perl-str-str_to_currency_pair), released on 2019-07-25.
72              
73             =head1 DESCRIPTION
74              
75             This coercion rules checks that:
76              
77             =over
78              
79             =item * string is in the form of "I<currency1>/I<currency2>"
80              
81             =item * I<currency1> is not the same as I<currency2>
82              
83             =item * I<currency1> is a known currency code
84              
85             =item * I<currency2> is a known currency code
86              
87             =back
88              
89             The rule is not enabled by default. You can enable it in a schema using e.g.:
90              
91             ["str", "x.perl.coerce_rules"=>["str_to_currency_pair"]]
92              
93             =for Pod::Coverage ^(meta|coerce)$
94              
95             =head1 HOMEPAGE
96              
97             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce-perl-str-str_to_currency_pair>.
98              
99             =head1 SOURCE
100              
101             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-str-str_to_currency_pair>.
102              
103             =head1 BUGS
104              
105             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-str-str_to_currency_pair>
106              
107             When submitting a bug or request, please include a test-file or a
108             patch to an existing test-file that illustrates the bug or desired
109             feature.
110              
111             =head1 AUTHOR
112              
113             perlancar <perlancar@cpan.org>
114              
115             =head1 COPYRIGHT AND LICENSE
116              
117             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
118              
119             This is free software; you can redistribute it and/or modify it under
120             the same terms as the Perl 5 programming language system itself.
121              
122             =cut