Add #literal conversion to convert strings to literals or symbols.
authorStefan Vogel <sv@exept.de>
Mon, 02 Apr 2001 11:04:11 +0200
changeset 1464 883cfaa89ffa
parent 1463 df2fec4aad9c
child 1465 02c6dfc01257
Add #literal conversion to convert strings to literals or symbols.
TypeConverter.st
--- a/TypeConverter.st	Wed Mar 28 19:45:00 2001 +0200
+++ b/TypeConverter.st	Mon Apr 02 11:04:11 2001 +0200
@@ -515,6 +515,53 @@
    
 !
 
+literal
+    "setup the converter to convert from a string to a literal
+     and vice versa. Invalid symbols (i.e. empty) are converted to nil;
+     nil values are converted to an empty string.
+     true, false, Numbers are parsed"
+
+    self
+        getBlock:[:model |
+                |val s|
+
+                val := model value.
+                (val isLiteral and:[val notNil]) ifTrue:[
+                    val isSymbol ifTrue:[
+                        "print 'special' symbols as symbols i.e. as #'symbol'"
+                        (s := val storeString) second == $' ifTrue:[
+                            s
+                        ] ifFalse:[
+                            val printString
+                        ]
+                    ] ifFalse:[
+                        val storeString
+                    ].
+                ] ifFalse:[
+                    ''.
+                ]]
+
+        putBlock:
+                [:model :string |
+
+                |value s|
+
+                string size == 0 ifTrue:[
+                    value := nil
+                ] ifFalse:[
+                    s := string withoutSeparators.
+                    value := Object readFrom:s onError:[
+                        s asSymbol
+                    ].
+                ].
+                model value:value]
+
+        updateBlock: [:m :a :p | true]
+
+    "Created: / 21.2.1997 / 18:58:38 / cg"
+    "Modified: / 26.5.1998 / 15:06:06 / cg"
+!
+
 number
     "setup the converter to convert from a string to a number
      and vice versa. Invalid numbers are converted to nil."
@@ -1112,5 +1159,5 @@
 !TypeConverter class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.36 2001-03-28 17:45:00 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/TypeConverter.st,v 1.37 2001-04-02 09:04:11 stefan Exp $'
 ! !