Created
April 6, 2010 15:22
-
-
Save robi42/357712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: src/org/ringojs/wrappers/Storable.java | |
=================================================================== | |
--- src/org/ringojs/wrappers/Storable.java (revision 283dfdd4c41ea6b9e7965f6abe01cad6e9842a65) | |
+++ src/org/ringojs/wrappers/Storable.java Wed Apr 07 00:16:59 CEST 2010 | |
@@ -1,23 +1,25 @@ | |
package org.ringojs.wrappers; | |
import org.mozilla.javascript.*; | |
-import org.mozilla.javascript.annotations.JSStaticFunction; | |
import org.mozilla.javascript.annotations.JSFunction; | |
import org.mozilla.javascript.annotations.JSGetter; | |
+import org.mozilla.javascript.annotations.JSStaticFunction; | |
import org.ringojs.util.ScriptUtils; | |
public class Storable extends ScriptableObject { | |
private Scriptable store; | |
private String type; | |
+ private Scriptable mapping; | |
private boolean isPrototype; | |
private Scriptable properties; | |
private Object key; | |
private Object entity; | |
- enum FactoryType {CONSTRUCTOR, FACTORY}; | |
+ enum FactoryType {CONSTRUCTOR, FACTORY} | |
+ | |
public Storable() { | |
this.isPrototype = true; | |
} | |
@@ -28,9 +30,17 @@ | |
this.isPrototype = true; | |
} | |
+ private Storable(Scriptable store, String type, Scriptable mapping) { | |
+ this.store = store; | |
+ this.type = type; | |
+ this.mapping = mapping; | |
+ this.isPrototype = true; | |
+ } | |
+ | |
private Storable(Storable prototype) { | |
this.store = prototype.store; | |
this.type = prototype.type; | |
+ this.mapping = prototype.mapping; | |
this.isPrototype = false; | |
} | |
@@ -70,11 +80,12 @@ | |
} | |
@JSStaticFunction | |
- public static Scriptable defineClass(Scriptable store, String type) | |
+ public static Scriptable defineClass(Scriptable store, String type, Scriptable mapping) | |
throws NoSuchMethodException { | |
int attr = DONTENUM | PERMANENT | READONLY; | |
Scriptable scope = ScriptRuntime.getTopCallScope(Context.getCurrentContext()); | |
- Storable prototype = new Storable(store, type); | |
+ Storable prototype = mapping == null ? new Storable(store, type) : | |
+ new Storable(store, type, mapping); | |
prototype.setParentScope(scope); | |
prototype.setPrototype(ScriptableObject.getClassPrototype(scope, "Storable")); | |
// create the constructor, visible to the application | |
@@ -124,7 +135,7 @@ | |
entity = invokeStoreMethod("getEntity", type, properties != null ? properties : key); | |
} | |
if (transaction == Undefined.instance) { | |
- invokeStoreMethod("save", properties, entity); | |
+ invokeStoreMethod("save", properties, entity); | |
} else { | |
invokeStoreMethod("save", properties, entity, transaction); | |
} | |
@@ -138,7 +149,7 @@ | |
key = invokeStoreMethod("getKey", type, entity); | |
} | |
if (transaction == Undefined.instance) { | |
- invokeStoreMethod("remove", key); | |
+ invokeStoreMethod("remove", key, type); | |
} else { | |
invokeStoreMethod("remove", key, transaction); | |
} | |
@@ -165,6 +176,11 @@ | |
return Undefined.instance; | |
} | |
+ @JSGetter("_mapping") | |
+ public Scriptable getMapping() { | |
+ return mapping; | |
+ } | |
+ | |
@Override | |
public boolean has(String name, Scriptable start) { | |
if (isPrototype) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might try this patch:
http://gist.github.com/358670
It attaches the third argument to defineClass to the "mapping" property of the created constructor. We could add it to the prototype, but I think the constructor/class is really the more appropriate place in this case.