File Coverage

blib/lib/SPOPS/Tie/StrictField.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package SPOPS::Tie::StrictField;
2              
3             # $Id: StrictField.pm,v 3.5 2004/06/02 00:48:24 lachoy Exp $
4              
5 1     1   875 use strict;
  1         1  
  1         48  
6 1     1   6 use base qw( SPOPS::Tie );
  1         1  
  1         648  
7             use Log::Log4perl qw( get_logger );
8             use SPOPS::Tie qw( IDX_DATA IDX_CHANGE IDX_INTERNAL IDX_TEMP
9             IDX_CHECK_FIELDS $PREFIX_TEMP $PREFIX_INTERNAL );
10              
11             $SPOPS::Tie::StrictField::VERSION = sprintf("%d.%02d", q$Revision: 3.5 $ =~ /(\d+)\.(\d+)/);
12              
13             my $log = get_logger();
14              
15             # Use this for setting up field lists to check
16              
17             my %FIELDS = ();
18              
19              
20             # Called by parent -- return a true to indicate fields ARE being
21             # checked
22              
23             sub _field_check {
24             my ( $class, $base_class, $p ) = @_;
25             if ( $base_class and ref $p->{field} eq 'ARRAY' ) {
26             unless ( ref $FIELDS{ $base_class } eq 'HASH' ) {
27             foreach my $key ( @{ $p->{field} } ) {
28             $FIELDS{ $base_class }->{ lc $key } = 1;
29             }
30             }
31             return 1;
32             }
33             return 0;
34             }
35              
36              
37             # Return true if we can fetch (is a valid field), false if not
38              
39             sub _can_fetch {
40             my ( $self, $key ) = @_;
41             $self->_storable_field_check unless ( $FIELDS{ $self->{class} } );
42             return 1 unless ( $self->{ IDX_CHECK_FIELDS() } );
43             return 1 if ( $FIELDS{ $self->{class} }->{ lc $key } );
44             my ( $call_package, $call_line ) = (caller(1))[0,2];
45             $log->error( "[$call_package @ $call_line]: Field '$key' is ",
46             "not valid, cannot retrieve value" );
47             return undef;
48             }
49              
50              
51             # Return true if we can store (is a valid field), false if not
52              
53             sub _can_store {
54             my ( $self, $key, $value ) = @_;
55             $self->_storable_field_check unless ( $FIELDS{ $self->{class} } );
56             return 1 unless ( $self->{ IDX_CHECK_FIELDS() } );
57             return 1 if ( $FIELDS{ $self->{class} }->{ lc $key } );
58             my ( $call_package, $call_line ) = (caller(1))[0,2];
59             $log->error( "[$call_package @ $call_line]: Field '$key' is ",
60             "not valid, cannot set value" );
61             return undef;
62             }
63              
64             sub _storable_field_check {
65             my ( $self ) = @_;
66             my $object_class = $self->{class};
67             return if ( $FIELDS{ $object_class } );
68             my $fields = $object_class->field;
69             $FIELDS{ $object_class } = { %{ $fields } };
70             }
71              
72             # For EXISTS and DELETE, We can only do these actions on the actual
73             # data; use the object methods for the other information.
74              
75             sub EXISTS {
76             my ( $self, $key ) = @_;
77             return $self->SUPER::EXISTS( $key ) unless ( $self->{ IDX_CHECK_FIELDS() } );
78             $log->is_debug &&
79             $log->debug( " tie: Checking for existence of ($key)\n" );
80             if ( $FIELDS{ $self->{class} }->{ lc $key } ) {
81             return exists $self->{ IDX_DATA() }->{ lc $key };
82             }
83             else {
84             my ( $call_package, $call_line ) = (caller(1))[0,2];
85             $log->error( "[$call_package @ $call_line]: Field '$key' is ",
86             "not valid, cannot check existence" );
87             return undef;
88             }
89             }
90              
91              
92             sub DELETE {
93             my ( $self, $key ) = @_;
94             return $self->SUPER::DELETE( $key ) unless ( $self->{ IDX_CHECK_FIELDS() } );
95             $log->is_debug &&
96             $log->debug( " tie: Clearing value for ($key)\n" );
97             if ( $FIELDS{ $self->{class} }->{ lc $key } ) {
98             $self->{ IDX_DATA() }->{ lc $key } = undef;
99             $self->{ IDX_CHANGE() }++;
100             }
101             else {
102             my ( $call_package, $call_line ) = (caller(1))[0,2];
103             $log->error( "[$call_package @ $call_line]: Field '$key' is ",
104             "not valid, cannot remove data" );
105             }
106             }
107              
108             1;
109              
110             __END__