New admin UI functionality.
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Video.pm
1 package App::IdiotBox::Video;
2
3 use Moo;
4
5 sub fields { return qw(slug bucket_slug name author details announcement_id) }
6
7 sub slug { shift->{slug} }
8 sub name { shift->{name} }
9 sub author { shift->{author} }
10 sub details { shift->{details} }
11 sub bucket { shift->{bucket} }
12 sub file_name {
13   (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
14   $s;
15 }
16 sub url_path {
17   join('/', $_[0]->bucket->slug, $_[0]->slug);
18 }
19
20 sub update {
21         my ($self, %args) = @_;
22
23         my %update = map { $_ => delete $args{$_} } grep { exists $args{$_} } $self->fields;
24
25         if (%args) {
26                 return (undef, "Unknown fields in update: " . join(',', keys %args));
27         }
28
29         unless (%update) {
30                 return (undef, "No changes requested");
31         }
32
33         for my $k (qw(slug bucket_slug name author details)) {
34                 my @bad;
35
36                 if (exists $update{$k} && $update{$k} =~ /^\s+$/) {
37                         push @bad, $k;
38                 }
39
40                 return (undef, "Empty fields in update: ", join(',', @bad)) if @bad;
41         }
42
43         for my $k (qw(slug bucket_slug)) {
44                 $update{$k} =~ s/\s+/-/g if exists $update{$k};
45         }
46
47         my $new = $self->clone;
48
49         $new->{$_} = $update{$_} for keys %update;
50
51         return $new;
52 }
53
54 1;