Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Last active September 2, 2018 08:30
Show Gist options
  • Save marty-suzuki/8a6ab234835fd9fe0216c6ab971ec75f to your computer and use it in GitHub Desktop.
Save marty-suzuki/8a6ab234835fd9fe0216c6ab971ec75f to your computer and use it in GitHub Desktop.
@testable import 'Your Target Name'
import UIKit
import Foundation
/// Define this protocol in your target
// protocol AutoPrivatePropertyAccessible {}
// MARK: - PrivatePropertyAccessible
protocol PrivatePropertyAccessible {
associatedtype PrivatePropertiesCompatible
var privateProperties: PrivateProperties<PrivatePropertiesCompatible> { get }
}
struct PrivateProperties<Base> {
fileprivate let base: Base
init(_ base: Base) {
self.base = base
}
}
extension PrivatePropertyAccessible where PrivatePropertiesCompatible == Self {
var privateProperties: PrivateProperties<Self> {
return PrivateProperties(self)
}
}
{% for type in types.implementing.AutoPrivatePropertyAccessible %}
// MARK: - {{ type.name }}
extension {{type.name}}: PrivatePropertyAccessible {}
extension PrivateProperties where Base == {{type.name}} {
{% for variable in type.storedVariables where variable.readAccess == "private" or variable.readAccess == "fileprivate" %}
var {{variable.name}}: {{variable.typeName}} {
return base.value(forKey: "{{variable.name}}") {% if variable.isOptional %}as? {{variable.unwrappedTypeName}}{% else %}as! {{variable.unwrappedTypeName}}{% endif %}
}
{% endfor %}
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment