HBMustacheMethods -> HBMustacheTransformable
HBMustacheContext -> HBMustacheSequenceContext
This commit is contained in:
Adam Fowler
2021-03-19 14:48:44 +00:00
parent 09f5c5953e
commit f9f8c1320a
4 changed files with 44 additions and 44 deletions

View File

@@ -11,7 +11,7 @@ public extension Sequence {
/// Render section using template /// Render section using template
func renderSection(with template: HBMustacheTemplate, stack: [Any]) -> String { func renderSection(with template: HBMustacheTemplate, stack: [Any]) -> String {
var string = "" var string = ""
var context = HBMustacheContext(first: true) var context = HBMustacheSequenceContext(first: true)
var iterator = makeIterator() var iterator = makeIterator()
guard var currentObject = iterator.next() else { return "" } guard var currentObject = iterator.next() else { return "" }

View File

@@ -1,6 +1,6 @@
/// Context that current object is being rendered in. Only really relevant when rendering a sequence /// Context that current object inside a sequence is being rendered in. Only relevant when rendering a sequence
struct HBMustacheContext: HBMustacheMethods { struct HBMustacheSequenceContext: HBMustacheTransformable {
var first: Bool var first: Bool
var last: Bool var last: Bool
var index: Int var index: Int
@@ -11,7 +11,7 @@ struct HBMustacheContext: HBMustacheMethods {
index = 0 index = 0
} }
/// Apply method to `HBMustacheContext`. These are available when processing elements /// Transform `HBMustacheContext`. These are available when processing elements
/// of a sequence. /// of a sequence.
/// ///
/// Format your mustache as follows to accept them. They look like a function without any arguments /// Format your mustache as follows to accept them. They look like a function without any arguments
@@ -19,10 +19,10 @@ struct HBMustacheContext: HBMustacheMethods {
/// {{#sequence}}{{index()}}{{/sequence}} /// {{#sequence}}{{index()}}{{/sequence}}
/// ``` /// ```
/// ///
/// Methods available are `first`, `last`, and `index` /// Transforms available are `first`, `last`, `index`, `even` and `odd`
/// - Parameter name: Method name /// - Parameter name: Method name
/// - Returns: Result /// - Returns: Result
func runMethod(_ name: String) -> Any? { func transform(_ name: String) -> Any? {
switch name { switch name {
case "first": case "first":
return first return first

View File

@@ -6,7 +6,7 @@ extension HBMustacheTemplate {
/// - context: Context that render is occurring in. Contains information about position in sequence /// - context: Context that render is occurring in. Contains information about position in sequence
/// - indentation: indentation of partial /// - indentation: indentation of partial
/// - Returns: Rendered text /// - Returns: Rendered text
func render(_ stack: [Any], context: HBMustacheContext? = nil, indentation: String? = nil) -> String { func render(_ stack: [Any], context: HBMustacheSequenceContext? = nil, indentation: String? = nil) -> String {
var string = "" var string = ""
if let indentation = indentation, indentation != "" { if let indentation = indentation, indentation != "" {
for token in tokens { for token in tokens {
@@ -23,7 +23,7 @@ extension HBMustacheTemplate {
return string return string
} }
func renderToken(_ token: Token, stack: [Any], context: HBMustacheContext? = nil) -> String { func renderToken(_ token: Token, stack: [Any], context: HBMustacheSequenceContext? = nil) -> String {
switch token { switch token {
case let .text(text): case let .text(text):
return text return text
@@ -96,7 +96,7 @@ extension HBMustacheTemplate {
} }
/// Get child object from variable name /// Get child object from variable name
func getChild(named name: String, from stack: [Any], method: String?, context: HBMustacheContext?) -> Any? { func getChild(named name: String, from stack: [Any], method: String?, context: HBMustacheSequenceContext?) -> Any? {
func _getImmediateChild(named name: String, from object: Any) -> Any? { func _getImmediateChild(named name: String, from object: Any) -> Any? {
if let customBox = object as? HBMustacheParent { if let customBox = object as? HBMustacheParent {
return customBox.child(named: name) return customBox.child(named: name)
@@ -139,9 +139,9 @@ extension HBMustacheTemplate {
// if we want to run a method and the current child can have methods applied to it then // if we want to run a method and the current child can have methods applied to it then
// run method on the current child // run method on the current child
if let method = method, if let method = method,
let runnable = child as? HBMustacheMethods let runnable = child as? HBMustacheTransformable
{ {
if let result = runnable.runMethod(method) { if let result = runnable.transform(method) {
return result return result
} }
} }

View File

@@ -13,17 +13,17 @@
/// ``` /// ```
/// {{#reversed(sequence)}}{{.}}{{\sequence}} /// {{#reversed(sequence)}}{{.}}{{\sequence}}
/// ``` /// ```
public protocol HBMustacheMethods { public protocol HBMustacheTransformable {
func runMethod(_ name: String) -> Any? func transform(_ name: String) -> Any?
} }
public extension StringProtocol { public extension StringProtocol {
/// Apply method to String/Substring /// Transform String/Substring
/// ///
/// Methods available are `capitalized`, `lowercased`, `uppercased` and `reversed` /// Transforms available are `capitalized`, `lowercased`, `uppercased` and `reversed`
/// - Parameter name: Method name /// - Parameter name: Method name
/// - Returns: Result /// - Returns: Result
func runMethod(_ name: String) -> Any? { func transform(_ name: String) -> Any? {
switch name { switch name {
case "capitalized": case "capitalized":
return capitalized return capitalized
@@ -39,22 +39,22 @@ public extension StringProtocol {
} }
} }
extension String: HBMustacheMethods {} extension String: HBMustacheTransformable {}
extension Substring: HBMustacheMethods {} extension Substring: HBMustacheTransformable {}
/// Protocol for sequence that can be sorted /// Protocol for sequence that can be sorted
private protocol HBComparableSequence { private protocol HBComparableSequence {
func runComparableMethod(_ name: String) -> Any? func comparableTransform(_ name: String) -> Any?
} }
extension Array: HBMustacheMethods { extension Array: HBMustacheTransformable {
/// Apply method to Array. /// Transform Array.
/// ///
/// Methods available are `first`, `last`, `reversed`, `count` and for arrays /// Transforms available are `first`, `last`, `reversed`, `count` and for arrays
/// with comparable elements `sorted`. /// with comparable elements `sorted`.
/// - Parameter name: method name /// - Parameter name: method name
/// - Returns: Result /// - Returns: Result
public func runMethod(_ name: String) -> Any? { public func transform(_ name: String) -> Any? {
switch name { switch name {
case "first": case "first":
return first return first
@@ -66,7 +66,7 @@ extension Array: HBMustacheMethods {
return count return count
default: default:
if let comparableSeq = self as? HBComparableSequence { if let comparableSeq = self as? HBComparableSequence {
return comparableSeq.runComparableMethod(name) return comparableSeq.comparableTransform(name)
} }
return nil return nil
} }
@@ -74,7 +74,7 @@ extension Array: HBMustacheMethods {
} }
extension Array: HBComparableSequence where Element: Comparable { extension Array: HBComparableSequence where Element: Comparable {
func runComparableMethod(_ name: String) -> Any? { func comparableTransform(_ name: String) -> Any? {
switch name { switch name {
case "sorted": case "sorted":
return sorted() return sorted()
@@ -84,14 +84,14 @@ extension Array: HBComparableSequence where Element: Comparable {
} }
} }
extension Dictionary: HBMustacheMethods { extension Dictionary: HBMustacheTransformable {
/// Apply method to Dictionary /// Transform Dictionary
/// ///
/// Methods available are `count`, `enumerated` and for dictionaries /// Transforms available are `count`, `enumerated` and for dictionaries
/// with comparable keys `sorted`. /// with comparable keys `sorted`.
/// - Parameter name: method name /// - Parameter name: method name
/// - Returns: Result /// - Returns: Result
public func runMethod(_ name: String) -> Any? { public func transform(_ name: String) -> Any? {
switch name { switch name {
case "count": case "count":
return count return count
@@ -99,7 +99,7 @@ extension Dictionary: HBMustacheMethods {
return map { (key: $0.key, value: $0.value) } return map { (key: $0.key, value: $0.value) }
default: default:
if let comparableSeq = self as? HBComparableSequence { if let comparableSeq = self as? HBComparableSequence {
return comparableSeq.runComparableMethod(name) return comparableSeq.comparableTransform(name)
} }
return nil return nil
} }
@@ -107,7 +107,7 @@ extension Dictionary: HBMustacheMethods {
} }
extension Dictionary: HBComparableSequence where Key: Comparable { extension Dictionary: HBComparableSequence where Key: Comparable {
func runComparableMethod(_ name: String) -> Any? { func comparableTransform(_ name: String) -> Any? {
switch name { switch name {
case "sorted": case "sorted":
return map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key } return map { (key: $0.key, value: $0.value) }.sorted { $0.key < $1.key }
@@ -118,12 +118,12 @@ extension Dictionary: HBComparableSequence where Key: Comparable {
} }
public extension FixedWidthInteger { public extension FixedWidthInteger {
/// Apply method to FixedWidthInteger /// Transform FixedWidthInteger
/// ///
/// Methods available are `plusone`, `minusone`, `odd`, `even` /// Transforms available are `plusone`, `minusone`, `odd`, `even`
/// - Parameter name: method name /// - Parameter name: method name
/// - Returns: Result /// - Returns: Result
func runMethod(_ name: String) -> Any? { func transform(_ name: String) -> Any? {
switch name { switch name {
case "plusone": case "plusone":
return self + 1 return self + 1
@@ -139,13 +139,13 @@ public extension FixedWidthInteger {
} }
} }
extension Int: HBMustacheMethods {} extension Int: HBMustacheTransformable {}
extension Int8: HBMustacheMethods {} extension Int8: HBMustacheTransformable {}
extension Int16: HBMustacheMethods {} extension Int16: HBMustacheTransformable {}
extension Int32: HBMustacheMethods {} extension Int32: HBMustacheTransformable {}
extension Int64: HBMustacheMethods {} extension Int64: HBMustacheTransformable {}
extension UInt: HBMustacheMethods {} extension UInt: HBMustacheTransformable {}
extension UInt8: HBMustacheMethods {} extension UInt8: HBMustacheTransformable {}
extension UInt16: HBMustacheMethods {} extension UInt16: HBMustacheTransformable {}
extension UInt32: HBMustacheMethods {} extension UInt32: HBMustacheTransformable {}
extension UInt64: HBMustacheMethods {} extension UInt64: HBMustacheTransformable {}