Skip to content

Instantly share code, notes, and snippets.

@wildthink
Last active January 13, 2017 17:46
Show Gist options
  • Save wildthink/b911a5b8e0d7dc6ef190fd8fe4f7a2ee to your computer and use it in GitHub Desktop.
Save wildthink/b911a5b8e0d7dc6ef190fd8fe4f7a2ee to your computer and use it in GitHub Desktop.
Simple data store API for Swift applications.
//
// Domain.swift
// Inspired by Eve
//
// Created by Jason Jobe on 11/26/16.
// Copyright © 2016 Jason Jobe. All rights reserved.
// https://gist.github.com/wildthink/b911a5b8e0d7dc6ef190fd8fe4f7a2ee
//
import Foundation
public struct DomainType: ExpressibleByStringLiteral, CustomStringConvertible {
public var name: String
public var plural_name: String { return "\(name)s" }
public var description: String { return "Type:\(name)" }
public init(unicodeScalarLiteral value: String) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: String) {
self.init(stringLiteral: value)
}
public init(stringLiteral value: String) {
name = value
}
}
public enum ChangeType { case insert, update, delete }
public protocol Domain {
func recall (type: DomainType, with context: Any?, where test: Any?, result: ([Any]?) -> Void)
func bind <T> (type: T.Type, graph: Any, update: (ChangeType, [T]) -> Void)
func commit <T: Any> (change: ChangeType, with: Any?, _ objects: [T])
var name: String? { get }
}
extension Domain {
public var name: String? { return nil }
public func recall (type: DomainType, result: ([Any]?) -> Void)
{
return self.recall (type: type, with: nil, where: nil, result: result)
}
public func recall (type: DomainType, with context: Any?, result: ([Any]) -> Void)
{
self.recall (type: type, with: context, where: nil, result: result)
}
public func recall (type: DomainType, with context: Any?, where test: Any?, result: ([Any]) -> Void)
{
Swift.print ("WARNING:", #function, "NOT IMPLEMENTED")
}
public func bind <T> (type: T.Type, graph: Any, update: (ChangeType, [T]) -> Void) {
Swift.print ("WARNING:", #function, "NOT IMPLEMENTED")
}
public func commit <T: Any> (change: ChangeType = .update, _ objects: [T]) {
self.commit(change: change, with: nil, objects)
}
public func commit <T: Any> (change: ChangeType, with context: Any?, _ objects: [T]) {
Swift.print ("WARNING:", #function, "NOT IMPLEMENTED")
}
}
#if os(iOS)
import UIKit.UIResponder
public typealias Responder = UIResponder
public typealias Application = UIApplication
public typealias ApplicationDelegate = UIApplicationDelegate
#else
import AppKit.NSResponder
public typealias Responder = NSResponder
public typealias Application = NSApplication
public typealias ApplicationDelegate = NSApplicationDelegate
#endif
public extension NSObjectProtocol where Self: Responder
{
public var domain: Domain?
{
var next: Responder? = self
while next != nil {
if next is Domain { return next as? Domain }
next = next?.next
}
return self is ApplicationDelegate
? nil : Application.shared.delegate as? Domain
}
public func domain (named: String) -> Domain?
{
var next: Responder? = self
while next != nil {
if let domain = next as? Domain, domain.name == named { return domain }
next = next?.next
}
return self is ApplicationDelegate
? nil : Application.shared.delegate as? Domain
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment