Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

- some progress

  • Loading branch information...
commit 930bbbe829465b031fb57a29e65a02ea07e9b76c 1 parent 5fb41af
Nickolay Platonov authored May 02, 2010
2  MANIFEST
... ...
@@ -1,4 +1,6 @@
1 1
 Changes
  2
+doc/html/Data/Visitor.html
  3
+doc/mmd/Data/Visitor.txt
2 4
 inc/Module/Build/Functions.pm
3 5
 inc/Module/Build/Functions/DSL.pm
4 6
 inc/Module/Build/JSAN.pm
110  lib/Data/Visitor.js
... ...
@@ -1,6 +1,5 @@
1 1
 Class('Data.Visitor', {
2 2
     
3  
-    
4 3
     has : {
5 4
         seen                    : Joose.I.Object
6 5
     },
@@ -8,97 +7,114 @@ Class('Data.Visitor', {
8 7
         
9 8
     methods : {
10 9
         
  10
+        getClassNameFor : function (object) {
  11
+            if (Joose.O.isInstance(object))      return object.meta.name
  12
+            
  13
+            return Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/\]$/, '')
  14
+        },
  15
+        
  16
+        
11 17
         visit : function () {
12 18
             var seen    = this.seen
13 19
             
14  
-            Joose.A.each(arguments, function (value) {
  20
+            var res = Joose.A.map(arguments, function (value) {
15 21
                 
16 22
                 if (value && typeof value == 'object' || typeof value == 'function') {
17 23
                     
18  
-                    var ref = value.__ID__
19  
-                    
20  
-                    if (!ref) ref = value.__ID__ = this.my.getRefAddr()
  24
+                    var ref         = value.__REFADR__
21 25
                     
22  
-                    if (seen[ref]) 
23  
-                        this.visitSeen(value)
24  
-                    else
25  
-                        this.visitNotSeenObject(value)
  26
+                    if (ref != null && seen[ ref ]) 
  27
+                        return this.visitSeen(value, seen[ ref ])
  28
+                    else                        
  29
+                        return this.visitNotSeen(value)
26 30
                     
27 31
                 } else
28  
-                    this.visitValue(value)
  32
+                    return this.visitValue(value)
29 33
                     
30 34
             }, this)
  35
+            
  36
+            return res.length > 1 ? res : res[0]
31 37
         },
32 38
         
33 39
         
34 40
         visitValue : function (value) {
  41
+            return value
  42
+        },
  43
+        
  44
+        
  45
+        visitSeen : function (value, seenResult) {
  46
+            return seenResult
35 47
         },
36 48
         
37 49
         
38  
-        visitSeen : function (value, result) {
  50
+        markSeen    : function (object) {
  51
+            this.seen[ object.__REFADR__ ] = object
39 52
         },
40 53
         
41 54
         
42  
-        // XXX also handle RegExp, Date
43  
-        visitNotSeenObject : function (value) {
44  
-            this.seen[ value.__ID__ ] = value
45  
-            
46  
-            if (Joose.O.isInstance(value))
47  
-                this.visitInstance(value)
48  
-            else
49  
-                if (value instanceof Array)
50  
-                    this.visitArray(value)
51  
-                else
52  
-                    if (typeof value == 'function')
53  
-                        this.visitFunction(value)
54  
-                    else
55  
-                        this.visitGenericObject(value)
  55
+        visitNotSeen : function (object) {
  56
+            var className   = this.getClassNameFor(object)
  57
+            
  58
+            if (!object.__REFADR__) object.__REFADR__ = this.getRefAdrFor(object)
  59
+            
  60
+            var REFADR  = object.__REFADR__
  61
+            
  62
+            this.markSeen(object)
  63
+
  64
+            
  65
+            if (Joose.O.isInstance(object)) return this.seen[ REFADR ] = this.visitJooseInstance(object, className)
  66
+            
  67
+            
  68
+            var methodName = 'visit' + className
  69
+            
  70
+            if (!this.meta.hasMethod(methodName)) methodName = 'visitObject' 
  71
+            
  72
+            return this.seen[ REFADR ] = this[ methodName ](object, className)
56 73
         },
57 74
         
58 75
         
59  
-        visitArray  : function (array) {
  76
+        visitArray  : function (array, className) {
60 77
             Joose.A.each(array, function (value, index) {
61 78
                 
62 79
                 this.visitArrayEntry(value, index, array)
63 80
                 
64 81
             }, this)
  82
+            
  83
+            return array
65 84
         },
66 85
         
67 86
         
68 87
         visitArrayEntry  : function (value, index, array) {
69  
-            this.visit(value)
  88
+            return this.visit(value)
70 89
         },
71 90
         
72 91
         
73  
-        visitGenericObject : function (object) {
  92
+        visitObject : function (object, className) {
74 93
             Joose.O.eachOwn(object, function (value, key) {
75 94
                 
76  
-                if (key != '__ID__') {
  95
+                if (key != '__REFADR__' && key != '__ID__') {
77 96
                     this.visitObjectKey(key, value, object)
78 97
                     this.visitObjectValue(value, key, object)
79 98
                 }
80 99
                 
81 100
             }, this)
  101
+            
  102
+            return object
82 103
         },
83 104
         
84 105
         
85  
-        visitInstance : function (value) {
86  
-            this.visitGenericObject(value)
  106
+        visitJooseInstance : function (value, className) {
  107
+            return this.visitObject(value, className)
87 108
         },
88 109
         
89 110
         
90 111
         visitObjectKey : function (key, value, object) {
91  
-            this.visitValue(key)
  112
+            return this.visitValue(key)
92 113
         },
93 114
         
94 115
         
95 116
         visitObjectValue : function (value, key, object) {
96  
-            this.visit(value)
97  
-        },
98  
-        
99  
-        
100  
-        visitFunction : function (value) {
101  
-            this.visitGenericObject(value)
  117
+            return this.visit(value)
102 118
         }
103 119
         
104 120
     },
@@ -106,9 +122,18 @@ Class('Data.Visitor', {
106 122
     
107 123
 body : function () {
108 124
     
109  
-    var ID      = 0
  125
+    var REF      = 0
110 126
     
111 127
     this.meta.extend({
  128
+        
  129
+        methods : {
  130
+            
  131
+            getRefAdrFor   : function (object) {
  132
+                return REF++
  133
+            }
  134
+        },
  135
+                
  136
+                
112 137
         my : {
113 138
             
114 139
             has : {
@@ -118,13 +143,8 @@ body : function () {
118 143
             
119 144
             methods : {
120 145
                 
121  
-                getRefAddr   : function () {
122  
-                    return ID++
123  
-                },
124  
-                
125  
-                
126 146
                 visit : function () {
127  
-                    var visitor = new this.HOST()
  147
+                    var visitor     = new this.HOST()
128 148
                     
129 149
                     return visitor.visit.apply(visitor, arguments)
130 150
                 }
32  t/010_basics.t.js
... ...
@@ -1,6 +1,6 @@
1 1
 StartTest(function(t) {
2 2
     
3  
-	t.plan(1)
  3
+	t.plan(15)
4 4
     
5 5
     var async0 = t.beginAsync()
6 6
     
@@ -11,6 +11,27 @@ StartTest(function(t) {
11 11
         
12 12
         t.ok(Data.Visitor, "Data.Visitor is here")
13 13
         
  14
+        var data1 = {}
  15
+        var data2 = []
  16
+        var data3 = 10
  17
+        var data4 = true
  18
+        
  19
+        t.ok(data1 === Data.Visitor.my.visit(data1), "Visiting don't modify the data #1")
  20
+        t.ok(data2 === Data.Visitor.my.visit(data2), "Visiting don't modify the data #2")
  21
+        t.ok(data3 === Data.Visitor.my.visit(data3), "Visiting don't modify the data #3")
  22
+        t.ok(data4 === Data.Visitor.my.visit(data4), "Visiting don't modify the data #4")
  23
+        
  24
+        
  25
+        //======================================================================================================================================================================================================================================================
  26
+        t.diag('N-arity')
  27
+        
  28
+        var res = Data.Visitor.my.visit(data1, data2, data3, data4)
  29
+        
  30
+        t.ok(data1 === res[0], "Visiting don't modify the data #1")
  31
+        t.ok(data2 === res[1], "Visiting don't modify the data #2")
  32
+        t.ok(data3 === res[2], "Visiting don't modify the data #3")
  33
+        t.ok(data4 === res[3], "Visiting don't modify the data #4")
  34
+        
14 35
         
15 36
         //======================================================================================================================================================================================================================================================
16 37
         t.diag('Composite structures')
@@ -36,12 +57,9 @@ StartTest(function(t) {
36 57
                 },
37 58
                 
38 59
                 
39  
-                visitArray      : function () { arrayCounter++ },
40  
-                
41  
-                visitInstance   : function () { instanceCounter++ },
42  
-                
43  
-                visitNotSeenObject : function () { objectCounter++ }
44  
-                
  60
+                visitArray              : function () { arrayCounter++ },
  61
+                visitJooseInstance      : function () { instanceCounter++ },
  62
+                visitNotSeen            : function () { objectCounter++ }
45 63
             }
46 64
         })
47 65
         

0 notes on commit 930bbbe

Please sign in to comment.
Something went wrong with that request. Please try again.