-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConfigIntializable.swift
49 lines (41 loc) · 1.55 KB
/
ConfigIntializable.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
import Foundation
/// Allows to intialize a new object without parameters or with configuration
public protocol ConfigInitializable {
init()
}
extension ConfigInitializable {
public typealias Config = Configurator<Self>
/// Instantiates a new object with specified configuration
///
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
@inlinable
public init(config configuration: (Config) -> Config) {
self.init(config: configuration(Config()))
}
/// Instantiates a new object with specified configuration
///
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
public init(config configurator: Config) {
self = configurator.configured(.init())
}
}
public protocol ConfigInitializableNSObject: NSObjectProtocol {}
extension ConfigInitializableNSObject where Self: NSObject {
public typealias Config = Configurator<Self>
/// Instantiates a new object with specified configuration
///
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
@inlinable
public init(config configuration: (Config) -> Config) {
self.init(config: configuration(Config()))
}
/// Instantiates a new object with specified configuration
///
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
@inlinable
public init(config configurator: Config) {
self.init()
configurator.configure(self)
}
}
extension NSObject: ConfigInitializableNSObject {}