-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGTStorableToolkit.swift
166 lines (132 loc) · 6.03 KB
/
GTStorableToolkit.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
//
// GTStorableToolkit.swift
// GTFileStorable
//
// Created by Gabriel Theodoropoulos.
// Copyright © 2019 Gabriel Theodoropoulos. All rights reserved.
//
// MIT License
//
// Copyright (c) 2019 Gabriel Theodoropoulos
//
// 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
/**
This class defines custom types which are being used along with the
`GTStorable` protocol.
*/
public class GTStorableToolkit {
/**
Supported file types for saving objects conforming to `GTStorable`.
*/
public enum FileType {
/// Encode and save data as JSON.
case json
/// Encode and save data as a Property List (plist).
case plist
/// Encode and save data using the `NSKeyedArchiver` class.
case archive
/**
It returns the file extension based on the current file type value.
*/
public func fileExtension() -> String {
return "\(self)"
}
}
/**
A convenient enumeration to specify a target directory.
Supported values are `documents` and `caches`. To define subdirectories
use instances of the `StoreOptions` struct.
*/
public enum Directory {
/// The documents directory of the app.
case documents
/// The caches directory of the app. Usually used for temporary files.
case caches
/**
The URL to directory.
*/
public func url() -> URL {
return self == .documents ? FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] : FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
}
}
/**
Create instances of this struct and use its properties for providing additional
data regarding paths and files that are managed through the `GTStorable` protocol.
Instances of this protocol are usually passed as arguments to `GTStorable` methods.
*/
public struct StoreOptions {
/// The target directory to use. Default value is `documents directory`.
public var directory: Directory = .documents
/// One or more subdirectories to be appended to `directory`. Leave it
/// `nil` if you don't want to use any.
public var subDirectory: String?
/// Set a custom file name for files managed by the `GTStorable` methods.
///
/// By leaving it `nil` the type of the object adopting the `GTStorable`
/// protocol is used as the default file name.
public var customFilename: String?
/// Specify a custom extension to files managed by the `GTStorable` methods.
///
/// Leave it `nil` to have the file type specify the file extension automatically.
public var customExtension: String?
/// When `true` the subdirectories specified by the `subDirectory` property are
/// being created automatically if they don't exist. Default value is `true`.
public var createSubDir = true
/**
An empty initializer. Assign values to properties manually.
*/
public init() { }
/**
Custom initializer which accepts the working directory only as an argument.
*/
public init(withDirectory directory: Directory) {
self.directory = directory
}
/**
Custom initializer which accepts initial values for all properties as arguments.
*/
public init(withDirectory directory: Directory, subDirectory: String? = nil, createDirIfNecessary: Bool, customFilename: String? = nil, customExtension: String? = nil) {
self.directory = directory
self.subDirectory = subDirectory
self.createSubDir = createDirIfNecessary
self.customFilename = customFilename
self.customExtension = customExtension
}
}
/**
Use instances of this struct to pass customized JSON and Property List
encoders and decoders to the various methods of the `GTStorable`
protocol.
*/
public struct CustomCoders {
/// Initialize and customize a `JSONEncoder` object to be used
/// for encoding items conforming to `GTStorable` protocol.
public var jsonEncoder: JSONEncoder?
/// Initialize and customize a `JSONDecoder` object to be used
/// for decoding items conforming to `GTStorable` protocol.
public var jsonDecoder: JSONDecoder?
/// Initialize and customize a `PropertyListEncoder` object to be used
/// for encoding items conforming to `GTStorable` protocol.
public var plistEncoder: PropertyListEncoder?
/// Initialize and customize a `PropertyListDecoder` object to be used
/// for encoding items conforming to `GTStorable` protocol.
public var plistDecoder: PropertyListDecoder?
}
}