main code repository.
Revision | 4ca717f30ce91cc8428b01e823d730a321bc4f84 (tree) |
---|---|
Time | 2017-03-31 23:02:18 |
Author | masakih <masakih@user...> |
Commiter | masakih |
CoreDataCoreのcoreをstaticに変更
@@ -11,24 +11,22 @@ import Cocoa | ||
11 | 11 | extension CoreDataConfiguration { |
12 | 12 | static let bookmark = CoreDataConfiguration("Bookmark") |
13 | 13 | } |
14 | -extension CoreDataCore { | |
15 | - static let bookmark = CoreDataCore(.bookmark) | |
16 | -} | |
17 | 14 | |
18 | 15 | class BookmarkDataStore: CoreDataAccessor, CoreDataManager { |
19 | - static var `default` = BookmarkDataStore(type: .reader) | |
16 | + static let core = CoreDataCore(.bookmark) | |
17 | + | |
18 | + static let `default` = BookmarkDataStore(type: .reader) | |
20 | 19 | class func oneTimeEditor() -> BookmarkDataStore { |
21 | 20 | return BookmarkDataStore(type: .editor) |
22 | 21 | } |
23 | 22 | |
24 | 23 | required init(type: CoreDataManagerType) { |
25 | - context = (type == .reader ? core.parentContext : core.editorContext()) | |
24 | + context = BookmarkDataStore.context(for: type) | |
26 | 25 | } |
27 | 26 | deinit { |
28 | 27 | save() |
29 | 28 | } |
30 | 29 | |
31 | - let core = CoreDataCore.bookmark | |
32 | 30 | let context: NSManagedObjectContext |
33 | 31 | } |
34 | 32 |
@@ -67,7 +67,7 @@ struct CoreDataCore { | ||
67 | 67 | // MARK: - protocol |
68 | 68 | protocol CoreDataProvider { |
69 | 69 | init(type: CoreDataManagerType) |
70 | - var core: CoreDataCore { get } | |
70 | + static var core: CoreDataCore { get } | |
71 | 71 | var context: NSManagedObjectContext { get } |
72 | 72 | func save() |
73 | 73 | func removeDataFile() |
@@ -108,7 +108,7 @@ extension CoreDataProvider { | ||
108 | 108 | } |
109 | 109 | } |
110 | 110 | func removeDataFile() { |
111 | - remove(name: core.config.fileName) | |
111 | + remove(name: type(of: self).core.config.fileName) | |
112 | 112 | } |
113 | 113 | private func presentOnMainThread(_ error: Error) { |
114 | 114 | if Thread.isMainThread { |
@@ -119,6 +119,15 @@ extension CoreDataProvider { | ||
119 | 119 | } |
120 | 120 | } |
121 | 121 | } |
122 | + | |
123 | + static func context(for type: CoreDataManagerType) -> NSManagedObjectContext { | |
124 | + switch type { | |
125 | + case .reader: | |
126 | + return core.parentContext | |
127 | + case .editor: | |
128 | + return core.editorContext() | |
129 | + } | |
130 | + } | |
122 | 131 | } |
123 | 132 | |
124 | 133 | extension CoreDataAccessor { |
@@ -11,25 +11,22 @@ import Cocoa | ||
11 | 11 | extension CoreDataConfiguration { |
12 | 12 | static let local = CoreDataConfiguration("LocalData") |
13 | 13 | } |
14 | -extension CoreDataCore { | |
15 | - static let local = CoreDataCore(.local) | |
16 | -} | |
17 | - | |
18 | 14 | |
19 | 15 | class LocalDataStore: CoreDataAccessor, CoreDataManager { |
20 | - static var `default` = LocalDataStore(type: .reader) | |
16 | + static let core = CoreDataCore(.local) | |
17 | + | |
18 | + static let `default` = LocalDataStore(type: .reader) | |
21 | 19 | class func oneTimeEditor() -> LocalDataStore { |
22 | 20 | return LocalDataStore(type: .editor) |
23 | 21 | } |
24 | 22 | |
25 | 23 | required init(type: CoreDataManagerType) { |
26 | - context = (type == .reader ? core.parentContext : core.editorContext()) | |
24 | + context = LocalDataStore.context(for: type) | |
27 | 25 | } |
28 | 26 | deinit { |
29 | 27 | save() |
30 | 28 | } |
31 | 29 | |
32 | - let core = CoreDataCore.local | |
33 | 30 | let context: NSManagedObjectContext |
34 | 31 | } |
35 | 32 |
@@ -11,24 +11,22 @@ import Cocoa | ||
11 | 11 | extension CoreDataConfiguration { |
12 | 12 | static let resourceHistory = CoreDataConfiguration("ResourceHistory") |
13 | 13 | } |
14 | -extension CoreDataCore { | |
15 | - static let resourceHistory = CoreDataCore(.resourceHistory) | |
16 | -} | |
17 | 14 | |
18 | 15 | class ResourceHistoryDataStore: CoreDataAccessor, CoreDataManager { |
19 | - static var `default` = ResourceHistoryDataStore(type: .reader) | |
16 | + static let core = CoreDataCore(.resourceHistory) | |
17 | + | |
18 | + static let `default` = ResourceHistoryDataStore(type: .reader) | |
20 | 19 | class func oneTimeEditor() -> ResourceHistoryDataStore { |
21 | 20 | return ResourceHistoryDataStore(type: .editor) |
22 | 21 | } |
23 | 22 | |
24 | 23 | required init(type: CoreDataManagerType) { |
25 | - context = (type == .reader ? core.parentContext : core.editorContext()) | |
24 | + context = ResourceHistoryDataStore.context(for: type) | |
26 | 25 | } |
27 | 26 | deinit { |
28 | 27 | save() |
29 | 28 | } |
30 | 29 | |
31 | - let core = CoreDataCore.resourceHistory | |
32 | 30 | let context: NSManagedObjectContext |
33 | 31 | } |
34 | 32 |
@@ -11,24 +11,22 @@ import Cocoa | ||
11 | 11 | extension CoreDataConfiguration { |
12 | 12 | static let kcd = CoreDataConfiguration("KCD", tryRemake: true) |
13 | 13 | } |
14 | -extension CoreDataCore { | |
15 | - static let kcd = CoreDataCore(.kcd) | |
16 | -} | |
17 | 14 | |
18 | 15 | class ServerDataStore: CoreDataAccessor, CoreDataManager { |
19 | - static var `default` = ServerDataStore(type: .reader) | |
16 | + static let core = CoreDataCore(.kcd) | |
17 | + | |
18 | + static let `default` = ServerDataStore(type: .reader) | |
20 | 19 | class func oneTimeEditor() -> ServerDataStore { |
21 | 20 | return ServerDataStore(type: .editor) |
22 | 21 | } |
23 | 22 | |
24 | 23 | required init(type: CoreDataManagerType) { |
25 | - context = (type == .reader ? core.parentContext : core.editorContext()) | |
24 | + context = ServerDataStore.context(for: type) | |
26 | 25 | } |
27 | 26 | deinit { |
28 | 27 | save() |
29 | 28 | } |
30 | 29 | |
31 | - let core = CoreDataCore.kcd | |
32 | 30 | let context: NSManagedObjectContext |
33 | 31 | } |
34 | 32 |
@@ -15,24 +15,22 @@ extension CoreDataConfiguration { | ||
15 | 15 | type: NSInMemoryStoreType |
16 | 16 | ) |
17 | 17 | } |
18 | -extension CoreDataCore { | |
19 | - static let temporary = CoreDataCore(.temporary) | |
20 | -} | |
21 | 18 | |
22 | 19 | class TemporaryDataStore: CoreDataAccessor, CoreDataManager { |
23 | - static var `default` = TemporaryDataStore(type: .reader) | |
20 | + static let core = CoreDataCore(.temporary) | |
21 | + | |
22 | + static let `default` = TemporaryDataStore(type: .reader) | |
24 | 23 | class func oneTimeEditor() -> TemporaryDataStore { |
25 | 24 | return TemporaryDataStore(type: .editor) |
26 | 25 | } |
27 | 26 | |
28 | 27 | required init(type: CoreDataManagerType) { |
29 | - context = (type == .reader ? core.parentContext : core.editorContext()) | |
28 | + context = TemporaryDataStore.context(for: type) | |
30 | 29 | } |
31 | 30 | deinit { |
32 | 31 | save() |
33 | 32 | } |
34 | 33 | |
35 | - let core = CoreDataCore.temporary | |
36 | 34 | let context: NSManagedObjectContext |
37 | 35 | } |
38 | 36 |