IO/Mask.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 144 CC 3 SCAR 11.5
CC
3
Cov
0%
new
6.0
_cache_table
6.0
pick_list
2.0
CRAP 3.1
1 #
2 # $Id: Mask.pm,v 1.2 2002/01/24 02:13:05 rsandberg Exp $
3 #
4  
5 package DBIx::IO::Mask;
6  
7 use strict;
8 use DBIx::IO::Search;
9  
10  
11 my $DEF_MASKED_COL_NAME = 'USER_MASK';
12  
13 my $cached_by_name;
14 my $cached_by_id;
15 my $sorted_masks;
16  
17  
18 =head1 NAME
19  
20 DBIx::IO::Mask - Help make id values more meaningful to humans
21  
22  
23 =head1 SYNOPSIS
24  
25 use DBIx::IO::Mask;
26  
27
28 $masker = new DBIx::IO::Mask($dbh,$field_name);
29 $masker = new DBIx::IO::Mask($dbh,undef,$table_name,$masked_col_name,$id_col_name,$no_cache);
30  
31 $valid_values = $masker->pick_list();
32  
33 $masked_value = $masker->mask($id_val);
34  
35 $id = $masker->unmask($masked_value);
36  
37 $ids_to_mask_hash = $masker->ids_to_mask();
38  
39  
40  
41 =head1 DESCRIPTION
42  
43 For applications that interface a human to a database, e.g. CGI, this class makes
44 database numeric ID values more meaningful to humans. It can also present a sorted list of valid
45 values to use in pop-up lists, where only certain values are allowed in the database.
46 It does this in conjunction with DBIx::IO::Search.
47  
48 The general strategy is that any column you want masked or that has a discrete set of values, should have
49 a corresponding table with those set of values. The table name should be the same as the ID column name except
50 with no _ID at the end. The table should have at least 2 columns in it:
51  
52 <COLUMN_NAME> - The name of the column you want masked,
53 it stores the ID values, and does not necessarily have
54 to be a numeric datatype.
55 USER_MASK - This column stores values that describe the id values
56  
57 This way a new $masker object can be created by just knowing the name of the column you want masked. Example:
58  
59 To get a sorted list of valid values for a column named STATE, create a table named STATE with
60 2 columns, STATE and USER_MASK with all states represented therein. Then:
61  
62 $masker = new DBIx::IO::Mask($dbh,'STATE');
63 $states = $masker->pick_list();
64  
65 This is the easy way, however if you have a table with a set of IDs and descriptions that doesn't conform to the guidelines above (or you just think the
66 whole scenario is dumb), you can specify the table name and desired column names in the constructor.
67  
68 All tables read are cached in class variables for efficiency. This may be significant
69 if you're using persistent objects/classes with such environments as mod_perl.
70  
71  
72 Happy Halloween
73  
74  
75 =head1 METHOD DETAILS
76  
77 =over 4
78  
79  
80 =item C<new (constructor)>
81  
82 $masker = new DBIx::IO::Mask($dbh,$field_name);
83 $masker = new DBIx::IO::Mask($dbh,undef,$table_name,$masked_col_name,$id_col_name,$no_cache);
84  
85 Create a new $masker object for all your masking pleasures with a
86 db handle, $dbh from DBI (or DBIAccess). The rest of the arguments are optional:
87 $table_name contains all the valid values; it defaults to $field_name minus '_ID', which may or may not be present.
88 $id_col_name is the name of the column that contains the id values in $table_name that are masked;
89 it defaults to $field_name.
90 $masked_col_name is the column in $table_name that has the meaningful values
91 the ids refer to; it defaults to 'USER_MASK'.
92 If $no_cache is true, then the cache will be refreshed (data will be pulled from the db, not the cache).
93  
94 Return 0 if $table_name doesn't exist, or no values to mask were found.
95 Return undef if error.
96  
97 =cut
98 sub new
99 {
100 my ($caller,$dbh,$field_name,$table_name,$masked_col_name,$id_col_name,$no_cache) = @_;
101 ref($dbh) || (warn("\$dbh doesn't appear to be valid"), return undef);
102 my $class = ref($caller) || $caller;
103 my $orig_table_name = $table_name || _strip_id($field_name);
104 $field_name = uc($field_name);
105 $table_name = uc($table_name) || _strip_id($field_name);
106 $orig_table_name ||= $table_name;
107 $masked_col_name = uc($masked_col_name) || $DEF_MASKED_COL_NAME;
108 $id_col_name = uc($id_col_name) || $field_name;
109 unless (exists($cached_by_name->{$table_name}{$masked_col_name}{$id_col_name}) && !$no_cache)
110 {
111 my $ret = $caller->_cache_table($dbh,$table_name,$masked_col_name,$id_col_name,$orig_table_name);
112 $ret || return $ret;
113 }
114 my $self = {
115 dbh => $dbh,
116 ids_by_mask => $cached_by_name->{$table_name}{$masked_col_name}{$id_col_name},
117 masks_by_id => $cached_by_id->{$table_name}{$masked_col_name}{$id_col_name},
118 sorted_mask_list => $sorted_masks->{$table_name}{$masked_col_name}{$id_col_name},
119 };
120 return bless($self,$class);
121 }
122  
123 =pod
124  
125 =item C<pick_list>
126  
127 $valid_values = $masker->pick_list();
128  
129 Return a machine sorted list of masks and ids where each element
130 is a hash with keys:
131  
132 ID => the id value
133 MASK => a meaningful indicator of what ID refers to
134  
135  
136 =cut
137 sub pick_list
138 {
139 my $self = shift;
140 return [ @{$self->{sorted_mask_list}} ];
141 }
142  
143 #=pod
144 #
145 #=item C<ids_by_mask>
146 #
147 # $masked_values = $masker->ids_by_mask();
148 #
149 #Return a hash of masked values where the keys are all the masked
150 #values and the values of the hash are the ids. This hash only makes
151 #sense where the table has a unique constraint on the masked values.
152 #If you're looking for a sorted list of all mask, id value pairs, use
153 #pick_list().
154 #
155 #=cut
156 sub ids_by_mask
157 {
158 my ($self) = @_;
159 return { %{$self->{ids_by_mask}} };
160 }
161  
162 =pod
163  
164 =item C<ids_to_mask>
165  
166 $ids_to_mask_hash = $masker->ids_to_mask();
167  
168 Return a hash ref of id => mask_value pairs where each key is
169 an id value and each value is meaningful to a human.
170  
171 =cut
172 sub ids_to_mask
173 {
174 my $self = shift;
175 return { %{$self->{masks_by_id}} };
176 }
177  
178 sub _strip_id
179 {
180 my $id = shift;
181 $id =~ s/_ID$//i;
182 return $id;
183 }
184  
185 sub _mask_search
186 {
187 my ($self,$dbh,$masked_col_name,$orig_table_name) = @_;
188 return new DBIx::IO::Search($dbh,$orig_table_name,undef,[ $masked_col_name ]);
189 }
190  
191 sub _cache_table
192 {
193 my ($self,$dbh,$table_name,$masked_col_name,$id_col_name,$orig_table_name) = @_;
194 my $mask_search;
195 unless ($mask_search = $self->_mask_search($dbh,$masked_col_name,$orig_table_name))
196 {
197 return $mask_search;
198 }
199 my $results = $mask_search->search() || return undef;
200 my %cached_by_name;
201 my %cached_by_id;
202 my @mask_list;
203 foreach my $result (@$results)
204 {
205 exists($cached_by_id{$result->{$id_col_name}}) && next;
206 $cached_by_name{$result->{$masked_col_name}} = $result->{$id_col_name};
207 $cached_by_id{$result->{$id_col_name}} = $result->{$masked_col_name};
208 push @mask_list, { ID => $result->{$id_col_name}, MASK => $result->{$masked_col_name} };
209 }
210 $cached_by_name->{$table_name}{$masked_col_name}{$id_col_name} = \%cached_by_name;
211 $cached_by_id->{$table_name}{$masked_col_name}{$id_col_name} = \%cached_by_id;
212 ##at for some reason the degugger shows that the $sorted_masks address is not being reused between pick_list() invocations, $cached_by_id is, however
213 $sorted_masks->{$table_name}{$masked_col_name}{$id_col_name} = \@mask_list;
214 return 1;
215 }
216  
217 sub _sort_format
218 {
219 my $ids_by_mask = shift;
220 my @sorted_hash;
221 foreach my $mask (sort(keys(%$ids_by_mask)))
222 {
223 push @sorted_hash, { ID => $ids_by_mask->{$mask}, MASK => $mask };
224 }
225 return \@sorted_hash;
226 }
227  
228 =pod
229  
230 =item C<mask>
231  
232 $masked_value = $masker->mask($id_val);
233  
234 Return the $masked_value of $id_val.
235  
236 =cut
237 sub mask
238 {
239 my ($self,$id_val) = @_;
240 return $self->{masks_by_id}{$id_val};
241 #If there is no mask, return $id_val.
242 # my $ret = $self->{masks_by_id}{$id_val};
243 # return (length($ret) ? $ret : $id_val);
244 }
245  
246 =pod
247  
248 =item C<unmask>
249  
250 $id = $masker->unmask($masked_value);
251  
252 Return the $id of a $masked_value.
253 CAUTION: Use ONLY if the masked value column has a unique constraint on it.
254  
255 =back
256  
257 =cut
258 sub unmask
259 {
260 my ($self,$masked_val) = @_;
261 return $self->{ids_by_mask}{$masked_val};
262 #If there is no id, return $masked_value.
263 # my $ret = $self->{ids_by_mask}{$masked_val};
264 # return (length($ret) ? $ret : $masked_val);
265 }
266  
267  
268 1;
269  
270 __END__