use Web::Simple __PACKAGE__;
use Method::Signatures::Simple;
+use FindBin;
+use HTML::Zoom;
+
+{
+ package App::IdiotBox::Announcement;
+
+ sub made_at { shift->{made_at} }
+ sub bucket { shift->{bucket} }
+ sub video_count { shift->{video_count} }
+
+ package App::IdiotBox::Bucket;
+
+ sub slug { shift->{slug} }
+ sub name { shift->{name} }
+ sub video_count { shift->{video_count} }
+}
+
+default_config(
+ template_dir => $FindBin::Bin.'/../share/html'
+);
dispatch {
sub (/) { $self->show_front_page },
$self->show_bucket($bucket)
},
sub (/*) {
- $self->show_video($bucket->videos->get({ slug => $_[1] });
+ $self->show_video($bucket->videos->get({ slug => $_[1] }));
}
]
}
};
+method recent_announcements { $self->{recent_announcements} }
+
method show_front_page {
my $ann = $self->recent_announcements;
$self->html_response(
front_page => [
- '#announcement-list' => {
- -repeat => {
- data => $ann->map(sub { +{
- '#fill-bucket-name' => { -replace_content => $_->bucket->name },
- '#fill-bucket-link' => {
- -set_attribute => { name => 'href', value => '/'.$_->slug.'/' }
- },
- '#fill-new-videos' => $_->videos->count,
- '#fill-total-videos' => $_->bucket->videos->count,
- } })
+ '#announcement-list' => [
+ -repeat_content => {
+ repeat_for => $ann->map(sub { [
+ '.fill-bucket-name' => [
+ -replace_content => { replace_with => $_->bucket->name }
+ ],
+ '.fill-bucket-link' => [
+ -set_attribute => { name => 'href', value => $_->bucket->slug.'/' }
+ ],
+ '.fill-new-videos' => [
+ -replace_content => { replace_with => $_->video_count }
+ ],
+ '.fill-total-videos' => [
+ -replace_content => { replace_with => $_->bucket->video_count }
+ ],
+ ] })->as_stream
}
- }
+ ]
]
);
}
+method show_bucket ($bucket) {
+ $self->html_response(bucket => [
+ '.fill-bucket-name' => [
+ -replace_content => { replace_with => $bucket->name }
+ ],
+ '#video-list' => [
+ -repeat_content => {
+ repeat_for => $bucket->videos->map(sub { [
+ '.fill-video-name' => [
+ -replace_content => { replace_with => $_->name }
+ ],
+ '.fill-video-author' => [
+ -replace_content => { replace_with => $_->author }
+ ],
+ '.fill-video-link' => [
+ -set_attribute => {
+ name => 'href', value => $_->slug.'/'
+ }
+ ],
+ ] })->as_stream
+ }
+ ]
+ ]);
+}
+
+method show_video ($video) {
+ $self->html_response(video => [
+ '.fill-video-name' => [
+ -replace_content => { replace_with => $video->name }
+ ],
+ '.fill-author-name' => [
+ -replace_content => { replace_with => $video->author }
+ ],
+ '.fill-bucket-link' => [
+ -set_attribute => { name => 'href', value => '../' }
+ ],
+ '.fill-bucket-name' => [
+ -replace_content => { replace_with => $video->bucket->name }
+ ],
+ '.fill-video-details' => [
+ -replace_content => { replace_with => $video->details }
+ ]
+ ]);
+}
+
method html_response ($template_name, $selectors) {
- my $io = $self->_zoom_for($template_name => $selectors)->as_io;
+ my $io = $self->_zoom_for($template_name => $selectors)->as_readable_fh;
return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
}
+method _template_filename_for ($name) {
+ $self->{config}{template_dir}.'/'.$name.'.html';
+}
+
method _layout_zoom {
- $self->{layout_zoom} ||= HTML::Zoom->from_filename(
- $self->_teamplate_filename_for('layout')
+ $self->{layout_zoom} ||= HTML::Zoom->from_file(
+ $self->_template_filename_for('layout')
)
}
method _zoom_for ($template_name, $selectors) {
($self->{zoom_for_template}{$template_name} ||= do {
my @body;
- HTML::Zoom->from_filename(
- $self->_template_filename_for($template_name);
+ HTML::Zoom->from_file(
+ $self->_template_filename_for($template_name)
)
->with_selectors(
- '#main-content' => { -capture_events_into => \@body }
+ '#main-content' => [
+ -capture_events => { into => \@body }
+ ]
)
- ->to_bit_bucket;
- my @all = $self->_layout_zoom->with_selectors(
- '#main-content' => {
- -replace_content_events => \@body
- }
- )->to_event_array;
- HTML::Zoom->from_events(\@all)
- })->with_selectors(@$selectors)
+ ->run;
+ $self->_layout_zoom->with_selectors(
+ '#main-content' => [
+ -replace_content_events => { replace_with => \@body }
+ ]
+ )->to_zoom;
+ })->with_selectors($selectors)
}
1;
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use Test::More;
+
+use App::IdiotBox;
+use Data::Perl::Collection::Set;
+
+my $idiotbox = App::IdiotBox->new({
+ config => { template_dir => 'share/html' }
+});
+
+my $ann = do {
+ my ($lpw, $opw) = map bless($_, 'App::IdiotBox::Bucket'),
+ { slug => 'lpw2009',
+ name => 'London Perl Workshop 2009',
+ video_count => 18 },
+ { slug => 'opw2010',
+ name => 'Perl Oasis 2010',
+ video_count => 6 };
+ my @ann = map bless($_, 'App::IdiotBox::Announcement'),
+ {
+ made_at => '2010-01-21 01:00:00',
+ bucket => $opw,
+ video_count => 3 },
+ {
+ made_at => '2010-01-01 14:00:00',
+ bucket => $lpw,
+ video_count => 5 };
+ Data::Perl::Collection::Set->new(members => [ @ann ]);
+};
+
+$idiotbox->{recent_announcements} = $ann;
+
+use Devel::Dwarn;
+
+#Dwarn
+
+my $front = $idiotbox->show_front_page;
+
+my $html = do {
+ my $string;
+ my $fh = $front->[-1];
+ while (defined (my $chunk = $fh->getline)) {
+ $string .= $chunk;
+ }
+ $string;
+};
+
+warn $html;
+
+pass;
+
+done_testing;
+
+#my %test;
+
+#HTML::Zoom->from_string($html)
+# ->with_selectors({
+# '.announcement' => [
+# -sub_selectors => {
+# selectors => [
+
+