Iniitial IdiotBox.pm and script/idiotbox
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
1 package App::IdiotBox;
2
3 use Web::Simple __PACKAGE__;
4 use Method::Signatures::Simple;
5
6 dispatch {
7   sub (/) { $self->show_front_page },
8   subdispatch sub (/*/...) {
9     my $bucket = $self->buckets->get({ slug => $_[1] });
10     [
11       sub (/) {
12         $self->show_bucket($bucket)
13       },
14       sub (/*) {
15         $self->show_video($bucket->videos->get({ slug => $_[1] });
16       }
17     ]
18   }
19 };
20
21 method show_front_page {
22   my $ann = $self->recent_announcements;
23   $self->html_response(
24     front_page => [
25       '#announcement-list' => {
26         -repeat => {
27           data => $ann->map(sub { +{
28             '#fill-bucket-name' => { -replace_content => $_->bucket->name },
29             '#fill-bucket-link' => {
30               -set_attribute => { name => 'href', value => '/'.$_->slug.'/' }
31             },
32             '#fill-new-videos' => $_->videos->count,
33             '#fill-total-videos' => $_->bucket->videos->count,
34           } })
35         }
36       }
37     ]
38   );
39 }
40
41 method html_response ($template_name, $selectors) {
42   my $io = $self->_zoom_for($template_name => $selectors)->as_io;
43   return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
44 }
45
46 method _layout_zoom {
47   $self->{layout_zoom} ||= HTML::Zoom->from_filename(
48     $self->_teamplate_filename_for('layout')
49   )
50 }
51
52 method _zoom_for ($template_name, $selectors) {
53   ($self->{zoom_for_template}{$template_name} ||= do {
54     my @body;
55     HTML::Zoom->from_filename(
56                   $self->_template_filename_for($template_name);
57                 )
58               ->with_selectors(
59                 '#main-content' => { -capture_events_into => \@body }
60                 )
61               ->to_bit_bucket;
62     my @all = $self->_layout_zoom->with_selectors(
63       '#main-content' => {
64         -replace_content_events => \@body
65       }
66     )->to_event_array;
67     HTML::Zoom->from_events(\@all)
68   })->with_selectors(@$selectors)
69 }
70
71 1;