forked from loganwright/Genome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contents.swift
236 lines (186 loc) · 5.49 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//: Playground - noun: a place where people can play
import UIKit
/**
If you are getting errors here, make sure you have built the frameworks.
Select `Genome-iOS` target
Set Simulator Device
Build w/ cmd + b
Once that's done you should be good to go.
You'll need to rebuild to access library changes here
*/
import Genome
import PureJsonSerializer
// MARK: Date Conversion
enum DateTransformationError : ErrorType {
case UnableToConvertString
}
extension NSDate {
class func dateWithBirthdayString(string: String) throws -> NSDate {
let df = NSDateFormatter()
df.dateFormat = "mm-d-yy"
if let date = df.dateFromString(string) {
return date
} else {
throw DateTransformationError.UnableToConvertString
}
}
class func birthdayStringWithDate(date: NSDate) throws -> String {
let df = NSDateFormatter()
df.dateFormat = "mm-d-yy"
return df.stringFromDate(date)
}
}
// MARK: Pet
enum PetType : String {
case Dog = "dog"
case Cat = "cat"
}
struct _Pet : MappableObject {
let name: String
let type: PetType
let nickname: String
init(map: Map) throws {
name = try map.extract("name")
nickname = try map.extract("nickname")
type = try map.extract("type") {
PetType(rawValue: $0)!
}
}
func sequence(map: Map) throws {
try name ~> map["name"]
try type ~> map["type"]
.transformToJson { $0.rawValue }
try nickname ~> map["nickname"]
}
}
struct Pet : BasicMappable {
var name: String!
var type: PetType!
var nickname: String?
mutating func sequence(map: Map) throws {
try name <~> map["name"]
try nickname <~> map["nickname"]
try type <~> map["type"]
.transformFromJson {
return PetType(rawValue: $0)
}
.transformToJson {
return $0.rawValue
}
}
}
let json_rover: Json = [
"name" : "Rover",
"nickname" : "RoRo",
"type" : "dog"
]
let rover = try Pet(js: json_rover)
print(rover)
// MARK: Person
struct Person : MappableObject {
let name: String
let pet: Pet
let birthday: NSDate
let favoriteFood: String?
init(map: Map) throws {
pet = try map.extract("pet")
name = try map.extract("name")
favoriteFood = try map.extract("favorite_food")
birthday = try map.extract("birthday", transformer: NSDate.dateWithBirthdayString)
}
mutating func sequence(map: Map) throws {
try name ~> map["name"]
try pet ~> map["pet"]
try favoriteFood ~> map["favorite_food"]
try birthday ~> map["birthday"]
.transformToJson(NSDate.birthdayStringWithDate)
}
}
let json_snowflake: Json = [
"name" : "Snowflake",
"type" : "cat"
]
let json_joe: Json = [
"name" : "Joe",
"birthday" : "12-15-84",
"pet" : json_snowflake
]
let joe = try Person(js: json_joe)
print(joe)
// MARK: Creating A Custom Mappable
extension Int {
static func fromString(string: String) -> Int {
return Int(string)!
}
}
extension String {
static func fromInt(int: Int) -> String {
return "\(int)"
}
}
class CustomBase : MappableBase {
required init() {}
static func newInstance(json: Json, context: Context) throws -> Self {
let map = Map(json: json, context: context)
let new = self.init()
try new.sequence(map)
return new
}
func sequence(map: Map) throws {}
}
// MARK: Complex Example
class Book : MappableBase {
var title: String = ""
var releaseYear: Int = 0
var id: String = ""
required init() {}
static func newInstance(json: Json, context: Context = EmptyJson) throws -> Self {
let map = Map(json: json, context: context)
return try newInstance(map)
}
static func newInstance(map: Map) throws -> Self {
let id: String = try map.extract("id")
let new = existingBookWithId(id) ?? self.init()
try new.sequence(map)
return new
}
func sequence(map: Map) throws {
try title <~> map["title"]
try id <~> map["id"]
// String => Int
try releaseYear <~> map["release_year"]
.transformFromJson(Int.fromString)
.transformToJson(String.fromInt)
}
}
func existingBookWithId<T: Book>(id: String) -> T? {
return nil
}
let json_book: Json = [
"title" : "Title",
"release_year" : "2009",
"id" : "asd9fj20m"
]
let book = try! Book.newInstance(json_book)
print(book)
// MARK: Core Data Example
import CoreData
extension NSManagedObjectContext : Context {}
public class NSMappableManagedObject: NSManagedObject, MappableBase {
public class var entityName: String {
return "\(self)"
}
public func sequence(map: Map) throws {
fatalError("Sequence must be overwritten")
}
public class func newInstance(json: Json, context: Context) throws -> Self {
return try newInstance(json, context: context, type: self)
}
public class func newInstance<T: NSMappableManagedObject>(json: Json, context: Context, type: T.Type) throws -> T {
let context = context as! NSManagedObjectContext
let new = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: context) as! T
let map = Map(json: json, context: context)
try new.sequence(map)
return new
}
}