Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/uasi/factorykit
Object generation library for Core Data
https://github.com/uasi/factorykit
Last synced: about 7 hours ago
JSON representation
Object generation library for Core Data
- Host: GitHub
- URL: https://github.com/uasi/factorykit
- Owner: uasi
- License: mit
- Created: 2013-12-04T02:19:00.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-05T14:15:19.000Z (almost 11 years ago)
- Last Synced: 2024-10-31T16:03:05.154Z (5 days ago)
- Language: Objective-C
- Size: 164 KB
- Stars: 15
- Watchers: 5
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: Changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# FactoryKit
## Installation
FactoryKit is available through [CocoaPods](http://cocoapods.org), to install
it simply add the following line to your Podfile:pod "FactoryKit"
## Usage
```objc
#import...
- (void)setUpObjects {
NSManagedObjectContext *context = ...;
FCTFactory *factory = [[FCTFactory alloc] initWithManagedObjectContext:context];// Define a builder for an entity.
[factory defineBuilderForEntityForName:@"Person" usingBlock:^(FCTDefinition *def) {
def[@"name"] = @"Bob";
}];// Generate a saved object.
NSManagedObject *person1 = [factory createManagedObjectUsingBuilderNamed:@"Person"];
NSAssert([[person1 valueForKey:@"name"] isEqualToString:@"Bob"], @"");
NSAssert(![person1 hasChanges], @"");// Or unsaved.
NSManagedObject *person2 = [factory buildManagedObjectUsingBuilderNamed:@"Person"];
NSAssert([[person2 valueForKey:@"name"] isEqualToString:@"Bob"], @"");
NSAssert( [person2 hasChanges], @"");// Give a builder a name.
[factory defineBuilderNamed:@"Mac" forEntityForName:@"Product" usingBlock:^(FCTDefinition *def) {
// Deferred definition.
def[@"builtAt"] = ^{ return [NSDate date]; };// Sequential definition (n = 1, 2, ...).
def[@"serialNo"] = ^id(NSUInteger n) { return @(n); };// And callbacks.
[def afterBuild:^(NSManagedObject *builtObject) {
doSomething(builtObject);
}];[def afterCreate:^(NSManagedObject *createdObject) {
oneMoreThing(createdObject);
}];
}];NSDate *beforeCreation = [NSDate date];
NSManagedObject *mac1 = [factory createManagedObjectUsingBuilderNamed:@"Mac"];
NSManagedObject *mac2 = [factory createManagedObjectUsingBuilderNamed:@"Mac"];// Deferred definitions are evaluated every time you create an object.
NSAssert([beforeCreation compare:[mac1 valueForKey:@"builtAt"]] == NSOrderedAscending, @"");
NSAssert([[mac1 valueForKey:@"builtAt"] compare:[mac2 valueForKey:@"builtAt"]] == NSOrderedAscending, @"");// Same, with sequence number.
NSAssert([[mac1 valueForKey:@"serialNo"] isEqualToNumber:@1], @"");
NSAssert([[mac2 valueForKey:@"serialNo"] isEqualToNumber:@2], @"");
}
```## Author
Tomoki Aonuma ([@uasi](https://twitter.com/uasi))
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull RequestFeature requests and bug reports are welcome. Feel free to submit an issue or
contact the author on Twitter.## License
FactoryKit is available under the MIT license. See the LICENSE file for more info.