Fixed typo affecting JS-detection on the production site.
[MyWeb.git] / Hash / RestrictTo.pm
1 # $Id$
2 # Hash tied to restrict access to the specified keys list.
3 # Copyright (C) 2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; exactly version 2 of June 1991 is required
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
19 package My::Hash::RestrictTo;
20 our $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
21 our $CVS_ID=q$Id$;
22 use strict;
23 use warnings;
24 use My::Web;    # for &Wrequire
25 Wrequire 'My::Hash';
26 our @ISA=qw(My::Hash);
27 use Carp qw(cluck confess);
28
29
30 sub TIEHASH($$@)
31 {
32 my($class,$parent,@keys)=@_;
33
34         my $self=$class->SUPER::TIEHASH($parent);
35         $self->{"keyshash"}={ map(($_=>1),@keys) };
36         return $self;
37 }
38
39 sub STORE($$$)
40 {
41 my($this,$key,$value)=@_;
42
43         confess "Key not permitted: $key" if !$this->{"keyshash"}{$key};
44         return $this->pass("STORE",$key,$value);
45 }
46
47 sub FETCH($$)
48 {
49 my($this,$key)=@_;
50
51         confess "Key not permitted: $key" if !$this->{"keyshash"}{$key};
52         return $this->pass("FETCH",$key);
53 }
54
55 sub FIRSTKEY($)
56 {
57 my($this)=@_;
58
59         my $a=keys(%{$this->{"keyshash"}});
60         return $this->NEXTKEY($this,undef());
61 }
62
63 sub NEXTKEY($$)
64 {
65 my($this,$lastkey)=@_;
66
67 cluck "TODO: Enumeration may not be expected.";
68         for (;;) {
69                 my $key=each(%{$this->{"keyshash"}});
70                 return if !defined $key;
71                 return $key if exists $this->{"parent"}{$key};
72                 }
73 }
74
75 sub EXISTS($$)
76 {
77 my($this,$key)=@_;
78
79         confess "Key not permitted: $key" if !$this->{"keyshash"}{$key};
80         return $this->pass("EXISTS",$key);
81 }
82
83 sub DELETE($$)
84 {
85 my($this,$key)=@_;
86
87         confess "Key not permitted: $key" if !$this->{"keyshash"}{$key};
88         return $this->pass("DELETE",$key);
89 }
90
91 sub CLEAR($)
92 {
93 my($this)=@_;
94
95 cluck "TODO: Enumeration may not be expected.";
96         delete $this->{"parent"}{$_} for keys(%{$this->{"keyshash"}});
97 }
98
99 sub SCALAR
100 {
101 my($this)=@_;
102
103 confess "TODO: Not yet implemented.";
104 }
105
106 1;