Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Implementation of CouchUser and CouchSecurity #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fabien
Copy link

@fabien fabien commented Jun 21, 2012

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

@fabien
Copy link
Author

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
Copy link
Contributor

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!

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);
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);
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants