File Coverage

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