SPOPS/Tie.pm

filter: Statementstmt 61.9% 57 / 92 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 82.6% 19 / 23 Podpod n/a totaltotal 47.8% 76 / 159 CC 18 SCAR 43.5
CC
18
Cov
44%
FETCH
210.0
TIEHASH
30.0
_field_check
2.0
CRAP 77.8
« SPOPS/Iterator/DBI.pm ↑ Summary SPOPS/DBI.pm »
1 package SPOPS::Tie;
2  
3 # $Id: Tie.pm,v 3.8 2004/06/02 00:48:22 lachoy Exp $
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use vars qw( $PREFIX_TEMP $PREFIX_INTERNAL );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use SPOPS::Exception qw( spops_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 @SPOPS::Tie::EXPORT_OK = qw( IDX_DATA IDX_CHANGE IDX_SAVE IDX_INTERNAL IDX_TEMP
13 IDX_CHECK_FIELDS IDX_LAZY_LOADED
14 $PREFIX_TEMP $PREFIX_INTERNAL );
15 $SPOPS::Tie::VERSION = sprintf("%d.%02d", q$Revision: 3.8 $ =~ /(\d+)\.(\d+)/);
16  
17 20 ▶use constant IDX_DATA => '_dat';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use constant IDX_CHANGE => '_chg';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use constant IDX_SAVE => '_svd';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 20 ▶use constant IDX_INTERNAL => '_int';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 ▶use constant IDX_TEMP => '_tmp';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 20 ▶use constant IDX_IS_LAZY_LOAD => '_ill';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 20 ▶use constant IDX_LAZY_LOADED => '_ll';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 20 ▶use constant IDX_LAZY_LOAD_SUB => '_lls';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 20 ▶use constant IDX_CHECK_FIELDS => '_chk';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 20 ▶use constant IDX_IS_MULTIVALUE => '_imv';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 20 ▶use constant IDX_MULTIVALUE => '_mv';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 20 ▶use constant IDX_IS_FIELD_MAP => '_ifm';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 20 ▶use constant IDX_FIELD_MAP => '_fm';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 my $log = get_logger();
32  
33 $PREFIX_TEMP = 'tmp_';
34 $PREFIX_INTERNAL = '_internal';
35  
36 # Tie interface stuff below here; see 'perldoc perltie' for what
37 # each method does. (Or better yet, read Damian Conway's discussion
38 # of tie in 'Object Oriented Perl'.)
39  
40  
41 # First activate the callback for the field check, then return the
42 # object. The object always keeps track of the actual properties, the
43 # class, whether the object's properties have been changed and keeps
44 # any temporary data that lives only for the object's lifetime.
45  
46 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=5 SCAR=34.0
47 0 my ( $class, $base_class, $p ) = @_;
48 0 ▶ $p ||= {};
Condition0%0/2
$p ||= {}
l!l
00
MC/DC0%0/1
$p ||= {}
$p
Truth table0%0/2
A
A= $p
Aresult
11
00
49  
50 # See if we're supposed to do any field checking
51  
52 0 my $HAS_FIELD = $class->_field_check( $base_class, $p );
53  
54 # Be able to deal with either an arrayref or a hashref of multivalue fields
55  
56 0 ▶ if ( ref $p->{multivalue} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p->{"multivalue"} eq "HASH") 0 0 0
57 0 $p->{multivalue} = { map { lc $_ => lc $p->{multivalue}{ $_ } } keys %{ $p->{multivalue} } };
58 }
59  
60 0 ▶ if ( ref $p->{multivalue} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p->{"multivalue"} eq "ARRAY") 0 0 0
61 0 $p->{multivalue} = { map { lc $_ => 1 } @{ $p->{multivalue} } };
62 }
63  
64 # Be sure all field map fields are lower-cased
65 0 ▶ if ( ref $p->{field_map} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p->{"field_map"} eq "HASH") 0 0 0
66 0 $p->{field_map} = { map { lc $_ => lc $p->{field_map}{ $_ } } keys %{ $p->{field_map} } };
67 }
68  
69 return bless ({ class => $base_class,
70 IDX_TEMP() => {},
71 IDX_INTERNAL() => {},
72 IDX_CHANGE() => 0,
73 IDX_SAVE() => 0,
74 IDX_DATA() => {},
75 IDX_IS_LAZY_LOAD() => $p->{is_lazy_load},
76 IDX_LAZY_LOADED() => {},
77 IDX_LAZY_LOAD_SUB()=> $p->{lazy_load_sub},
78 IDX_IS_MULTIVALUE()=> ( ref $p->{multivalue} eq 'HASH' ),
79 IDX_MULTIVALUE() => $p->{multivalue},
80 IDX_IS_FIELD_MAP() => ( ref $p->{field_map} eq 'HASH' ),
81 IDX_FIELD_MAP() => $p->{field_map},
82 0 IDX_CHECK_FIELDS() => $HAS_FIELD }, $class );
83 }
84  
85 0 ▶sub _field_check { return undef; }
Subroutine _field_check: not called CC=1 SCAR=6.9
86  
87 # Just go through each of the possible things that could be
88 # set and do the appropriate action.
89  
90 0 ▶sub FETCH {
Subroutine FETCH: not called CC=14 SCAR=53.5
91 0 my ( $self, $key ) = @_;
92 0 ▶ return unless ( $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
93 0 my $cmp_key = lc $key;
94 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug(" tie: Trying to retrieve value for ($key)") 0 0 0
95 $log->debug( " tie: Trying to retrieve value for ($key)" );
96 0 ▶ return $self->{ IDX_CHANGE() } if ( $key eq IDX_CHANGE );
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_chg" 0 0 0
97 0 ▶ return $self->{ IDX_SAVE() } if ( $key eq IDX_SAVE );
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "_svd" 0 0 0
98 0 ▶ return $self->{ IDX_TEMP() }{ $cmp_key } if ( $key =~ /^$PREFIX_TEMP/ );
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^$SPOPS::Tie::PREFIX_TEMP/ 0 0 0
99 0 ▶ return $self->{ IDX_INTERNAL() }{ $cmp_key } if ( $key =~ /^$PREFIX_INTERNAL/ );
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^$SPOPS::Tie::PREFIX_INTERNAL/ 0 0 0
100 0 ▶ return undef unless ( $self->_can_fetch( $key ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_can_fetch($key) 0 0 0
101 0 ▶ if ( $self->{ IDX_IS_FIELD_MAP() } and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ifm"} and $self->{"_fm"}{$cmp_key}) 0 0 0
Condition0%0/3
$self->{"_ifm"} and $self->{"_fm"}{$cmp_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ifm"} and $self->{"_fm"}{$cmp_key}
$self->{"_ifm"} $self->{"_fm"}{$cmp_key}
Truth table0%0/3
A and B
A= $self->{"_ifm"}B= $self->{"_fm"}{$cmp_key}
ABresult
0X0
100
111
102 $self->{ IDX_FIELD_MAP() }{ $cmp_key } ) {
103 #warn "(FETCH) using field map: old value ($cmp_key) new ($self->{ IDX_FIELD_MAP() }{ $cmp_key })";
104 0 $cmp_key = $self->{ IDX_FIELD_MAP() }{ $cmp_key };
105 }
106 0 ▶ if ( $self->{ IDX_IS_LAZY_LOAD() } and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_ill"} and not $self->{"_ll"}{$cmp_key}) 0 0 0
Condition0%0/3
$self->{"_ill"} and not $self->{"_ll"}{$cmp_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_ill"} and not $self->{"_ll"}{$cmp_key}
$self->{"_ill"} not $self->{"_ll"}{$cmp_key}
Truth table0%0/3
A and not(B)
A= $self->{"_ill"}B= not $self->{"_ll"}{$cmp_key}
ABresult
0X0
100
111
107 ! $self->{ IDX_LAZY_LOADED() }{ $cmp_key } ) {
108 0 $self->_lazy_load( $key );
109 }
110 0 ▶ if ( $self->{ IDX_IS_MULTIVALUE() } and $self->{ IDX_MULTIVALUE() }{ $cmp_key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_imv"} and $self->{"_mv"}{$cmp_key}) 0 0 0
Condition0%0/3
$self->{"_imv"} and $self->{"_mv"}{$cmp_key}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_imv"} and $self->{"_mv"}{$cmp_key}
$self->{"_imv"} $self->{"_mv"}{$cmp_key}
Truth table0%0/3
A and B
A= $self->{"_imv"}B= $self->{"_mv"}{$cmp_key}
ABresult
0X0
100
111
111 #warn "(FETCH) using multivalue for key $cmp_key";
112 0 return [ keys %{ $self->{ IDX_DATA() }{ $cmp_key } } ];
113 }
114 0 return $self->{ IDX_DATA() }{ $cmp_key };
115 }
116  
117  
118 0 ▶sub _can_fetch { return 1 }
Subroutine _can_fetch: not called CC=1 SCAR=6.9
119  
120  
121 sub _lazy_load {
122 my ( $self, $key ) = @_;
123 my $cmp_key = lc $key;
124 unless ( ref $self->{ IDX_LAZY_LOAD_SUB() } eq 'CODE' ) {
125 spops_error "Lazy loading activated but no load function specified!";
126 }
127 $log->is_info &&
128 $log->info( "Lazy loading [$key]; is-loaded marker empty" );
129 $self->{ IDX_DATA() }{ $cmp_key } =
130 $self->{ IDX_LAZY_LOAD_SUB() }->( $self->{class},
131 $self->{ IDX_DATA() },
132 $key );
133 $self->{ IDX_LAZY_LOADED() }{ $cmp_key }++;
134 }
135  
136  
137 # Similar to FETCH
138  
139 sub STORE {
140 my ( $self, $key, $value ) = @_;
141 my $cmp_key = lc $key;
142 $log->is_debug &&
143 $log->debug( " tie: Storing [$key] => [", ( defined $value ) ? $value : 'undef', "]" );
144 return $self->{ IDX_CHANGE() } = $value if ( $key eq IDX_CHANGE );
145 return $self->{ IDX_SAVE() } = $value if ( $key eq IDX_SAVE );
146 return $self->{ IDX_TEMP() }{ $cmp_key } = $value if ( $key =~ /^$PREFIX_TEMP/ );
147 return $self->{ IDX_INTERNAL() }{ $cmp_key } = $value if ( $key =~ /^$PREFIX_INTERNAL/ );
148 return undef unless ( $self->_can_store( $key, $value ) );
149 $self->{ IDX_CHANGE() }++;
150  
151 if ( $self->{ IDX_IS_FIELD_MAP() } and
152 $self->{ IDX_FIELD_MAP() }{ $cmp_key } ) {
153 #warn "(STORE) using field map: old value ($cmp_key) new ($self->{ IDX_FIELD_MAP() }{ $cmp_key })";
154 $cmp_key = $self->{ IDX_FIELD_MAP() }{ $cmp_key };
155 }
156  
157 # Non-multivalue properties just return the newly stored value
158  
159 unless ( $self->{ IDX_IS_MULTIVALUE() } and $self->{ IDX_MULTIVALUE() }{ $cmp_key } ) {
160 $self->{ IDX_IS_LAZY_LOAD() } && $self->{ IDX_LAZY_LOADED() }{ $cmp_key }++;
161 return $self->{ IDX_DATA() }{ $cmp_key } = $value;
162 }
163  
164 #warn "(STORE) using multivalue for key $cmp_key";
165  
166 # If we're using multiple values we need to see what type of
167 # $value we've got
168  
169 # If $value is undef, we clear out all values in the object
170  
171 unless ( defined $value ) {
172 $self->{ IDX_DATA() }{ $cmp_key } = {};
173 return undef;
174 }
175  
176 my $typeof = ref $value;
177  
178 # If a scalar, just set it
179  
180 unless ( $typeof ) {
181 $self->{ IDX_DATA() }{ $cmp_key }{ $value } = 1;
182 return $value;
183 }
184  
185 # If array, set it (if the array is empty, then we're
186 # resetting the values)
187  
188 if ( $typeof eq 'ARRAY' ) {
189 #warn "(STORE) Current value of ($cmp_key)", Dumper( $self->{ IDX_DATA() }{ $cmp_key } ), "";
190 $self->{ IDX_DATA() }{ $cmp_key } = { map { $_ => 1 } @{ $value } };
191 #warn "(STORE) Value after set of ($cmp_key)", Dumper( $self->{ IDX_DATA() }{ $cmp_key } ), "";
192 return undef;
193 }
194  
195 # If hash, go through each of the potential options and
196 # perform the action; everything else is ignored
197  
198 if ( $typeof eq 'HASH' ) {
199 my $remove_fields = ( ref $value->{remove} eq 'ARRAY' ) ? $value->{remove} : [ $value->{remove} ];
200 foreach my $rmv ( @{ $remove_fields } ) {
201 next unless ( $rmv );
202 delete $self->{ IDX_DATA() }{ $cmp_key }{ $rmv };
203 }
204  
205 my $modify_fields = $value->{modify} || {};
206 foreach my $mdfy ( keys %{ $modify_fields } ) {
207 delete $self->{ IDX_DATA() }{ $cmp_key }{ $mdfy };
208 $self->{ IDX_DATA() }{ $cmp_key }{ $modify_fields->{ $mdfy } } = 1
209 }
210 return undef;
211 }
212  
213 # We don't know how to handle anything else
214  
215 spops_error "Cannot handle a value type of [$typeof] with multivalues";
216 }
217  
218 sub _can_store { return 1 }
219  
220  
221 # For EXISTS and DELETE, We can only do these actions on the actual
222 # data; use the object methods for the other information.
223  
224 sub EXISTS {
225 my ( $self, $key ) = @_;
226 $log->is_debug &&
227 $log->debug( " tie: Checking for existence of ($key)" );
228 return exists $self->{ IDX_DATA() }{ lc $key };
229 $log->error( "Field '$key' is not valid, cannot check existence" );
230 }
231  
232  
233 sub DELETE {
234 my ( $self, $key ) = @_;
235 $log->is_debug &&
236 $log->debug( " tie: Clearing value for ($key)" );
237 delete $self->{ IDX_DATA() }{ lc $key };
238 $self->{ IDX_CHANGE() }++;
239 }
240  
241  
242 # We've disabled the ability to do: $object = {} or %{ $object } = ();
243 # nothing bad happens, it's just a no-op
244  
245 sub CLEAR {
246 my ( $self ) = @_;
247 $log->error( "Trying to clear object through hash means failed; use object interface" );
248 }
249  
250  
251 # Note that you only see the data when you cycle through the keys
252 # or even do a Data::Dumper::Dumper( $object ); you do not see
253 # the meta-data being tracked. This is a feature.
254  
255 sub FIRSTKEY {
256 my ( $self ) = @_;
257 $log->is_debug &&
258 $log->debug( " tie: Finding first key in data object" );
259 keys %{ $self->{ IDX_DATA() } };
260 my $first_key = each %{ $self->{ IDX_DATA() } };
261 return undef unless defined $first_key;
262 return $first_key;
263 }
264  
265  
266 sub NEXTKEY {
267 my ( $self ) = @_;
268 $log->is_debug &&
269 $log->debug( " tie: Finding next key in data object" );
270 my $next_key = each %{ $self->{ IDX_DATA() } };
271 return undef unless defined $next_key;
272 return $next_key;
273 }
274  
275 1;
276  
277 __END__
« SPOPS/Iterator/DBI.pm ↑ Summary SPOPS/DBI.pm »