Skip to content

Instantly share code, notes, and snippets.

@DanielCardonaRojas
Last active January 9, 2020 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielCardonaRojas/90d7dbe357db79e70049b3eea76e44a1 to your computer and use it in GitHub Desktop.
Save DanielCardonaRojas/90d7dbe357db79e70049b3eea76e44a1 to your computer and use it in GitHub Desktop.
Builder pattern
import Foundation
public typealias Adapter<T> = (inout T) -> Void
public class BuilderHandler<V> {
let adapter: Adapter<V>
public init(_ handler: @escaping Adapter<V>) {
self.adapter = handler
}
}
public protocol Builder {
typealias Block = Adapter<Self>
typealias AdapterBlock = BuilderHandler<Self>
}
extension NSObject: Builder {}
extension Builder {
public func with(_ configure: Block) -> Self {
var this = self
configure(&this)
return this
}
public func with(_ configure: AdapterBlock) -> Self {
var this = self
configure.adapter(&this)
return this
}
public func combining(_ handlers: AdapterBlock...) -> Self {
var this = self
for h in handlers {
h.adapter(&this)
}
return this
}
}
protocol Builder {
typealias Handler = (inout Self) -> Void
}
//extension NSObject: Builder {}
extension NSObject: Builder {}
extension Builder {
public func with(_ configure: Handler) -> Self {
var this = self
configure(&this)
return this
}
}
extension Builder where Self: UIView {
func usingAutoLayout() -> Self {
self.translatesAutoresizingMaskIntoConstraints = false
return self
}
func userInteractionDisabled() -> Self {
self.isUserInteractionEnabled = false
return self
}
func userInteractionEnabled() -> Self {
self.isUserInteractionEnabled = true
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment