WWW/Scraper/FieldTranslation.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 174 CC 21 SCAR 36.9
CC
21
Cov
0%
_setupTranslation
90.0
postSelect
42.0
translate
20.0
CRAP 40.2
1 use strict;
2 package WWW::Scraper::FieldTranslation;
3 use vars qw(@ISA $VERSION);
4 $VERSION = sprintf("%d.%02d", q$Revision: 1.0 $ =~ /(\d+)\.(\d+)/);
5 use Tie::Persistent;
6 use Storable;
7  
8 sub new {
9 my ($self, $engineName, $rqstName, $fieldName, $newFieldName) = @_;
10 $self = bless {}, $self;
11
12 $self->{'_state'} = 0; # Current state of FieldTranslation object -
13 # 0 - no initialization done
14 # 1 - persistent hash is opened to read
15 # 2 - persistent hash is opened to read/write
16 $self->{'_engineName'} = $engineName;
17 $self->{'_requestName'} = $rqstName;
18 $self->{'_fieldName'} = $fieldName;
19 $self->{'_newFieldName'}= $newFieldName;
20  
21 $self->{'_translationSearchPath'} = \@INC;
22 $self->{'_translationTie'} = undef;
23
24 return $self;
25 }
26  
27  
28  
29  
30 ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
31 # Translate from the canonical Request->locations to the Engine's native option,
32 # using values from the persistent tied file "<engineName>.<requestType>.locations";
33 #
34 # The values in %LocationsTie might be an array of values, and of course
35 # the same value could be in multiple elements, so we gather all the
36 # translations up in a hash to insure each value appears just once in
37 # the result.
38 sub translate {
39 my ($self, $scraper, $rqst, $val) = @_;
40 my (%results, @postSelect);
41 my ($rsltRtn, $postSelectRtn);
42  
43 my $LT = $self->_setupTranslation(1);
44  
45 my $aval = $val;
46 $aval = [$val] unless 'ARRAY' eq ref $val;
47 my ($va, $v);
48  
49 for ( @$aval ) {
50 my $prepost = $$LT{$_};
51 for ( keys %$prepost ) {
52 $v = $$prepost{$_};
53 # The 'pre' array of the Field Translation table.
54 if ( m/^pre/ ) {
55 %results = %$v;
56 $rsltRtn = \%results;
57 }
58 # The 'post' array of the Field Translation table.
59 elsif ( m/^post/ ) {
60 push @postSelect, $v;
61 $postSelectRtn = \@postSelect
62 }
63 }
64 }
65
66 $self->{'_postSelect'}{$_} = $postSelectRtn;
67 return $rsltRtn;
68 }
69  
70  
71  
72 sub postSelect {
73 my ($self, $scraper, $rqst, $rslt) = @_;
74
75 my $val;
76 for ( keys %{$self->{'_postSelect'}} ) {
77 my $pstSlct = ${$self->{'_postSelect'}}{$_};
78 if ( $pstSlct ) {
79 my $match = 0;
80 MATCH: for my $chkIt ( @$pstSlct ) {
81 for my $fldnam ( keys %$chkIt ) {
82 $val = $rslt->_elem($fldnam);
83 my $qrs = $$chkIt{$fldnam};
84 if ( ref($qrs) eq 'ARRAY' ) {
85 for ( @$qrs ) {
86 if ( $val =~ m/$_/ ) {
87 $match = 1;
88 last MATCH;
89 }
90 }
91 }
92 else {
93 if ( $val =~ m/$qrs/ ) {
94 $match = 1;
95 last MATCH;
96 }
97 }
98 }
99 }
100 # If no match, then record the disqualifying field for posterity, and return 0.
101 unless ( $match ) {
102 $self->{'_unTranslationTie'}{$val} += 1;
103 return 0;
104 }
105 }
106 }
107 return 1;
108 }
109  
110  
111  
112  
113 ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
114 sub setTranslation {
115 my ($self, $key, $val) = @_;
116 my $LT = $self->_setupTranslation(2);
117 my $orgVal = $$LT{$key};
118 $$LT{$key} = $val if defined $val;
119 return $orgVal;
120 }
121 ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
122  
123  
124  
125 ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
126 # $typ == 0, readable; $typ == 1, read/writable.
127 sub _setupTranslation {
128 my ($self, $typ) = @_;
129  
130 return $self->{'_translationTie'} if ($typ == $self->{'_state'});
131
132 my $TranslationsSearchPath = $self->{'_translationSearchPath'};
133 my $prefix = ref $self;
134 $prefix =~ s/FieldTranslation.*$//;
135 $prefix =~ s-::-/-g;
136 my $engineName = $prefix.$self->{'_engineName'};
137 $engineName .= '.' if $engineName;
138 my $rqstName = $self->{'_requestName'};
139 $rqstName .= '.' if $rqstName;
140 my $fieldName = $self->{'_fieldName'};
141
142 my $tieFile;
143 for ( @$TranslationsSearchPath ) {
144 $tieFile = "$_/$engineName$rqstName$fieldName";
145 last if -f $tieFile;
146 };
147 # If couldn't find the transformations definition file, then
148 # create one in the same folder that <ScraperModule>.pm is in.
149 unless ( -f $tieFile ) {
150 for ( @$TranslationsSearchPath ) {
151 if ( -f "$_/$engineName"."pm" ) {
152 $tieFile = "$_/$engineName$rqstName$fieldName";
153 last;
154 }
155 }
156 }
157 my %TranslationTie;
158 tie (%TranslationTie, 'Tie::Persistent', $tieFile, ($typ == 2) ? 'rw' : 'r') or
159 die "Can't tie $engineName$rqstName$fieldName: $!";
160 my %UnTranslationTie;
161 tie (%UnTranslationTie, 'Tie::Persistent', $tieFile.'.mismatch', 'rw') or
162 die "Can't tie $engineName$rqstName$fieldName.mismatch: $!";
163  
164 $self->{'_state'} = $typ;
165 $self->{'_translationTie'} = \%TranslationTie;
166 $self->{'_unTranslationTie'} = \%UnTranslationTie;
167 return ($self->{'_translationTie'}, $self->{'_unTranslationTie'}) if wantarray;
168 return $self->{'_translationTie'};
169 }
170  
171  
172  
173 # A generalize get/set method for object attributes.
174 sub _attr {
175 my ($self, $attr, $value) = @_;
176 my $rtn = $self->{$attr};
177 $self->{$attr} = $value if defined $value;
178 if ( wantarray ) {
179 return $rtn if 'ARRAY' eq ref $rtn;
180 return [$rtn];
181 }
182 return $rtn;
183 }
184 sub debug { $_[0]->_attr('_debug', $_[1]) }
185  
186 1;
187  
188 __END__