Skip to content

Instantly share code, notes, and snippets.

View groue's full-sized avatar

Gwendal Roué groue

View GitHub Profile
@groue
groue / gist:401e88b7148bc126adb3
Last active August 29, 2015 14:15
GRMustache.swift cycle
import Mustache
// Wrap the result in a section, and the key `cycleKey` will enumerate and cycle
// around boxes, one after the other.
func cyclingBox(key cycleKey: String, #boxes: [MustacheBox]) -> MustacheBox {
var index = 0
return Box { (key: String) in
if key == cycleKey {
let box = boxes[index]
index = (index + 1) % boxes.count

This blog post shows how Swift protocols can help using CoreData: http://martiancraft.com/blog/2015/07/objective-c-swift-core-data/

Following this advice, I have tried this technique, with success, but with a request for improvement.

Below you will find a bare minimal view of the API I'm working on. Notice the not nice Person.self in the line db.fetchAll(Person.self, "SELECT * FROM persons"). I want to improve that, and the linked blog post was a great deal of inspiration.

// Library
class RowModel {} // Equivalent of NSManagedObject in Core Data
class Database {
// Library code
class Model { }
protocol Fetchable {
typealias FetchableType
static func fetchAll(sql: String) -> [FetchableType]
}
extension Fetchable where Self : Model, FetchableType == Self {
static func fetchAll(sql: String) -> [FetchableType] {
return [] // bare minimal, for demonstration purpose
// Library code
class Model { }
protocol Fetchable {
typealias FetchableType = Self
static func fetchAll(sql: String) -> [FetchableType]
}
extension Fetchable where Self : Model, FetchableType == Self {
static func fetchAll(sql: String) -> [FetchableType] {
return [] // bare minimal, for demonstration purpose
// Library code
class Model { }
protocol Fetchable {
typealias FetchableType = Self
static func fetchAll(sql: String) -> [FetchableType]
}
extension Fetchable where Self : Model, FetchableType == Self {
static func fetchAll(sql: String) -> [FetchableType] {
return [] // bare minimal, for demonstration purpose
// helper function for silentValueForKey
id silentValueForUndefinedKey(id self, SEL _cmd, NSString *key) {
return nil;
}
// The purpose of this function is to have the same result as
// [object valueForKey:key], but instead of letting [NSObject valueForUndefinedKey:]
// raise an NSUndefinedKeyException, it silently returns nil instead.
id silentValueForKey(id object, NSString *key) {
// Does object provide the same implementations of valueForKey: and valueForUndefinedKey: as NSObject?
layout.mustache:
<html>
<head><title>{{{$yield title}}}</title></head>
<body>
{{>navigation}}
<div class="content">
{{{$yield}}}
</div>
{{>footer}}
</body>
super.mustache:
<html>
<head><title>{{{title}}}</title></head>
<body>
{{>navigation}}
<div class="content">
{{{content}}}
</div>
{{>footer}}
</body>
@groue
groue / Document.mustache
Created September 22, 2012 11:03
GRMustache: variable tag that expand into a template string
{{#movie}}
{{link}}
{{#director}}
by {{link}}
{{/director}}
{{/movie}}
@groue
groue / _documentation.md
Created September 22, 2012 11:04
GRMustache: variable tag that renders a dynamic partial