diff Lib/IMPL/DOM/Transform/ObjectToDOM.pm @ 250:129e48bb5afb

DOM refactoring ObjectToDOM methods are virtual QueryToDOM uses inflators Fixed transform for the complex values in the ObjectToDOM QueryToDOM doesn't allow to use complex values (HASHes) as values for nodes (overpost problem)
author sergey
date Wed, 07 Nov 2012 04:17:53 +0400
parents 2746a8e5a6c4
children 299af584c05f
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Transform/ObjectToDOM.pm	Tue Nov 06 20:00:57 2012 +0400
+++ b/Lib/IMPL/DOM/Transform/ObjectToDOM.pm	Wed Nov 07 04:17:53 2012 +0400
@@ -12,9 +12,9 @@
     },
     base => [
         'IMPL::Transform' => sub {
-            -plain => \&TransformPlain,
-            HASH => \&TransformHash,
-            -default => \&TransformDefault
+            -plain => 'TransformPlain',
+            HASH => 'TransformHash',
+            -default => 'TransformDefault'
         }
     ],
     props => [
@@ -25,7 +25,8 @@
 };
 
 use constant {
-    SchemaNode => 'IMPL::DOM::Schema::Node'
+    SchemaNode => 'IMPL::DOM::Schema::Node',
+    ComplexNode => 'IMPL::DOM::Schema::ComplexNode'
 };
 
 sub CTOR {
@@ -53,15 +54,26 @@
 sub TransformPlain {
     my ($this,$data) = @_;
     
-    $this->_navi->Current->nodeValue( $this->_navi->inflateValue($data) );
+    $this->_navi->Current->nodeValue( $data );
     return $this->_navi->Current;
 }
 
+sub currentNode {
+    shift->_navi->Current;
+}
+
+sub inflateNodeValue {
+    shift->_navi->inflateValue(shift);
+}
+
 sub TransformHash {
     my ($this,$data) = @_;
     
     die ArgumentException->new(data => 'A HASH reference is required')
         unless ref $data eq 'HASH';
+        
+    return $this->StoreObject($this->currentNode,$data)
+        if !$this->currentNode->schema->isa(ComplexNode);
 
     KEYLOOP: foreach my $key (keys %$data) {
         my $value = $data->{$key};
@@ -69,25 +81,36 @@
         if (ref $value eq 'ARRAY') {
             foreach my $subval (@$value) {
                 
+                $this->_navi->saveState();
+                
                 my $node = $this->_navi->NavigateCreate($key);
                 
                 unless(defined $node) {
-                    $this->_navi->Back();
+                    #$this->_navi->Back();
+                    $this->_navi->restoreState();
                     next KEYLOOP;
                 }
                 
+                $this->_navi->applyState();
+                
                 $this->Transform($subval);
                 
                 $this->_navi->Back();
             }
         } else {
+            $this->_navi->saveState();
             my $node = $this->_navi->NavigateCreate($key);
-                
+
             unless(defined $node) {
-                $this->_navi->Back();
+                #$this->_navi->Back();
+                $this->_navi->restoreState();
                 next KEYLOOP;
             }
             
+            $this->_navi->applyState();
+            
+            warn "$key = $value";
+            
             $this->Transform($value);
             
             $this->_navi->Back();            
@@ -96,9 +119,24 @@
     return $this->_navi->Current;
 }
 
+# this method handles situatuions when a complex object must be stored in a
+# simple node.
+sub StoreObject {
+    my ($this,$node,$data) = @_;
+    
+    $node->nodeValue($data);
+    
+    warn "Stored value for", $node->nodeName;
+    
+    return $node;
+}
+
 sub TransformDefault {
     my ($this,$data) = @_;
     
+    return $this->StoreObject($this->currentNode,$data)
+        if !$this->currentNode->schema->isa(ComplexNode);
+    
     if ( ref $data and eval { $data->can('GetMeta') } ) {
         my %props = map {
             $_->name, 1