Skip to content

Commit

Permalink
add FISetter (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytyubox authored Mar 10, 2020
1 parent 617aa5d commit 52a3b53
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions Sources/fluentinterface/FluentInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public postfix func - <T>(lhs: FluentInterface<T>) -> T {
/// ```
/// resource https://www.appcoda.com.tw/fluent-interface/
@dynamicMemberLookup public struct FluentInterface<Subject> {

public typealias FISetter<Value> = ((Value) -> FluentInterface<Subject>)

let subject: Subject

// 因為要動到 subject 的屬性,所以 keyPath 的型別必須是 WritableKeyPath
Expand All @@ -43,20 +46,20 @@ public postfix func - <T>(lhs: FluentInterface<T>) -> T {
/// [Flentinterface] get the subject.
public func unwrappingSubject() -> Subject {
subject
}
/// [Fluentinterface] Quick way to touch subject and remain fluent interface
/// - Parameter handlel: A cloure to get the subject
public nonmutating func handlingSubject(_ handle:
(Subject) -> Void) -> Self {
handle(subject)
return self
}
/// [Fluentinterface] Quick way to manipulate subject and remain fluent interface
/// - Parameter handle: A cloure to inout set subject
public nonmutating func manipulateSubjct(_ handle:
(inout Subject) -> Void) -> Self {
var subject = self.subject
handle(&subject)
return FluentInterface(subject: subject)
}
}
/// [Fluentinterface] Quick way to touch subject and remain fluent interface
/// - Parameter handlel: A cloure to get the subject
public nonmutating func handlingSubject(_ handle:
(Subject) -> Void) -> Self {
handle(subject)
return self
}
/// [Fluentinterface] Quick way to manipulate subject and remain fluent interface
/// - Parameter handle: A cloure to inout set subject
public nonmutating func manipulateSubjct(_ handle:
(inout Subject) -> Void) -> Self {
var subject = self.subject
handle(&subject)
return FluentInterface(subject: subject)
}
}

0 comments on commit 52a3b53

Please sign in to comment.