diff --git a/lib/Routes/Examples/Template-Node1.rakumod b/lib/Routes/Examples/Template-Node1.rakumod index eb8a21a..c53c037 100644 --- a/lib/Routes/Examples/Template-Node1.rakumod +++ b/lib/Routes/Examples/Template-Node1.rakumod @@ -1,6 +1,113 @@ use Cro::HTTP::Router; use Cro::WebApp::Template; +#some exploratory code to do node behaviour +role Node { + has Str $.id is rw; # Unique ID for the node + has Node $.parent is rw; + has Node @.children is rw; + +method add-child(Node $child) { + $child.parent = self; + $child.id = self.child-id($child); + @!children.push($child); +} + +method remove-child(Node $child) { + @!children .= grep({ $_ !=:= $child }); + $child.parent = Nil; + $child.id = Nil; + self.update-child-ids; # Ensure remaining children have correct IDs +} + +# Update IDs for all children based on their position +method update-child-ids { + for @!children.kv -> $index, $child { + $child.id = self.id ~ '/' ~ ($index + 1); + $child.update-child-ids; # Recursively update grandchildren + } +} + +# Generate a path-based unique ID for a child +method child-id(Node $child --> Str) { + return self.id ~ '/' ~ (@!children.elems); +} + +method call-on-children(Str $method, *@args) { + @!children.map: { + $_."$method"(|@args) if $_.can($method); + } +} + +method call-deepmap(Str $method, Int :$indent = 4 --> Str) { + my $content = self."$method"(); + my $child-content = @!children.map({ + $_.call-deepmap($method, :$indent) + }).join("\n"); + return $content ~ ($child-content ?? "\n" ~ $child-content.indent($indent) !! ''); +} + +# Find a node by path +method find(Str $path --> Node) { + return self if $path eq self.id; + for @!children -> $child { + my $result = $child.find($path); + return $result if $result; + } + Nil; +} + +method Str { + self.id ~ ':' ~ self.name; +} +} + +class TreeNode does Node { + has Str $.name; + + method body { + q|