File Coverage

blib/lib/Data/Sah/Coerce/perl/To_str/From_str/to_cryptoexchange_safename.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::to_cryptoexchange_safename;
2              
3             # AUTHOR
4             our $DATE = '2019-11-28'; # DATE
5             our $DIST = 'Data-Sah-Coerce-perl-To_str-From_str-to_cryptoexchange_safename'; # DIST
6             our $VERSION = '0.007'; # VERSION
7              
8 1     1   1872 use 5.010001;
  1         4  
9 1     1   5 use strict;
  1         3  
  1         19  
10 1     1   4 use warnings;
  1         2  
  1         194  
11              
12             sub meta {
13             +{
14 1     1 0 18 v => 4,
15             summary => 'Coerce string containing cryptoexchange code/name/safename to safename',
16             might_fail => 1,
17             prio => 50,
18             precludes => [qr/\AFrom_str::to_cryptoexchange_(.+)?\z/],
19             };
20             }
21              
22             sub coerce {
23 1     1 0 15 my %args = @_;
24              
25 1         3 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}{"CryptoExchange::Catalog"} //= 0;
31 1         5 $res->{expr_coerce} = join(
32             "",
33             "do { my \$cat = CryptoExchange::Catalog->new; my \@data = \$cat->all_data; ",
34             "my \$lc = lc($dt); my \$rec; for (\@data) { if (defined(\$_->{code}) && \$lc eq lc(\$_->{code}) || \$lc eq lc(\$_->{name}) || \$lc eq \$_->{safename}) { \$rec = \$_; last } } ",
35             "if (!\$rec) { ['Unknown cryptoexchange code/name/safename: ' . \$lc] } else { [undef, \$rec->{safename}] } ",
36             "}",
37             );
38              
39 1         3 $res;
40             }
41              
42             1;
43             # ABSTRACT: Coerce string containing cryptoexchange 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::to_cryptoexchange_safename - Coerce string containing cryptoexchange code/name/safename to safename
54              
55             =head1 VERSION
56              
57             This document describes version 0.007 of Data::Sah::Coerce::perl::To_str::From_str::to_cryptoexchange_safename (from Perl distribution Data-Sah-Coerce-perl-To_str-From_str-to_cryptoexchange_safename), 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::to_cryptoexchange_safename"]}]
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-to_cryptoexchange_safename>.
70              
71             =head1 SOURCE
72              
73             Source repository is at L<https://github.com/perlancar/perl-Data-Sah-Coerce-perl-To_str-From_str-to_cryptoexchange_safename>.
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-to_cryptoexchange_safename>
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