Support "_push", "_pop", "_set" keys.
[MyWeb.git] / Hash / Push.pm
1 # $Id$
2 # Hash tied to optionally dynamically generate its values
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::Push;
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 require Data::Compare;
29
30
31 # You are recommended to STORE all the keys, not to parent-inherit them.
32
33
34 sub _normalize($$)
35 {
36 my($self,$val)=@_;
37
38         $val=[] if !defined $val;
39         $val=[$val] if "ARRAY" ne ref($val);
40         return $val;
41 }
42
43 sub _ours($$)
44 {
45 my($self,$key,$state)=@_;
46
47         my $ref=\$self->{"ours"}{$key};
48         warn "State change for key \"$key\"; was=".(0+$$ref).",now=".(0+$state)
49                         if defined($state) && defined($$ref) && $state!=$$ref;
50         $$ref=$state if defined $state;
51         return $$ref;
52 }
53
54 sub STORE
55 {
56 my($this,$key,$value)=@_;
57
58         if ($key=~s/_push$//) {
59                 $this->_ours($key,1);
60                 return $this->pass("STORE",$key,[@{$this->_normalize($this->pass("FETCH",$key))},$value]);
61                 }
62         if ($key=~s/_pop$//) {
63                 $this->_ours($key,1);
64                 my $val_orig=$this->FETCH($key);
65                 for my $vali (reverse 0..$#$val_orig) {
66                         next if Data::Compare::Compare($val_orig->[$vali],$value);
67                         splice @$val_orig,$vali,1;
68                         return $this->pass("STORE",$key,$val_orig);
69                         }
70                 cluck "value[1] not found in the array[2] of key[0]:\n".Dumper($key,$value,$val_orig);
71                 return;
72                 }
73         if ($key=~s/_set$//) {
74                 $this->_ours($key,1);
75                 return $this->pass("STORE",$key,$this->_normalize($value));
76                 }
77         $this->_ours($key,0);
78         return $this->pass("STORE",$key,$value);
79 }
80
81 sub FETCH($$)
82 {
83 my($this,$key_orig)=@_;
84
85         my $force=((my $key=$key_orig)=~s/_push$//);
86         $this->_ours($key,1) if $force;
87         my $val=$this->pass("FETCH",$key);
88         return $this->_normalize($val) if $force || $this->_ours($key);
89         return $val;
90 }
91
92 1;