From 5f49dd6953be5c59af3b985fe0666384f7c8d331 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sun, 14 Mar 2021 10:19:51 +0000 Subject: [PATCH] Moving stuff about/renaming --- Sources/HummingbirdMustache/Context.swift | 5 ++ Sources/HummingbirdMustache/Method.swift | 22 +----- Sources/HummingbirdMustache/Parent.swift | 14 ++++ Sources/HummingbirdMustache/Sequence.swift | 28 +++++++ .../HummingbirdMustache/Template+Render.swift | 74 +------------------ 5 files changed, 54 insertions(+), 89 deletions(-) create mode 100644 Sources/HummingbirdMustache/Parent.swift create mode 100644 Sources/HummingbirdMustache/Sequence.swift diff --git a/Sources/HummingbirdMustache/Context.swift b/Sources/HummingbirdMustache/Context.swift index 623d8e1..27744fd 100644 --- a/Sources/HummingbirdMustache/Context.swift +++ b/Sources/HummingbirdMustache/Context.swift @@ -2,4 +2,9 @@ struct HBMustacheContext { let first: Bool let last: Bool + + init(first: Bool = false, last: Bool = false) { + self.first = first + self.last = last + } } diff --git a/Sources/HummingbirdMustache/Method.swift b/Sources/HummingbirdMustache/Method.swift index c59efe0..14e26f8 100644 --- a/Sources/HummingbirdMustache/Method.swift +++ b/Sources/HummingbirdMustache/Method.swift @@ -1,23 +1,9 @@ -protocol HBMustacheBaseMethods { +protocol HBMustacheMethods { func runMethod(_ name: String) -> Any? } -protocol HBMustacheMethods { - typealias Method = (Self) -> Any - static var methods: [String: Method] { get set } -} -extension HBMustacheMethods { - static func addMethod(named name: String, method: @escaping Method) { - Self.methods[name] = method - } - func runMethod(_ name: String) -> Any? { - guard let method = Self.methods[name] else { return nil } - return method(self) - } -} - -extension String: HBMustacheBaseMethods { +extension String: HBMustacheMethods { func runMethod(_ name: String) -> Any? { switch name { case "lowercased": @@ -30,7 +16,7 @@ extension String: HBMustacheBaseMethods { } } -extension Array: HBMustacheBaseMethods { +extension Array: HBMustacheMethods { func runMethod(_ name: String) -> Any? { switch name { case "reversed": @@ -43,7 +29,7 @@ extension Array: HBMustacheBaseMethods { } } -extension Dictionary: HBMustacheBaseMethods { +extension Dictionary: HBMustacheMethods { func runMethod(_ name: String) -> Any? { switch name { case "enumerated": diff --git a/Sources/HummingbirdMustache/Parent.swift b/Sources/HummingbirdMustache/Parent.swift new file mode 100644 index 0000000..a315e59 --- /dev/null +++ b/Sources/HummingbirdMustache/Parent.swift @@ -0,0 +1,14 @@ + +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] } +} + diff --git a/Sources/HummingbirdMustache/Sequence.swift b/Sources/HummingbirdMustache/Sequence.swift new file mode 100644 index 0000000..ee73da1 --- /dev/null +++ b/Sources/HummingbirdMustache/Sequence.swift @@ -0,0 +1,28 @@ + +protocol HBMustacheSequence { + func renderSection(with template: HBMustacheTemplate) -> String + func renderInvertedSection(with template: HBMustacheTemplate) -> String +} + +extension Sequence { + func renderSection(with template: HBMustacheTemplate) -> String { + var string = "" + for obj in self { + string += template.render(obj) + } + return string + } + + func renderInvertedSection(with template: HBMustacheTemplate) -> String { + var iterator = makeIterator() + if iterator.next() == nil { + return template.render(self) + } + return "" + } + +} + +extension Array: HBMustacheSequence {} +extension ReversedCollection: HBMustacheSequence {} +extension EnumeratedSequence: HBMustacheSequence {} diff --git a/Sources/HummingbirdMustache/Template+Render.swift b/Sources/HummingbirdMustache/Template+Render.swift index 3d5be7a..7854828 100644 --- a/Sources/HummingbirdMustache/Template+Render.swift +++ b/Sources/HummingbirdMustache/Template+Render.swift @@ -37,7 +37,7 @@ extension HBMustacheTemplate { func renderSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String { switch child { - case let array as HBSequence: + case let array as HBMustacheSequence: return array.renderSection(with: template) case let bool as Bool: return bool ? template.render(parent) : "" @@ -52,7 +52,7 @@ extension HBMustacheTemplate { func renderInvertedSection(_ child: Any?, parent: Any, with template: HBMustacheTemplate) -> String { switch child { - case let array as HBSequence: + case let array as HBMustacheSequence: return array.renderInvertedSection(with: template) case let bool as Bool: return bool ? "" : template.render(parent) @@ -86,7 +86,7 @@ extension HBMustacheTemplate { child = _getChild(named: nameSplit[...], from: object) } if let method = method, - let runnable = child as? HBMustacheBaseMethods { + let runnable = child as? HBMustacheMethods { if let result = runnable.runMethod(method) { return result } @@ -113,71 +113,3 @@ extension HBMustacheTemplate { } } -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) -> String - func renderInvertedSection(with template: HBMustacheTemplate) -> String -} - -extension Array: HBSequence { - func renderSection(with template: HBMustacheTemplate) -> String { - var string = "" - for obj in self { - string += template.render(obj) - } - return string - } - - func renderInvertedSection(with template: HBMustacheTemplate) -> String { - if count == 0 { - return template.render(self) - } - return "" - } -} - -extension ReversedCollection: HBSequence { - func renderSection(with template: HBMustacheTemplate) -> String { - var string = "" - for obj in self { - string += template.render(obj) - } - return string - } - - func renderInvertedSection(with template: HBMustacheTemplate) -> String { - if count == 0 { - return template.render(self) - } - return "" - } -} - -extension EnumeratedSequence: HBSequence { - func renderSection(with template: HBMustacheTemplate) -> String { - var string = "" - for obj in self { - string += template.render(obj) - } - return string - } - - func renderInvertedSection(with template: HBMustacheTemplate) -> String { - if self.underestimatedCount == 0 { - return template.render(self) - } - return "" - } -}