/[cvs]/nfo/perl/libs/Data/Mungle/Transform/OO.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Mungle/Transform/OO.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Sun Dec 1 04:44:42 2002 UTC (21 years, 7 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +5 -2 lines
FILE REMOVED
+ moved code to Data::Transform::Deep

1 ##############################################
2 #
3 # $Id: OO.pm,v 1.1 2002/11/29 04:49:20 joko Exp $
4 #
5 # $Log: OO.pm,v $
6 # Revision 1.1 2002/11/29 04:49:20 joko
7 # + initial check-in
8 #
9 # Revision 1.1 2002/10/10 03:26:00 cvsjoko
10 # + new
11 #
12 ##############################################
13
14
15 package Data::Transform::OO;
16
17 use strict;
18 use warnings;
19
20 require Exporter;
21 our @ISA = qw( Exporter );
22 our @EXPORT_OK = qw( &object2hash &hash2object );
23
24 use Data::Dumper;
25 use Data::Transform::Deep qw( getStructSlotByStringyAddress );
26
27 sub object2hash {
28 my $object = shift;
29 my $options = shift;
30 my $deref = var_deref($object);
31 var2utf8($deref) if ($options->{utf8});
32 return $deref;
33 }
34
35 # todo: maybe do diff(s) here sometimes!?
36 sub hash2object {
37 my $object = shift;
38 my $data = shift;
39 my $options = shift;
40
41 # "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables,
42 # this method corrects this
43 var_NumericalHashToArray($data) if ($options->{php});
44
45 # utf8-conversion/-encoding (essential for I18N)
46 var_utf2iso($data) if ($options->{utf8});
47
48 # get fresh object from database
49 # todo:
50 # - handle "locked" objects !!!
51 # - check if already another update occoured (object-revisioning needed!)
52 #my $obj = $self->getObject($oid);
53
54 # mix changes into fresh object and save it back
55 hash2object_traverse_mixin($object, $data);
56
57 # done in core mixin function?
58 #$self->{storage}->update($obj);
59
60 # we should "undef" some objects here to destroy them in memory and enforce reloading when used next time
61 # simulate:
62 #$self->{storage}->disconnect();
63
64 }
65
66 # ------------------------------------
67 # core mixin function
68 # TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data
69
70 # remember keys of structures we are traversing
71 # this is a HACK:
72 # - don't (!!!) "packageglobal" @indexstack
73 # - it will lead to problems with parallelism!
74 my @indexstack;
75
76 # traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object
77 sub hash2object_traverse_mixin {
78 my $object = shift;
79 my $data = shift;
80 my $bool_recursion = shift;
81
82 # clear our key - stack if we are called from user-code (non-recursively)
83 @indexstack = () if (!$bool_recursion);
84
85 my $classname = ref $object;
86 # if ($classname) {
87 # print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n";
88 # }
89
90 # parser: detected OBJECT (a Tangram one?) (reftype == HASH) (assume)
91 # what's exactly done here? hmmm.... it works ;)
92 # maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!!
93 # extend! check!
94 if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) {
95
96 # print STDERR "===", "reftype: ", attributes::reftype($obj), "\n";
97
98 # loop through fields of object (Tangram-object)
99 foreach (keys %{$object}) {
100 push @indexstack, $_;
101
102 # determine type of object
103 my $ref = ref $object->{$_};
104 # print STDERR "attrname: $_ ATTRref: $ref", "\n";
105 if ($ref) {
106 hash2object_traverse_mixin($object->{$_}, $data, 1);
107 } else {
108 my $val = getStructSlotByStringyAddress($data, \@indexstack);
109 $object->{$_} = $val;
110 }
111 pop @indexstack;
112 }
113
114 # save object to database ...
115 # ... do an update if it already exists, do an insert if it doesn't
116 # my $objectId = $self->{storage}->id($obj);
117 # $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" );
118 # if ($objectId) {
119 # $self->{storage}->update($obj);
120
121 #print __PACKAGE__ . ":", "\n";
122 #print Dumper($object);
123
124 # } else {
125 # $self->{storage}->insert($obj);
126 # }
127
128 }
129
130 #&& ( ref($obj) ne 'HASH' )
131
132 # loop through entries of array (IntrArray, isn't it?)
133 if ((ref $object) eq 'ARRAY') {
134 # print STDERR "===", "refttype ", attributes::reftype($obj), "\n";
135 my $i = 0;
136 foreach (@{$object}) {
137 push @indexstack, $i;
138 my $ref = ref $_;
139 # print STDERR "attrname: $_ ATTRref: $ref", "\n";
140 if ($ref && $_) {
141 hash2object_traverse_mixin($_, $data, 1);
142 } else {
143 $object->[$i] = $_;
144 }
145 pop @indexstack;
146 $i++;
147 }
148 }
149
150 }
151
152 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed