-
Notifications
You must be signed in to change notification settings - Fork 2
/
StoryboardDependencyInjection.ejs
58 lines (54 loc) · 1.9 KB
/
StoryboardDependencyInjection.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<%
// 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 %>
<% }) -%>
}
}
<% }) %>