Coder Social home page Coder Social logo

xperienced / objective-record Goto Github PK

View Code? Open in Web Editor NEW

This project forked from supermarin/objectiverecord

1.0 1.0 0.0 518 KB

A lightweight Active Record - style of managing CoreData objects.

C 0.10% Ruby 0.61% Shell 0.79% Objective-C 98.50%

objective-record's Introduction

Intro

This is a lightweight ActiveRecord way of managing Core Data objects. The syntax is borrowed from Ruby on Rails.
And yeah, no AppDelegate code. It's fully tested with Kiwi.

Usage

  1. Install with CocoaPods or clone
  2. #import "ObjectiveRecord.h" in your model or .pch file.

Create / Save / Delete

Person *john = [Person create];
john.name = @"John";
john.save;
john.delete;

NSDictionary *attributes; // assume it's populated with name = john, key = value,...
[Person create:dictionary];

[Person create:@{ @"name" : @"John", @"age" : @12, @"member" : @NO }]; // XCode >= 4.4

Finders

NSArray *people = [Person all];
NSArray *johns = [Person where:@"name == 'John'"];
Person *johnDoe = [Person where:@"name == 'John' AND surname = 'Doe'"].first;

// XCode >= 4.4
NSArray *people = [Person where:@{ @"age" : @18 }];

NSArray *people = [Person where:@{ @"age" : @18,
                  @"member" : @YES,
                  @"state" : @"NY"
                  }];

Custom ManagedObjectContext

NSManagedObjectContext *newContext = [NSManagedObjectContext new];

Person *john = [Person createInContext:newContext];
Person *john = [Person where:@"name == 'John'" inContext:newContext].first;
NSArray *people = [Person allInContext:newContext];

Custom CoreData model or .sqlite database

If you've added the Core Data manually, you can change the custom model and database name in CoreDataManager.m

static NSString *CUSTOM_MODEL_NAME = @"Database";
static NSString *CUSTOM_DATABASE_NAME = nil;

NSArray additions

NSArray *array; // assume it's full of objects

[array each:^(id object) {
    
    NSLog(@"Object: %@", object); 
}];

[array eachWithIndex:^(id object, int index) {
    
    NSLog(@"Object: %@ idx: %i", object, index); 
}];

id object = array.first;
id object = array.last;

Examples

// find
[[Person all] each:^(Person *person) {
    
    person.member = @NO;
}];

for(Person *person in [Person all]) {
  
    person.member = @YES;
}

// create / save
Person *john = [Person create];
john.name = @"John";
john.surname = @"Wayne";
john.save;

// find / delete
[[Person where: @{ "member" : @NO }] each:^(Person *person) {
  
  [person delete];
}];

####ToDo Try to make where:(NSString *)condition take va_args like NSLog(). That way you wouldn't have to use -stringWithFormat, and it would look cleaner.

objective-record's People

Contributors

andrewsardone avatar xperienced avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.