refactor: TemplateLoader to protocol, follow Swift API guidelines

This commit is contained in:
Kyle Fuller
2016-11-28 03:50:53 +00:00
parent 5ca1b78854
commit 429290e0b7
9 changed files with 65 additions and 34 deletions

View File

@@ -2,8 +2,29 @@ import Foundation
import PathKit
public protocol TemplateLoader {
func loadTemplate(name: String) throws -> Template?
func loadTemplate(names: [String]) throws -> Template?
}
extension TemplateLoader {
func loadTemplate(names: [String]) throws -> Template? {
for name in names {
let template = try loadTemplate(name: name)
if template != nil {
return template
}
}
return nil
}
}
// A class for loading a template from disk
public class TemplateLoader {
public class FileSystemLoader: TemplateLoader {
public let paths: [Path]
public init(paths: [Path]) {
@@ -16,19 +37,25 @@ public class TemplateLoader {
}
}
public func loadTemplate(_ templateName: String) -> Template? {
return loadTemplate([templateName])
public func loadTemplate(name: String) throws -> Template? {
for path in paths {
let templatePath = path + Path(name)
if templatePath.exists {
return try Template(path: templatePath)
}
}
return nil
}
public func loadTemplate(_ templateNames: [String]) -> Template? {
public func loadTemplate(names: [String]) throws -> Template? {
for path in paths {
for templateName in templateNames {
for templateName in names {
let templatePath = path + Path(templateName)
if templatePath.exists {
if let template = try? Template(path: templatePath) {
return template
}
return try Template(path: templatePath)
}
}
}