API  0.9.10
CPValueTransformer.j
Go to the documentation of this file.
1 /*
2  * CPKeyValueBinding.j
3  * Foundation
4  *
5  * Created by Ross Boucher 2/15/09
6  * Copyright 280 North, Inc.
7  *
8  * Adapted from GNUStep
9  * Released under the LGPL.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 
27 var transformerMap = @{};
28 @implementation CPValueTransformer : CPObject
29 {
30  id __doxygen__;
31 }
32 
33 + (void)initialize
34 {
35  if (self !== [CPValueTransformer class])
36  return;
37 
38 // [CPValueTransformer setValueTransformer:[[CPNegateBooleanTransformer alloc] init] forName:CPNegateBooleanTransformerName];
39 // [CPValueTransformer setValueTransformer:[[CPIsNilTransformer alloc] init] forName:CPIsNilTransformerName];
40 // [CPValueTransformer setValueTransformer:[[CPIsNotNilTransformer alloc] init] forName:CPIsNotNilTransformerName];
41 // [CPValueTransformer setValueTransformer:[[CPUnarchiveFromDataTransformer alloc] init] forName:CPUnarchiveFromDataTransformerName];
42 }
43 
44 + (void)setValueTransformer:(CPValueTransformer)transformer forName:(CPString)aName
45 {
46  [transformerMap setObject:transformer forKey:aName];
47 }
48 
49 + (CPValueTransformer)valueTransformerForName:(CPString)aName
50 {
51  return [transformerMap objectForKey:aName];
52 }
53 
55 {
56  return [transformerMap allKeys];
57 }
58 
60 {
61  return NO;
62 }
63 
65 {
66  return [CPObject class];
67 }
68 
69 - (id)reverseTransformedValue:(id)aValue
70 {
71  if (![[self class] allowsReverseTransformation])
72  {
73  [CPException raise:CPInvalidArgumentException reason:(self + " is not reversible.")];
74  }
75 
76  return [self transformedValue:aValue];
77 }
78 
79 - (id)transformedValue:(id)aValue
80 {
81  return nil;
82 }
83 
84 @end
85 
86 // built-in transformers
88 {
89  id __doxygen__;
90 }
91 
93 {
94  return YES;
95 }
96 
98 {
99  return [CPNumber class];
100 }
101 
102 - (id)reverseTransformedValue:(id)aValue
103 {
104  return ![aValue boolValue];
105 }
106 
107 - (id)transformedValue:(id)aValue
108 {
109  return ![aValue boolValue];
110 }
111 
112 @end
113 @implementation CPIsNilTransformer : CPValueTransformer
114 {
115  id __doxygen__;
116 }
117 
119 {
120  return NO;
121 }
122 
124 {
125  return [CPNumber class];
126 }
127 
128 - (id)transformedValue:(id)aValue
129 {
130  return aValue === nil || aValue === undefined;
131 }
132 
133 @end
135 {
136  id __doxygen__;
137 }
138 
140 {
141  return NO;
142 }
143 
145 {
146  return [CPNumber class];
147 }
148 
149 - (id)transformedValue:(id)aValue
150 {
151  return aValue !== nil && aValue !== undefined;
152 }
153 
154 @end
156 {
157  id __doxygen__;
158 }
159 
161 {
162  return YES;
163 }
164 
166 {
167  return [CPData class];
168 }
169 
170 - (id)reverseTransformedValue:(id)aValue
171 {
173 }
174 
175 - (id)transformedValue:(id)aValue
176 {
178 }
179 
180 @end
181 
182 CPNegateBooleanTransformerName = @"CPNegateBoolean";
185 CPUnarchiveFromDataTransformerName = @"CPUnarchiveFromData";
186 CPKeyedUnarchiveFromDataTransformerName = @"CPKeyedUnarchiveFromData";
187 
189 [CPValueTransformer setValueTransformer:[[CPIsNilTransformer alloc] init] forName:CPIsNilTransformerName];
190 [CPValueTransformer setValueTransformer:[[CPIsNotNilTransformer alloc] init] forName:CPIsNotNilTransformerName];
191 [CPValueTransformer setValueTransformer:[[CPUnarchiveFromDataTransformer alloc] init] forName:CPUnarchiveFromDataTransformerName];
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPNegateBooleanTransformerName
A Cappuccino wrapper for any data type.
Definition: CPData.h:2
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
Unarchives objects created using CPKeyedArchiver.
Implements keyed archiving of object graphs (e.g. for storing data).
An immutable string (collection of characters).
Definition: CPString.h:2
var transformerMap
CPIsNilTransformerName
CPKeyedUnarchiveFromDataTransformerName
CPIsNotNilTransformerName
id unarchiveObjectWithData:(CPData aData)
Class class()
Definition: CPObject.j:179
void setValueTransformer:forName:(CPValueTransformer transformer, [forName] CPString aName)
CPUnarchiveFromDataTransformerName
A bridged object to native Javascript numbers.
Definition: CPNumber.h:2
id transformedValue:(id aValue)
id alloc()
Definition: CPObject.j:130
CPData archivedDataWithRootObject:(id anObject)