Implementation of CouchUser and CouchSecurity #29

Open
wants to merge 3 commits into
from

Projects

None yet

2 participants

@fabien
fabien commented Jun 21, 2012

Support for /_users and /some-db/_security. Unit tested.

@fabien
fabien commented Jun 22, 2012

Implemented Embedded Model properties.

With this setup a Model property can be embedded instead of referenced by ID,
making it easy to develop a nested model structure. The next logical step is
to enable this for NSArray properties - allowing embedding and referencing of
other CouchModel objects.

If an existing Model is embedded, it keeps a referenceID to the original Model.

See 6b2e8c87219ea25aaa65f38c270165abcfe1b924 and the new unit tests for more information.

@snej
Member
snej commented Jun 22, 2012

Could you please use git rebase --interactive to merge these into a single commit, or at least one commit per new feature? It will keep the change log more concise. Thanks!

fabien added some commits Jun 21, 2012
@fabien fabien CouchModel improvements: defaults, property handling, load/reload/exi…
…sts.
dc3dea0
@fabien fabien Implemented CouchUser (_users) and CouchSecurity (_security) support.
CouchUser* user = [_server userWithName:@"Alice"];
user.password = @"secret"; // will be hashed on the server
// set some additional properties
[user setValue:@"Alice" ofProperty:@"firstname"];
[user setValue:@"Wonderland" ofProperty:@"lastname"];
if ([[user save] wait]) NSLog(@"Saved: %@", user);

CouchSecurity* security = [_db security];
security.readerNames = [NSArray arrayWithObjects:@"bob", nil];
security.adminRoles  = [NSArray arrayWithObjects:@"boss", nil];
// add an admin user
[security addObject:user.name forProperty:kSecurityAdminNamesKey];
if ([security update]) NSLog(@"Saved: %@", security);
7191f9b
@fabien fabien Implemented Embedded Model properties.
With this setup a Model property can be embedded instead of referenced by ID,
making it easy to develop a nested model structure. The next logical step is
to enable this for NSArray properties - allowing embedding and referencing of
other CouchModel objects.

If an existing Model is embedded, it keeps a referenceID to the original Model.

There are various ways to control the embedding behaviour, using hook methods:

+ (id) embeddedModelForDocument:(CouchDocument *)document
                         parent:(CouchModel*)parent
                       property:(NSString *)property;

- (BOOL) isEmbedableModelProperty: (NSString*)property;
- (BOOL) isEmbedableIn: (CouchModel*)parent forProperty:(NSString*)property;
- (void) didEmbedIn: (CouchModel*)parent forProperty:(NSString*)property;
- (NSDictionary*) embeddedPropertiesToSave;

Example of creating a new embedded model:

Student* alice = [self createModelWithName: @"Alice" grade: 9];
Student* buddy = [alice embedModelForProperty:@"buddy"];
buddy.name = @"Bob";
[alice save];
if (alice.buddy.name == bob.name) NSLog(@"Buddies");

Example of embedding an existing model:

Student* alice = [self createModelWithName: @"Alice" grade: 9];
Student* bob = [self createModelWithName: @"Bob" grade: 7];
[alice embedModel:bob forProperty:@"buddy"];
[alice save];
if (alice.buddy.name == bob.name) {
  NSLog(@"Buddies - referenceID: %@", alice.buddy.referenceID);
}
2f26103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment