Skip to content

Commit 89a4236

Browse files
author
Fernando Corrêa de Oliveira
committed
redirect
1 parent 9f73780 commit 89a4236

File tree

7 files changed

+48
-37
lines changed

7 files changed

+48
-37
lines changed

README.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ has Todo @.todos;
5151

5252
method RENDER($_) {
5353
.ol: {
54-
for @!todos -> Todo $todo {
55-
.add-child: $todo
56-
}
54+
.add-children: @!todos;
5755
}
5856
.form:
5957
:endpoint(self.new-todo),
@@ -66,40 +64,55 @@ method RENDER($_) {
6664
method new-todo(Str :$description!)
6765
is endpoint{
6866
:path</bla>,
69-
:return(-> | { boilerplate :title("My TODO list"), { .add-child: TodoList.new } })
67+
:redirect</>,
7068
} {
7169
@!todos.push: Todo.new: :$description;
7270
}
7371
```
7472

7573
```raku
76-
# humming-bird-todo.raku
74+
# examples/App.rakumod
75+
use TodoList;
76+
use HTML::Component;
77+
use HTML::Component::Boilerplate;
78+
unit class App does HTML::Component;
79+
80+
method RENDER($?) {
81+
boilerplate
82+
:title("My TODO list"),
83+
*.add-child: TodoList.new
84+
}
85+
```
86+
87+
```raku
88+
# examples/humming-bird-todo.raku
7789
use v6.d;
7890

7991
use Humming-Bird::Core;
8092
use HTML::Component::Boilerplate;
8193
use lib "examples";
8294
use TodoList;
8395
use Todo;
96+
use App;
8497
use HTML::Component::Endpoint;
8598

86-
my $index = boilerplate :title("My TODO list"), { .add-child: TodoList.new }
99+
my $index = App.new.RENDER;
87100
my $html = $index.HTML;
88101

89102
get('/', -> $request, $response {
90-
$response.html($html);
103+
$response.html(App.new.RENDER.HTML);
91104
});
92105

93106
for HTML::Component::Endpoint.endpoints {
94107
if .verb.uc eq "GET" {
95108
get .path, -> $request, $response {
96-
$response.html: .run-defined(Any, |$request.query<>)
109+
$response.html: .run-defined(Any, |$request.query<>).Str;
110+
$response.redirect: $_ with .redirect;
97111
}
98112
}
99113
}
100114

101115
listen(12345);
102-
103116
```
104117

105118
# DESCRIPTION

examples/App.rakumod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use TodoList;
2+
use HTML::Component;
3+
use HTML::Component::Boilerplate;
4+
unit class App does HTML::Component;
5+
6+
method RENDER($?) {
7+
boilerplate
8+
:title("My TODO list"),
9+
*.add-child: TodoList.new
10+
}

examples/TodoList.rakumod

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ has Todo @.todos;
1212

1313
method RENDER($_) {
1414
.ol: {
15-
for @!todos -> Todo $todo {
16-
.add-child: $todo
17-
}
15+
.add-children: @!todos;
1816
}
1917
.form:
2018
:endpoint(self.new-todo),
@@ -27,7 +25,7 @@ method RENDER($_) {
2725
method new-todo(Str :$description!)
2826
is endpoint{
2927
:path</bla>,
30-
:return(-> | { boilerplate :title("My TODO list"), { .add-child: TodoList.new } })
28+
:redirect</>,
3129
} {
3230
@!todos.push: Todo.new: :$description;
3331
}

examples/humming-bird-todo.raku

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ use HTML::Component::Boilerplate;
55
use lib "examples";
66
use TodoList;
77
use Todo;
8+
use App;
89
use HTML::Component::Endpoint;
910

10-
my $index = boilerplate :title("My TODO list"), { .add-child: TodoList.new }
11+
my $index = App.new.RENDER;
1112
my $html = $index.HTML;
1213

1314
get('/', -> $request, $response {
14-
$response.html($html);
15+
$response.html(App.new.RENDER.HTML);
1516
});
1617

1718
for HTML::Component::Endpoint.endpoints {
1819
if .verb.uc eq "GET" {
1920
get .path, -> $request, $response {
20-
$response.html: .run-defined(Any, |$request.query<>)
21+
$response.html: .run-defined(Any, |$request.query<>).Str;
22+
$response.redirect: $_ with .redirect;
2123
}
2224
}
2325
}

lib/HTML/Component.rakumod

-20
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,8 @@ sub html(|c) is export {
55
::("HTML::Component::Tag::HTML").new: |c;
66
}
77

8-
# sub body(&func, *%_ where { .keys.all ~~ BODY.&all-html-attrs.any }) is export {
9-
# my $body = BODY.new: |%_;
10-
# $body.&func;
11-
# $body
12-
# }
13-
14-
#class SNIPPET does OnOl does OnBody does OnHTML does OnHead does Node {}
15-
16-
# sub snippet(&func) is export {
17-
# #do given func SNIPPET.new { .children.head }
18-
# func SNIPPET.new # for now
19-
# }
20-
218
method HTML {
229
my $*HTML-COMPONENT-RENDERING = True;
2310
$.RENDER: CALLERS::<self>;
2411
Empty
2512
}
26-
27-
# method render-root {
28-
# html -> HTML::Component $root { self.RENDER: $root }
29-
# }
30-
# method render {
31-
# snippet(-> HTML::Components $root { self.RENDER: $root }).RENDER
32-
# }

lib/HTML/Component/Endpoint.rakumod

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ has $.load-meth = "LOAD";
1313
has Bool $.undefined = &!method.signature.params.first.modifier ne ":D";
1414
has Bool $.defined = &!method.signature.params.first.modifier ne ":U";
1515
has &.return = -> :$component, :$method-output { $method-output };
16+
has $.redirect;
1617

1718
submethod TWEAK(|) {
1819
@endpoints.push: self
@@ -48,4 +49,3 @@ multi trait_mod:<is>(Method $method, :%endpoint (Str() :$path!, |)) is export {
4849
nextsame
4950
}
5051
}
51-

lib/HTML/Component/Tag/Node.rakumod

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ multi method new(&body, *%_) {
1212
$obj
1313
}
1414

15+
multi method add-children(+@components) {
16+
$.add-child: $_ for @components
17+
}
18+
19+
multi method add-child(@components) {
20+
$.add-child: $_ for @components
21+
}
22+
1523
multi method add-child(HTML::Component $comp) {
1624
@!children.push: $comp;
1725
$comp

0 commit comments

Comments
 (0)