add comments
This commit is contained in:
parent
caf995c0b0
commit
6758aed034
@ -12,14 +12,16 @@ public protocol ParentModelProtocol: MoleculeTreeTraversalProtocol, AnyObject {
|
|||||||
|
|
||||||
var children: [MoleculeModelProtocol] { get }
|
var children: [MoleculeModelProtocol] { get }
|
||||||
|
|
||||||
|
/// Method for replacing surface level children. (Does not recurse.)
|
||||||
func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool
|
func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
public extension ParentModelProtocol {
|
public extension ParentModelProtocol {
|
||||||
|
|
||||||
/// Top level test to replace child molecules. Each parent molecule should attempt to replace.
|
/// Top level test to replace child molecules. Each parent molecule should attempt to replace.
|
||||||
func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool { return false }
|
func replaceChildMolecule(with molecule: MoleculeModelProtocol) throws -> Bool { return false }
|
||||||
|
|
||||||
/// Helper function for replacing molecules.
|
/// Helper function for replacing a single molecules with type and optionality checks.
|
||||||
func replaceChildMolecule<T>(at childMolecule: inout T, with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
func replaceChildMolecule<T>(at childMolecule: inout T, with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
||||||
guard let childIdMolecule = childMolecule as? MoleculeModelProtocol else { return false }
|
guard let childIdMolecule = childMolecule as? MoleculeModelProtocol else { return false }
|
||||||
if childIdMolecule.id == replacementMolecule.id {
|
if childIdMolecule.id == replacementMolecule.id {
|
||||||
@ -32,6 +34,7 @@ public extension ParentModelProtocol {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper for replacing a molecule in place within an array. Note the "in".
|
||||||
func replaceChildMolecule<T>(in molecules: inout [T], with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
func replaceChildMolecule<T>(in molecules: inout [T], with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
||||||
guard let moleculeIdModels = molecules as? [MoleculeModelProtocol], let matchingIndex = moleculeIdModels.firstIndex(where: {
|
guard let moleculeIdModels = molecules as? [MoleculeModelProtocol], let matchingIndex = moleculeIdModels.firstIndex(where: {
|
||||||
$0.id == replacementMolecule.id
|
$0.id == replacementMolecule.id
|
||||||
|
|||||||
@ -38,9 +38,10 @@ public extension TemplateModelProtocol {
|
|||||||
return rootMolecules.depthFirstTraverse(options: options, depth: depth, onVisit: onVisit)
|
return rootMolecules.depthFirstTraverse(options: options, depth: depth, onVisit: onVisit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Recursively finds and replaces the first child matching the replacement molecule id property.
|
||||||
func replaceMolecule(with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
func replaceMolecule(with replacementMolecule: MoleculeModelProtocol) throws -> Bool {
|
||||||
// Attempt root level replacement on the template model first.
|
// Attempt root level replacement on the template model first.
|
||||||
if try self.replaceChildMolecule(with: replacementMolecule) {
|
if try replaceChildMolecule(with: replacementMolecule) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user