File Coverage

blib/lib/Data/Sah/Coerce/perl/To_str/From_str/ToCryptoCurrencySafename.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::ToCryptoCurrencySafename;
2              
3             # AUTHOR
4             our $DATE = '2019-11-28'; # DATE
5             our $DIST = 'Data-Sah-Coerce-perl-To_str-From_str-ToCryptoCurrencySafename'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 1     1   1769 use 5.010001;
  1         3  
9 1     1   5 use strict;
  1         2  
  1         17  
10 1     1   4 use warnings;
  1         2  
  1         167  
11              
12             sub meta {
13             +{
14 1     1 0 16 v => 4,
15             summary => 'Coerce string containing cryptocurrency code/name/safename to safename',
16             might_fail => 1,
17             prio => 50,
18             precludes => [qr/\Astr_to_cryptocurrency_(.+)?\z/],
19             };
20             }
21              
22             sub coerce {
23 1     1 0 16 my %args = @_;
24              
25 1         2 my $dt = $args{data_term};
26              
27 1         2 my $res = {};
28              
29 1         3 $res->{expr_match} = "!ref($dt)";
30 1   50     8 $res->{modules}{"CryptoCurrency::Catalog"} //= 0;
31 1         5 $res->{expr_coerce} = join(
32             "",
33             "do { my \$cat = CryptoCurrency::Catalog->new; ",
34             "my \$rec; eval { \$rec = \$cat->by_code($dt) }; if (\$@) { eval { \$rec = \$cat->by_name($dt) } } if (\$@) { eval { \$rec = \$cat->by_safename($dt) } } ",
35             "if (\$@) { ['Unknown cryptocurrency code/name/safename: ' . $dt] } else { [undef, \$rec->{safename}] } ",
36             "}",
37             );
38              
39 1         3 $res;
40             }
41              
42             1;
43             # ABSTRACT: Coerce string containing cryptocurrency code/name/safename to safename
44              
45             __END__
46              
47             =pod
48              
49             =encoding UTF-8
50              
51             =head1 NAME
52              
53             Data::Sah::Coerce::perl::To_str::From_str::ToCryptoCurrencySafename - Coerce string containing cryptocurrency code/name/safename to safename
54              
55             =head1 VERSION
56              
57             This document describes version 0.006 of Data::Sah::Coerce::perl::To_str::From_str::ToCryptoCurrencySafename (from Perl distribution Data-Sah-Coerce-perl-To_str-From_str-ToCryptoCurrencySafename), released on 2019-11-28.
58              
59             =head1 SYNOPSIS
60              
61             To use in a Sah schema:
62              
63             ["str",{"x.perl.coerce_rules"=>["From_str::ToCryptoCurrencySafename"]}]
64              
65             =for Pod::Coverage ^(meta|coerce)$
66              
67             =head1 HOMEPAGE
68              
69             Please visit the project's homepage at L<https://metacpan.org/release/Data-Sah-Coerce-perl-To_str-From_str-ToCryptoCurrencySafename>.
70              
71             =head1 SOURCE
72              
73             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-To_str-From_str-ToCryptoCurrencySafename>.
74              
75             =head1 BUGS
76              
77             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-ToCryptoCurrencySafename>
78              
79             When submitting a bug or request, please include a test-file or a
80             patch to an existing test-file that illustrates the bug or desired
81             feature.
82              
83             =head1 AUTHOR
84              
85             perlancar <perlancar@cpan.org>
86              
87             =head1 COPYRIGHT AND LICENSE
88              
89             This software is copyright (c) 2019, 2018 by perlancar@cpan.org.
90              
91             This is free software; you can redistribute it and/or modify it under
92             the same terms as the Perl 5 programming language system itself.
93              
94             =cut