Moving stuff about, renaming

This commit is contained in:
Adam Fowler
2021-03-12 09:55:12 +00:00
parent 02af0ec296
commit 633f494e18
5 changed files with 59 additions and 55 deletions

View File

@@ -8,10 +8,14 @@ public class HBMustacheLibrary {
templates[name] = template
}
public func getTemplate(named name: String) -> HBMustacheTemplate? {
templates[name]
}
public func render(_ object: Any, withTemplateNamed name: String) -> String? {
guard let template = templates[name] else { return nil }
return template.render(object, library: self)
}
var templates: [String: HBMustacheTemplate]
private var templates: [String: HBMustacheTemplate]
}

View File

@@ -0,0 +1,17 @@
func unwrapOptional(_ object: Any) -> Any? {
let mirror = Mirror(reflecting: object)
guard mirror.displayStyle == .optional else { return object }
guard let first = mirror.children.first else { return nil }
return first.value
}
extension Mirror {
func getValue(forKey key: String) -> Any? {
guard let matched = children.filter({ $0.label == key }).first else {
return nil
}
return unwrapOptional(matched.value)
}
}

View File

@@ -1,35 +0,0 @@
protocol HBMustacheParent {
func child(named: String) -> Any?
}
extension HBMustacheParent {
// default child to nil
func child(named: String) -> Any? { return nil }
}
extension Dictionary: HBMustacheParent where Key == String {
func child(named: String) -> Any? { return self[named] }
}
protocol HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
}
extension Array: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
if count == 0 {
return template.render(self, library: library)
}
return ""
}
}

View File

@@ -69,7 +69,7 @@ extension HBMustacheTemplate {
childObject = customBox.child(named: name)
} else {
let mirror = Mirror(reflecting: object)
childObject = mirror.getAttribute(forKey: name)
childObject = mirror.getValue(forKey: name)
}
guard childObject != nil else { return nil }
let names2 = names.dropFirst()
@@ -100,19 +100,37 @@ extension HBMustacheTemplate {
}
}
func unwrap(_ any: Any) -> Any? {
let mirror = Mirror(reflecting: any)
guard mirror.displayStyle == .optional else { return any }
guard let first = mirror.children.first else { return nil }
return first.value
protocol HBMustacheParent {
func child(named: String) -> Any?
}
extension Mirror {
func getAttribute(forKey key: String) -> Any? {
guard let matched = children.filter({ $0.label == key }).first else {
return nil
}
return unwrap(matched.value)
}
extension HBMustacheParent {
// default child to nil
func child(named: String) -> Any? { return nil }
}
extension Dictionary: HBMustacheParent where Key == String {
func child(named: String) -> Any? { return self[named] }
}
protocol HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String
}
extension Array: HBSequence {
func renderSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
var string = ""
for obj in self {
string += template.render(obj, library: library)
}
return string
}
func renderInvertedSection(with template: HBMustacheTemplate, library: HBMustacheLibrary?) -> String {
if count == 0 {
return template.render(self, library: library)
}
return ""
}
}