Skip to content

Commit f1e2b11

Browse files
committed
feat(Builder): Scope methods
1 parent 30e15de commit f1e2b11

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Sources/FunctionalConfigurator/Configurator.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ extension Configurator {
148148
}
149149
}
150150

151+
public func scope(
152+
_ configuration: @escaping (Configurator<Value>) -> Configurator<Value>
153+
) -> Configurator {
154+
_block.configurator.appendingConfiguration { base in
155+
_block.keyPath.embed(
156+
modification(of: _block.keyPath.extract(from: base), with: configuration),
157+
in: base
158+
)
159+
}
160+
}
161+
151162
public subscript<LocalValue>(
152163
dynamicMember keyPath: WritableKeyPath<Value, LocalValue>
153164
) -> CallableBlock<LocalValue> {

Tests/DeclarativeConfigurationTests/BuilderTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,27 @@ final class BuilderTests: XCTestCase {
5656

5757
XCTAssertEqual(flag, false, "Reinforce transform wasn't called")
5858
}
59+
60+
func testScope() {
61+
struct Container: BuilderProvider, Equatable {
62+
struct Content: Equatable {
63+
var a: Int = 0
64+
var b: Int = 0
65+
var c: Int = 0
66+
}
67+
var content: Content = .init()
68+
}
69+
70+
let expected = Container(content: .init(a: 1, b: 2, c: 3))
71+
let initial = Container()
72+
let actual = initial.builder
73+
.content.scope { $0
74+
.a(1)
75+
.b(2)
76+
.c(3)
77+
}.build()
78+
79+
XCTAssertNotEqual(initial, expected)
80+
XCTAssertEqual(actual, expected)
81+
}
5982
}

0 commit comments

Comments
 (0)