Skip to content

Commit 7a51150

Browse files
committed
feat: ConfigInitializable protocol added
1 parent 8e2e98e commit 7a51150

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,11 @@ class ImageViewController: UIViewController {
4444
.layer.cornerRadius(10)
4545
}
4646

47-
let imageView: UIImageView = .init()
47+
let imageView = UIImageView(config: StyleSheet.imageView)
4848

4949
override func loadView() {
5050
self.view = imageView
5151
}
52-
53-
override func viewDidLoad() {
54-
super.viewDidLoad()
55-
StyleSheet.imageView.configure(imageView)
56-
}
5752
}
5853
```
5954

@@ -118,7 +113,7 @@ If you use SwiftPM for your project, you can add DeclarativeConfiguration to you
118113
```swift
119114
.package(
120115
url: "git@github.com:makeupstudio/swift-declarative-configuration.git",
121-
from: "0.0.1"
116+
from: "0.0.2"
122117
)
123118
```
124119

@@ -133,4 +128,4 @@ Do not forget about target dependencies:
133128

134129
## License
135130

136-
This library is released under the MIT license. See [LICENSE](./LICENSE) for details.
131+
This library is released under the MIT license. See [LICENSE](./LICENSE) for details.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Foundation
2+
3+
/// Allows to intialize a new object without parameters or with configuration
4+
public protocol ConfigInitializable {
5+
init()
6+
}
7+
8+
extension ConfigInitializable {
9+
public typealias Config = Configurator<Self>
10+
11+
/// Instantiates a new object with specified configuration
12+
///
13+
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
14+
public init(_ configuration: (Config.Type) -> Config) {
15+
self.init(configuration(Config.self))
16+
}
17+
18+
/// Instantiates a new object with specified configuration
19+
///
20+
/// Note: Type must implement custom intializer with no parameters, even if it inherits from NSObject
21+
public init(_ configurator: Config) {
22+
self = configurator.configure(.init())
23+
}
24+
}
25+
26+
extension NSObject: ConfigInitializable {}

Sources/FunctionalConfigurator/Configurator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public struct Configurator<Base> {
7373
extension Configurator {
7474
@dynamicMemberLookup
7575
public struct CallableBlock<Value> {
76-
private var _block: NonCallableBlock<Value>
76+
var _block: NonCallableBlock<Value>
7777

7878
init(
7979
configurator: Configurator,

Tests/FunctionalConfigurationTests/ConfiguratorTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,44 @@ final class ConfiguratorTests: XCTestCase {
2828
XCTAssertNotEqual(actual, initial)
2929
XCTAssertEqual(actual, expected)
3030
}
31+
32+
33+
func testConfigInitializable() {
34+
class TestConfigurable: NSObject {
35+
override init() { // required init
36+
super.init()
37+
}
38+
39+
init(value: Bool, wrapped: TestConfigurable.Wrapped) {
40+
self.value = value
41+
self.wrapped = wrapped
42+
}
43+
44+
struct Wrapped: Equatable {
45+
var value = 0
46+
}
47+
48+
var value = false
49+
var wrapped = Wrapped()
50+
}
51+
52+
let initial = TestConfigurable()
53+
let expected = TestConfigurable(value: true, wrapped: .init(value: 1))
54+
let actual1 = TestConfigurable { $0
55+
.value(true)
56+
.wrapped(.init(value: 1))
57+
}
58+
let actual2 = TestConfigurable(
59+
TestConfigurable.Config
60+
.value(true)
61+
.wrapped(.init(value: 1))
62+
)
63+
64+
XCTAssertNotEqual(actual1.value, initial.value)
65+
XCTAssertNotEqual(actual1.wrapped, initial.wrapped)
66+
XCTAssertEqual(actual1.value, actual2.value)
67+
XCTAssertEqual(actual1.wrapped, actual2.wrapped)
68+
XCTAssertEqual(actual1.value, expected.value)
69+
XCTAssertEqual(actual1.wrapped, expected.wrapped)
70+
}
3171
}

0 commit comments

Comments
 (0)