Latest 0.2.0
Homepage https://github.com/siuying/NanoStoreModel
License MIT
Platforms ios 5.0, requires ARC
Dependencies NanoStore
Authors

Quick and easy way to use NanoStore objects as your model.

NanoStoreModel composite of two parts: a model DSL and a thin layer of Objective-C classes provide dynamic attributes accessor, initializer, finders, helpers and KVO.

Stage: Proof of concept. Don’t use it in production yet, but try it out and see if it works!

How It Works

  1. Define your model.
  2. Use the model class directly or subclass them.

Define a Model

Define property normally as you would for your subclass of NSMObject.

  • For the properties that are of supported types (NSArray, NSDictionary, NSString, NSData, NSDate, NSNumber), the values will be persisted.
  • For the properties that are NSFNanoBag, the key of the bag will be persisted.
  • Other properties are ignored.
@interface User : NSMObject
@property (strong) NSString* name;
@property (strong) NSNumber* age;
@property (strong) NSDate* createdAt;
@property (strong) NSFNanoBag* cars;
@end

@implementation User
@dynamic name, age, createdAt, cars;
@end

Using Model

// Instantiate a NanoStore and open it
NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFMemoryStoreType path:nil error:nil];

// Create an User
User *user = [User model];
user.name = @"Joe";
user.age = @20;
user.createdAt = [NSDate date];

// Add it to the document store
[nanoStore addObject:object error:nil];

// Save the Store
[nanoStore saveStoreAndReturnError:nil];

// Close the document store
[nanoStore closeWithError:nil];

Using Bags

When you set a bag to a NSFNanoBag property, the key of the bag will be saved to the model. Later when you retrieve the model, the corresponding bag will be automatically retrieved.

You can add any object conforming to the NSFNanoObjectProtocol protocol to the bag.

// Instantiate a NanoStore and open it
NSFNanoStore *nanoStore = [NSFNanoStore createAndOpenStoreWithType:NSFMemoryStoreType path:nil error:nil];

// Create an User
User *user = [User modelWithDictionary:@{@"name": @"Joe", @"age": @18, @"createdAt": [NSDate date]}];

// Add a bag
user.cars = [NSFNanoBag bag];

// Add object to bag
[user.cars addObject:[Car modelWithDictionary:@{@"name": @"Honda"}] error:nil];

// Add them to the document store
[nanoStore addObject:user error:nil];
[nanoStore addObject:user.cars error:nil];

// Save the Store
[nanoStore saveStoreAndReturnError:nil];

// Close the document store
[nanoStore closeWithError:nil];

More Info

Obviously you will need to check NanoStore before using NanoStoreModel.

LICENSE

MIT License, for details please read LICENSE.

Latest podspec

{
    "name": "NanoStoreModel",
    "version": "0.2.0",
    "platforms": {
        "ios": "5.0"
    },
    "license": "MIT",
    "source": {
        "git": "https://github.com/siuying/NanoStoreModel.git",
        "tag": "0.2.0"
    },
    "source_files": "NanoStoreModel/Model/*.{h,m}",
    "requires_arc": true,
    "homepage": "https://github.com/siuying/NanoStoreModel",
    "summary": "Quick and easy way to use NanoStore as your model.",
    "authors": {
        "Francis Chong": "[email protected]"
    },
    "dependencies": {
        "NanoStore": [
            ">= 2.6.0"
        ]
    }
}

Pin It on Pinterest

Share This