front page rendering appears to work (given git zoom, dbixds and web-simple, anyway)
Matt S Trout [Mon, 18 Jan 2010 19:31:39 +0000 (19:31 +0000)]
lib/App/IdiotBox.pm
share/html/bucket.html
share/html/front_page.html
share/html/layout.html
t/show/front_page.t [new file with mode: 0644]

index 29452c0..4d880c7 100644 (file)
@@ -2,6 +2,26 @@ package App::IdiotBox;
 
 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 },
@@ -12,60 +32,118 @@ dispatch {
         $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;
index 4b67051..9609d88 100644 (file)
@@ -1,7 +1,8 @@
 <html>
 <body>
   <div id="main-content">
-    <ul class="video-list">
+    <h4 class="fill-bucket-name">THIS IZ MAI BUKKIT</h4>
+    <ul id="video-list">
       <li class="video">
         <a class="fill-video-link fill-video-name">Really Imaginative Video Name</a>
       </li>
index a2434df..a2bea84 100644 (file)
@@ -1,7 +1,7 @@
 <html>
 <body>
   <div id="main-content">
-    <ul class="announcement-list">
+    <ul id="announcement-list">
       <li class="announcement">
         <span class="fill-made-at">29th February 2010</span>
         <a class="fill-bucket-link">
index c5a071a..65c406a 100644 (file)
@@ -1,9 +1,10 @@
 <html>
-  <head>
-    <title>Idiot Box</title>
-  </head>
-  <body>
-    <h1 id="title">Idiot Box</h1>
-    <div id="main-content" />
-  </body>
+<head>
+  <title>Idiot Box</title>
+</head>
+<body>
+  <h1 id="title">Idiot Box</h1>
+  <div id="main-content">
+  </div>
+</body>
 </html>
diff --git a/t/show/front_page.t b/t/show/front_page.t
new file mode 100644 (file)
index 0000000..f7c67b3
--- /dev/null
@@ -0,0 +1,63 @@
+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 => [
+                    
+