Permalink
Browse files

Also added the new additions to CouchCocoa

  • Loading branch information...
1 parent e6e0ba1 commit c1f9d5258e1f0d1d454ef4803078ac20b59b04b5 Deepwinter committed May 24, 2012
@@ -0,0 +1,50 @@
+//
+// CouchModelFactory.h
+// CouchCocoa
+//
+// Created by Jens Alfke on 11/22/11.
+// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
+//
+
+#import <CouchCocoa/CouchDatabase.h>
+@class CouchDocument;
+
+
+/** A configurable mapping from CouchDocument to CouchModel.
+ It associates a model class with a value of the document's "type" property. */
+@interface CouchModelFactory : NSObject
+{
+ NSMutableDictionary* _typeDict;
+}
+
+/** Returns a global shared CouchModelFactory that's consulted by all databases.
+ Mappings registered in this instance will be used as a fallback by all other instances if they don't have their own. */
++ (CouchModelFactory*) sharedInstance;
+
+/** Given a document, attempts to return a CouchModel for it.
+ If the document's modelObject property is set, it returns that value.
+ If the document's "type" property has been registered, instantiates the associated class.
+ Otherwise returns nil. */
+- (id) modelForDocument: (CouchDocument*)document;
+
+/** Associates a value of the "type" property with a CouchModel subclass.
+ @param classOrName Either a CouchModel subclass, or its class name as an NSString.
+ @param type The value value of a document's "type" property that should indicate this class. */
+- (void) registerClass: (id)classOrName forDocumentType: (NSString*)type;
+
+/** Returns the appropriate CouchModel subclass for this document.
+ The default implementation just passes the document's "type" property value to -classForDocumentType:, but subclasses could override this to use different properties (or even the document ID) to decide. */
+- (Class) classForDocument: (CouchDocument*)document;
+
+/** Looks up the CouchModel subclass that's been registered for a document type. */
+- (Class) classForDocumentType: (NSString*)type;
+
+@end
+
+
+@interface CouchDatabase (CouchModelFactory)
+
+/** The CouchModel factory object to be used by this database.
+ Every database has its own instance by default, but you can set this property to use a different one -- either to use a custom subclass, or to share a factory among multiple databases, or both. */
+@property (retain) CouchModelFactory* modelFactory;
+@end
@@ -0,0 +1,47 @@
+//
+// CouchTouchDBServer.h
+// CouchCocoa
+//
+// Created by Jens Alfke on 12/20/11.
+// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
+//
+
+#import "CouchServer.h"
+@class TDServer, TDDatabase;
+
+
+/** A convenience class that glues TouchDB into CouchCocoa.
+ On creation, starts up an instance of TDServer and sets up TDURLProtocol to serve it.
+ The CouchServer URL is set to the root URL served by the protocol, so you can treat it just like a normal remote server instance. */
+@interface CouchTouchDBServer : CouchServer
+{
+ @private
+ TDServer* _touchServer;
+ NSError* _error;
+ BOOL _observing;
+}
+
+/** A shared per-process instance. Remember that CouchCocoa is not thread-safe so you can't
+ use this shared instance among multiple threads. */
++ (CouchTouchDBServer*) sharedInstance;
+
+/** Preferred initializer. Starts up an in-process server. */
+- (id)init;
+
+/** Inherited initializer, if you want to connect to a remote server for debugging purposes. */
+- (id) initWithURL: (NSURL*)url;
+
+/** If this is non-nil, the server failed to initialize. */
+@property (readonly) NSError* error;
+
+/** Invokes the given block on the TouchDB server thread, passing it a pointer to the TDServer.
+ You can use this to (carefully!) access the TDServer API.
+ Be aware that the block may not run immediately; it's queued and will be called immediately before the server handles the next REST call. */
+- (void) tellTDServer: (void (^)(TDServer*))block;
+
+/** Invokes the given block on the TouchDB server thread, passing it a pointer to a TDDatabase.
+ You can use this to (carefully!) access the TDDatabase API.
+ Be aware that the block may not run immediately; it's queued and will be called immediately before the server handles the next REST call. */
+- (void) tellTDDatabaseNamed: (NSString*)dbName to: (void (^)(TDDatabase*))block;
+
+@end

0 comments on commit c1f9d52

Please sign in to comment.