Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (54 sloc) 1.9 KB
<%
// Usage:
// 1. Add the following protocol to your project:
// protocol StoryboardInitializable where Self: UIViewController {
// static func instantiateFromStoryboard() -> Self
// }
//
// 2. Conform view controllers to it.
// 3. Use this template with Sourcery: https://github.com/krzysztofzablocki/Sourcery
//
//
// Author: Arek Holko, MIT LICENSE
// https://github.com/fastred/StoryboardDependencyInjectionTemplates
-%>
<% types.implementing.StoryboardInitializable.forEach(function(type){ -%>
// MARK: - <%= type.name -%> - StoryboardDependencyInjection
extension <%= type.name -%> {
<%
var allProperties = [];
var currentType = type;
while (currentType) {
allProperties.push.apply(allProperties, currentType.storedVariables);
currentType = currentType.supertype;
}
var properties = allProperties.filter(function(property) {
return property.isImplicitlyUnwrappedOptional &&
!property.attributes.IBOutlet &&
["internal", "public", "open"].indexOf(property.writeAccess) > -1
})
-%>
static func makeFromStoryboard(
<% properties.forEach(function(property, index){ -%>
<%= property.name -%>: <%= property.typeName.unwrappedTypeName -%><% if (index !== properties.length - 1) { %>,<% } %>
<% }) -%>
) -> <%= type.name -%> {
let viewController = <%= type.name %>.instantiateFromStoryboard()
viewController.setDependencies(
<% properties.forEach(function(property, index){ -%>
<%= property.name -%>: <%= property.name -%><% if (index !== properties.length - 1) { %>,<% } %>
<% }) -%>
)
return viewController
}
func setDependencies(
<% properties.forEach(function(property, index){ -%>
<%= property.name -%>: <%= property.typeName.unwrappedTypeName -%><% if (index !== properties.length - 1) { %>,<% } %>
<% }) -%>
) {
<% properties.forEach(function(property){ -%>
self.<%= property.name %> = <%= property.name %>
<% }) -%>
}
}
<% }) %>