lib/Finance/Bank/SentinelBenefits/Csv401kConverter/SymbolMap.pm

filter: Statementstmt 100% 38 / 38 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 54 / 54 CC 7 SCAR 11.0
CC
7
Cov
100%
get_symbol
4.0
BUILD
4.0
BEGIN
1.0
CRAP 3.0
« lib/Finance/Bank/SentinelBenefits/Csv401kConverter.pm ↑ Summary blib/lib/Finance/Bank/SentinelBenefits/example.pl »
1 package Finance::Bank::SentinelBenefits::Csv401kConverter::SymbolMap;
2 8 $Finance::Bank::SentinelBenefits::Csv401kConverter::SymbolMap::VERSION = '1.3';
3 8 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7  
8  
9 Finance::Bank::SentinelBenefits::Csv401kConverter::SymbolMap - fuzzy lookup
10 of security descriptions to symbols
11  
12  
13  
14 =head1 VERSION
15  
16 version 1.3
17  
18 =head1 SYNOPSIS
19  
20  
21  
22 This class is necessary because Sentinel does not always supply the symbol
23 in the download, so it can become necessary to do a lookup based on the
24 description field that they supply to find the correct security symbol.
25  
26  
27  
28 =cut
29  
30 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use Scalar::Util qw{ openhandle };
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 Constructor
34  
35  
36  
37 =head2 new()
38  
39  
40  
41 my $st = Bank::SentinelBenefits::Csv401kConverter::SymbolMap->new({
42 symbol_map => $HashRef[Str] | FileHandle });
43  
44 Can be initialzed either from a hash mapping of the form description -> symbol
45 or a comma delimited file of the same type.
46  
47  
48  
49 =cut
50  
51 =head1 Internal accessors
52  
53  
54  
55 =head2 $foo->symbol_map()
56  
57  
58  
59 This is either a hash ref of strings, mapping descriptions to symbols,
60 or a filehandle pointing to a file of the format C<'description','symbol>
61  
62  
63  
64 Not really for external use
65  
66  
67  
68 =head2 $foo->_true_symbol_map()
69  
70  
71  
72 This is a hash ref of strings, mapping descriptions to symbols. Internal use only.
73  
74  
75  
76 =cut
77  
78 8 has 'symbol_map' => (
79 is => 'ro',
80 isa => 'HashRef[Str] | FileHandle',
81 required => 1,
82 );
83  
84  
85  
86 8 has '_true_symbol_map' => (
87 is => 'rw',
88 isa => 'HashRef[Str]',
89 init_arg => undef,
90 );
91  
92 =head1 Methods
93  
94  
95  
96 =head2 $foo->get_symbol($description)
97  
98  
99  
100 Takes a security description. Returns either the symbol, if
101 lookup is successful, or undef;
102  
103  
104  
105 =cut
106  
107 50 ▶sub get_symbol{
Subroutine get_symbol: called CC=4 SCAR=13.9
Pod documented
108 50 my $self = shift;
109 50 my $description = shift;
110  
111 50 my $ref = $self->_true_symbol_map();
112  
113 50 my $symbol = $ref->{$description};
114  
115 50 ▶ return $symbol if $symbol;
Branch: 1 branch
BranchTrueFalseTotal
if $symbol 8 42 50
116
117 42 foreach my $key(keys(%$ref)){
118 189 ▶ if( $description =~/$key/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($description =~ /$key/u) 38 151 189
119 38 return $ref->{$key};
120 }
121 }
122  
123 4 return;
124 }
125  
126  
127 =head2 $foo->BUILD()
128  
129 Internal Moose infrastructure, ignore.
130  
131 =cut
132 8 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod documented
133 8 my $self = shift;
134
135 8 my $symmap = $self->symbol_map();
136  
137 8 my %newmap;
138 8 my $fh = openhandle ($symmap);
139  
140 8 ▶ if (not defined $fh){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fh) { } 4 4 8
141 #it's a hash ref of str, clone it to avoid pesky people trying to mess with it later
142 4 foreach my $key(keys(%$symmap)){
143 24 $newmap{$key} = $symmap->{$key};
144 }
145 }else{
146 #it's a file handle, the file must be in the format
147 #description,symbol
148  
149 4 ▶ while(<$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 24 4 28
150 24 chomp;
151 24 my @parts = split /,/;
152  
153 24 $newmap{$parts[0]} = $parts[1];
154 }
155 }
156  
157 #Was going to lock it down, but maybe it's more useful to
158 #leave it unlocked in case someone has a legit use case to
159 #fiddle with it at runtime
160 8 $self->_true_symbol_map(\%newmap);
161 }
162  
163 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 8 __PACKAGE__->meta->make_immutable;
166  
167  
168 =head1 LICENSE AND COPYRIGHT
169 Copyright 2009-2023 David Solimano
170 This file is part of Finance::Bank::SentinelBenefits::Csv401kConverter
171  
172 Finance::Bank::SentinelBenefits::Csv401kConverter is free software: you can redistribute it and/or modify
173 it under the terms of the GNU General Public License as published by
174 the Free Software Foundation, either version 3 of the License, or
175 (at your option) any later version.
176  
177 Finance::Bank::SentinelBenefits::Csv401kConverter is distributed in the hope that it will be useful,
178 but WITHOUT ANY WARRANTY; without even the implied warranty of
179 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
180 GNU General Public License for more details.
181  
182 You should have received a copy of the GNU General Public License
183 along with Finance::Bank::SentinelBenefits::Csv401kConverter. If not, see <http://www.gnu.org/licenses/>.
184  
185 =cut
186 8 1
« lib/Finance/Bank/SentinelBenefits/Csv401kConverter.pm ↑ Summary blib/lib/Finance/Bank/SentinelBenefits/example.pl »