-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ForEachWeavable and other updates
- Loading branch information
1 parent
5c4952b
commit 66139d2
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
public struct ForEachWeavable: Directive { | ||
private var objects: [ObjectWeavable] = [] | ||
|
||
var include: Bool = true | ||
var skip: Bool = false | ||
|
||
init<T>(_ array: [T], content: @escaping (T) -> ObjectWeavable) { | ||
array.forEach { | ||
objects.append(content($0)) | ||
} | ||
} | ||
} | ||
|
||
public extension ForEachWeavable { | ||
/** | ||
Only include this object in the operation if the argument is true. | ||
- Parameter argument: A boolean argument. | ||
- Returns: An `Object` with its include value set. | ||
*/ | ||
func include(if argument: Bool) -> ForEachWeavable { | ||
var copy = self | ||
copy.include = argument | ||
return copy | ||
} | ||
|
||
/** | ||
Skip this object if the argument is true | ||
- Parameter argument: A boolean argument. | ||
- Returns: An `Object` with its skip value set. | ||
*/ | ||
func skip(if argument: Bool) -> ForEachWeavable { | ||
var copy = self | ||
copy.skip = argument | ||
return copy | ||
} | ||
} | ||
|
||
extension ForEachWeavable: ObjectWeavable { | ||
public var description: String { | ||
objects.map { $0.description }.joined(separator: " ") | ||
} | ||
|
||
public var debugDescription: String { | ||
objects.map { $0.debugDescription }.joined(separator: " ") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters