ActiveTouch is an implementation of ActiveRecord using TouchDB-iOS (an implementation of CouchDB for iOS)
- Why not CoreData? Just because it's a complex tool (TouchDB is easier than CoreData).
##Contact:
Developed by Lucas Medeiros
- Cocoapods - https://github.com/CocoaPods/CocoaPods
To install cocoapods you will need ruby.
gem install cocoapods
More information about cocoapods:
Add the dependency to your Podfile:
platform :ios
...
pod 'ActiveTouch'
...Run pod install to install the dependencies.
- First you need to setup the database in you AppDelegate:
#import "ATDatabaseContainer.h"
@implementation ATAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[ATDatabaseContainer sharedInstance] openDatabaseWithName:@"your_database_name"];
return YES;
}
- Create a class that inherits from ATModel
#import <Foundation/Foundation.h>
#import "ATModel.h"
@interface Car : ATModel
@property (nonatomic, copy) NSString *model;
@property (nonatomic, copy) NSString *year;
@end
- Getting all cars:
[Car allWithSuccessBlock:^(NSArray *collection) {
self.cars = [NSMutableArray arrayWithArray:collection];
[self.tableView reloadData];
} withErrorBlock:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
- Getting all cars using pagination:
[Car allWithLimit:10 skipping:5 withSuccessBlock:^(NSArray *collection) {
} withErrorBlock:^(NSError *error) {
}];
- Finding by _id
Car *car = [Car findById:@"12312-ASADSD"];
-
Validating model:
ATModel has a method called isValid that returns a BOOL, by default it returns YES. If you want some sort of validation you can override it and validates the model as you wish.
-
Creating a car:
Car *car = [[Car alloc] init];
car.model = @"aModel";
car.year = @"aYear";
[car createWithSuccessBlock:^{
} withErrorBlock:^(NSError *error) {
}];
- Updating a car:
[car updateWithSuccessBlock:^{
} withErrorBlock:^(NSError *error) {
}];
- Destroying a car:
[car destroyWithSuccessBlock:^{
} withErrorBlock:^(NSError *error) {
}];
- Adding more couch db views
If want to add another couchdb view you can overrir the +(void)registerViews method of ATModel like this:
+ (void)registerViews
{
[super registerViews];
//Add another view here
}
- Changing the default view order
When you call the all method it will order your documents by _id, if you would like to change the order do this:
+ (NSArray *)sortOrder
{
return @[ @"name", @"age" ];
}
- ActiveTouch has support for running integration tests. What you need to do is setup your kiwi specs like this:
#import "ATDatabaseTestRunner.h"
#import "ATDatabaseContainer.h"
#import "Kiwi.h"
SPEC_BEGIN(ATModelIntegrationSpec)
beforeAll(^{
[ATDatabaseTestRunner openTestDatabaseNamed:@"your_test_database"];
});
beforeEach(^{
[ATDatabaseContainer stub:@selector(sharedInstance) andReturn:[ATDatabaseTestRunner databaseContainer]];
[ATDatabaseTestRunner cleanDatabase];
});
afterAll(^{
[ATDatabaseTestRunner removeDatabase];
});
SPEC_END
- ActiceTouch also has a built-in kiwi matcher for validation purposes:
specify(^{
[[model should] beValid];
});
The ATModel class inherits from MTLModel class from Github's Mantle,
which can be used to unmarshall JSONs that comes from webservices calls.
This make your integration with network frameworks, such as AFNetworking, easier.
ActiveTouch requires iOS 5.x or greater.
Eduardo Gurgel for giving me the project name.
Usage is provided under the MIT License. See LICENSE for the full details.