« blib/lib/Finance/Bank/SentinelBenefits/Csv401kConverter.pm
↑ Summary
blib/lib/Finance/Bank/SentinelBenefits/Csv401kConverter/SideReverser.pm »
| 1 | package Finance::Bank::SentinelBenefits::Csv401kConverter::SymbolMap; | ||
| 2 | $Finance::Bank::SentinelBenefits::Csv401kConverter::SymbolMap::VERSION = '1.2'; | ||
| 3 | use Modern::Perl; | ||
| 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.2 | ||
| 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 | use Moose; | ||
| 31 | use Scalar::Util qw{ openhandle }; | ||
| 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 | has 'symbol_map' => ( | ||
| 79 | is => 'ro', | ||
| 80 | isa => 'HashRef[Str] | FileHandle', | ||
| 81 | required => 1, | ||
| 82 | ); | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | 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 | sub get_symbol{ | ||
| 108 | my $self = shift; | ||
| 109 | my $description = shift; | ||
| 110 | |||
| 111 | my $ref = $self->_true_symbol_map(); | ||
| 112 | |||
| 113 | my $symbol = $ref->{$description}; | ||
| 114 | |||
| 115 | return $symbol if $symbol; | ||
| 116 | |||
| 117 | foreach my $key(keys(%$ref)){ | ||
| 118 | if( $description =~/$key/ ){ | ||
| 119 | return $ref->{$key}; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | =head2 $foo->BUILD() | ||
| 128 | |||
| 129 | Internal Moose infrastructure, ignore. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | sub BUILD { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my $symmap = $self->symbol_map(); | ||
| 136 | |||
| 137 | my %newmap; | ||
| 138 | my $fh = openhandle ($symmap); | ||
| 139 | |||
| 140 | if (not defined $fh){ | ||
| 141 | #it's a hash ref of str, clone it to avoid pesky people trying to mess with it later | ||
| 142 | foreach my $key(keys(%$symmap)){ | ||
| 143 | $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 | while(<$fh>){ | ||
| 150 | chomp; | ||
| 151 | my @parts = split /,/; | ||
| 152 | |||
| 153 | $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 | $self->_true_symbol_map(\%newmap); | ||
| 161 | } | ||
| 162 | |||
| 163 | no Moose; | ||
| 164 | |||
| 165 | __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 | 1 |