WIP: objc utilities

This commit is contained in:
John Rommel Estropia
2016-06-19 02:40:25 +09:00
parent 3d5c4f8121
commit e5a199489c
19 changed files with 913 additions and 177 deletions

View File

@@ -37,6 +37,32 @@ import CoreData
@objc
public final class CSFrom: NSObject, CoreStoreObjectiveCType {
/**
The associated `NSManagedObject` entity class
*/
@objc
public var entityClass: AnyClass {
return self.bridgeToSwift.entityClass
}
/**
The `NSPersistentStore` configuration names to associate objects from.
May contain `NSString` instances to pertain to named configurations, or `NSNull` to pertain to the default configuration
*/
@objc
public var configurations: [AnyObject]? {
return self.bridgeToSwift.configurations?.map {
switch $0 {
case nil: return NSNull()
case let string as NSString: return string
}
}
}
/**
Initializes a `CSFrom` clause with the specified entity class.
```
@@ -58,14 +84,24 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
MyPersonEntity *people = [transaction fetchAllFrom:[CSFrom entityClass:[MyPersonEntity class] configuration:@"Configuration1"]];
```
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration.
- parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `[NSNull null]` to use the default configuration.
- parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter)
- returns: a `CSFrom` clause with the specified configurations
*/
@objc
public static func entityClass(entityClass: AnyClass, configuration: String?) -> CSFrom {
public static func entityClass(entityClass: AnyClass, configuration: AnyObject) -> CSFrom {
return self.init(From(entityClass, configuration))
switch configuration {
case let string as String:
return self.init(From(entityClass, string))
case is NSNull:
return self.init(From(entityClass, nil))
default:
CoreStore.abort("The configuration argument only accepts NSString and NSNull values")
}
}
/**
@@ -81,7 +117,22 @@ public final class CSFrom: NSObject, CoreStoreObjectiveCType {
@objc
public static func entityClass(entityClass: AnyClass, configurations: [AnyObject]) -> CSFrom {
return self.init(From(entityClass, configurations.map { $0 is NSNull ? nil : ($0 as! String) }))
var arguments = [String?]()
for configuration in configurations {
switch configuration {
case let string as String:
arguments.append(string)
case is NSNull:
arguments.append(nil)
default:
CoreStore.abort("The configurations argument only accepts NSString and NSNull values")
}
}
return self.init(From(entityClass, arguments))
}

View File

@@ -37,6 +37,14 @@ import CoreData
@objc
public final class CSGroupBy: NSObject, CSQueryClause, CoreStoreObjectiveCType {
/**
The list of key path strings to group results with
*/
public var keyPaths: [KeyPath] {
return self.bridgeToSwift.keyPaths
}
/**
Initializes a `CSGroupBy` clause with a list of key path strings

View File

@@ -38,15 +38,12 @@ import CoreData
public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClause, CoreStoreObjectiveCType {
/**
Initializes a `CSWhere` clause with an `NSPredicate`
- parameter predicate: the `NSPredicate` for the fetch or query
- returns: a `CSWhere` clause with an `NSPredicate`
The internal `NSPredicate` instance for the `Where` clause
*/
@objc
public static func predicate(predicate: NSPredicate) -> CSWhere {
public var predicate: NSPredicate {
return self.init(Where(predicate))
return self.bridgeToSwift.predicate
}
/**
@@ -100,6 +97,18 @@ public final class CSWhere: NSObject, CSFetchClause, CSQueryClause, CSDeleteClau
return self.init(Where(keyPath, isMemberOf: list))
}
/**
Initializes a `CSWhere` clause with an `NSPredicate`
- parameter predicate: the `NSPredicate` for the fetch or query
- returns: a `CSWhere` clause with an `NSPredicate`
*/
@objc
public static func predicate(predicate: NSPredicate) -> CSWhere {
return self.init(Where(predicate))
}
// MARK: NSObject

View File

@@ -0,0 +1,152 @@
//
// CoreStoreBridge.h
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import <Foundation/Foundation.h>
#ifndef CoreStoreBridge_h
#define CoreStoreBridge_h
#if !__has_feature(objc_arc)
#error CoreStore Objective-C utilities require ARC be enabled
#endif
#if !__has_extension(attribute_overloadable)
#error CoreStore Objective-C utilities can only be used on platforms that support C function overloading
#endif
#define CS_OBJC_EXTERN extern
#define CS_OBJC_OVERLOADABLE __attribute__((__overloadable__))
#define CS_OBJC_REQUIRES_NIL_TERMINATION(A, B) __attribute__((sentinel(A, B)))
// MARK: - From
@class CSFrom;
/**
@abstract
Initializes a <tt>CSFrom</tt> clause with the specified entity class.
@code
MyPersonEntity *people = [transaction fetchAllFrom:From([MyPersonEntity class])];
@endcode
@param entityClass
the <tt>NSManagedObject</tt> class type to be created
@result
a <tt>CSFrom</tt> clause with the specified entity class
*/
CS_OBJC_EXTERN CS_OBJC_OVERLOADABLE
CSFrom *_Nonnull From(Class _Nonnull entityClass);
/**
@abstract
Initializes a <tt>CSFrom</tt> clause with the specified configurations.
@code
MyPersonEntity *people = [transaction fetchAllFrom:From([MyPersonEntity class], @[@"Configuration1"])];
@endcode
@param entityClass
the <tt>NSManagedObject</tt> class type to be created
@param configurations
an array of the <tt>NSPersistentStore</tt> configuration names to associate objects from. This parameter is required if multiple configurations contain the created <tt>NSManagedObject</tt>'s entity type. Set to <tt>[NSNull null]</tt> to use the default configuration.
@result
a <tt>CSFrom</tt> clause with the specified configurations
*/
CS_OBJC_EXTERN CS_OBJC_OVERLOADABLE
CSFrom *_Nonnull From(Class _Nonnull entityClass, NSArray<id> *_Nonnull configurations);
// MARK: - Where
@class CSWhere;
/**
@abstract
Initializes a <tt>CSWhere</tt> clause with a predicate that always evaluates to the specified boolean value
@param value
the boolean value for the predicate
@result
a <tt>CSWhere</tt> clause with a predicate that always evaluates to the specified boolean value
*/
CS_OBJC_EXTERN CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(BOOL value);
/**
@abstract
Initializes a <tt>CSWhere</tt> clause with a predicate using the specified string format and arguments
@param format
the format string for the predicate
@param argumentArray
the arguments for <tt>format</tt>
@result
a <tt>CSWhere</tt> clause with a predicate using the specified string format and arguments
*/
CS_OBJC_EXTERN CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(NSString *_Nonnull format, ...);
/**
@abstract
Initializes a <tt>CSWhere</tt> clause with an <tt>NSPredicate</tt>
@param predicate
the <tt>NSPredicate</tt> for the fetch or query
@result
a <tt>CSWhere</tt> clause with an <tt>NSPredicate</tt>
*/
CS_OBJC_EXTERN CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(NSPredicate *_Nonnull predicate);
// MARK: - GroupBy
@class CSGroupBy;
/**
@abstract
Initializes a <tt>CSGroupBy</tt> clause with a list of key path strings
@param keyPaths
a list of key path strings to group results with
@result
a <tt>CSGroupBy</tt> clause with a list of key path strings
*/
CS_OBJC_OVERLOADABLE
CSGroupBy *_Nonnull GroupBy(NSArray<NSString *> *_Nonnull keyPaths);
#endif /* CoreStoreBridge_h */

View File

@@ -0,0 +1,76 @@
//
// CoreStoreBridge.m
// CoreStore
//
// Copyright © 2016 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "CoreStoreBridge.h"
#import <CoreStore/CoreStore-Swift.h>
CS_OBJC_OVERLOADABLE
CSFrom *_Nonnull From(Class _Nonnull entityClass) {
return [CSFrom entityClass:entityClass];
}
CS_OBJC_OVERLOADABLE
CSFrom *_Nonnull From(Class _Nonnull entityClass, NSArray<id> *_Nonnull configurations) {
return [CSFrom entityClass:entityClass configurations:configurations];
}
// MARK: - Where
CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(BOOL value) {
return [CSWhere value:value];
}
CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(NSString *_Nonnull format, ...) {
CSWhere *where;
va_list args;
va_start(args, format);
where = [CSWhere predicate:[NSPredicate predicateWithFormat:format arguments:args]];
va_end(args);
return where;
}
CS_OBJC_OVERLOADABLE
CSWhere *_Nonnull Where(NSPredicate *_Nonnull predicate) {
return [CSWhere predicate:predicate];
}
// MARK: - GroupBy
CS_OBJC_OVERLOADABLE
CSGroupBy *_Nonnull GroupBy(NSArray<NSString *> *_Nonnull keyPaths) {
return [CSGroupBy keyPaths:keyPaths];
}