3 use Web::Simple __PACKAGE__;
4 use Method::Signatures::Simple;
9 package App::IdiotBox::Announcement;
11 sub made_at { shift->{made_at} }
12 sub bucket { shift->{bucket} }
13 sub video_count { shift->{video_count} }
15 package App::IdiotBox::Bucket;
17 sub slug { shift->{slug} }
18 sub name { shift->{name} }
20 exists $_[0]->{video_count}
21 ? $_[0]->{video_count}
22 : $_[0]->{videos}->count
24 sub videos { shift->{videos} }
26 package App::IdiotBox::Video;
28 sub slug { shift->{slug} }
29 sub name { shift->{name} }
30 sub author { shift->{author} }
31 sub details { shift->{details} }
32 sub bucket { shift->{bucket} }
36 template_dir => 'share/html',
38 db_file => 'var/lib/idiotbox.db',
44 ($store = $self->config->{store}) =~ /^(\w+)$/
45 or die "Store config should be just a name, got ${store} instead";
46 my $store_class = "App::IdiotBox::Store::${store}";
47 eval "require ${store_class}; 1"
48 or die "Couldn't load ${store} store: $@";
49 $store_class->bind($self);
53 sub (/) { $self->show_front_page },
54 subdispatch sub (/*/...) {
55 my $bucket = $self->buckets->get({ slug => $_[1] });
58 $self->show_bucket($bucket)
61 $self->show_video($bucket->videos->get({ slug => $_[1] }));
67 method recent_announcements { $self->{recent_announcements} }
69 method buckets { $self->{buckets} }
71 method show_front_page {
72 my $ann = $self->recent_announcements;
75 $_->select('#announcement-list')
76 ->repeat_content($ann->map(sub {
79 $_->select('.bucket-name')->replace_content($obj->bucket->name)
80 ->select('.bucket-link')->set_attribute({
81 name => 'href', value => $obj->bucket->slug.'/'
83 ->select('.new-videos')->replace_content($obj->video_count)
84 ->select('.total-videos')->replace_content(
85 $obj->bucket->video_count
93 method show_bucket ($bucket) {
94 $self->html_response(bucket => sub {
95 $_->select('.bucket-name')->replace_content($bucket->name)
96 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
99 $_->select('.video-name')->replace_content($video->name)
100 ->select('.video-author')->replace_content($video->author)
101 ->select('.video-link')->set_attribute(
102 { name => 'href', value => $video->slug.'/' }
109 method show_video ($video) {
110 $self->html_response(video => sub {
111 $_->select('.video-name')->replace_content($video->name)
112 ->select('.author-name')->replace_content($video->author)
113 ->select('.bucket-link')->set_attribute(
114 { name => 'href', value => '../' }
116 ->select('.bucket-name')->replace_content($video->bucket->name)
117 ->select('.video-details')->replace_content($video->details)
121 method html_response ($template_name, $selectors) {
122 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
123 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
126 method _template_filename_for ($name) {
127 $self->{config}{template_dir}.'/'.$name.'.html';
130 method _layout_zoom {
131 $self->{layout_zoom} ||= HTML::Zoom->from_file(
132 $self->_template_filename_for('layout')
136 method _zoom_for ($template_name, $selectors) {
137 ($self->{zoom_for_template}{$template_name} ||= do {
139 HTML::Zoom->from_file(
140 $self->_template_filename_for($template_name)
142 ->select('#main-content')->collect_content({ into => \@body })
145 ->select('#main-content')->replace_content(\@body)
147 })->apply($selectors);