Minor fixes to make updating actually work.
[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 with 'App::IdiotBox::Clonable';
8
9 sub announcement_id { shift->{announcement_id} }
10 sub bucket_slug { shift->{bucket_slug} }
11 sub slug { shift->{slug} }
12 sub name { shift->{name} }
13 sub author { shift->{author} }
14 sub details { shift->{details} }
15 sub bucket { shift->{bucket} }
16 sub file_name {
17   (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
18   $s;
19 }
20 sub url_path {
21   join('/', $_[0]->bucket->slug, $_[0]->slug);
22 }
23
24 sub update {
25         my ($self, %args) = @_;
26
27         my %update = map { $_ => delete $args{$_} } grep { exists $args{$_} } $self->fields;
28
29         if (%args) {
30                 return (undef, "Unknown fields in update: " . join(',', keys %args));
31         }
32
33         unless (%update) {
34                 return (undef, "No changes requested");
35         }
36
37         for my $k (qw(slug bucket_slug name author details)) {
38                 my @bad;
39
40                 if (exists $update{$k} && $update{$k} =~ /^\s+$/) {
41                         push @bad, $k;
42                 }
43
44                 return (undef, "Empty fields in update: ", join(',', @bad)) if @bad;
45         }
46
47         for my $k (qw(slug bucket_slug)) {
48                 $update{$k} =~ s/\s+/-/g if exists $update{$k};
49         }
50
51         my $new = $self->clone;
52
53         $new->{$_} = $update{$_} for keys %update;
54
55         return $new;
56 }
57
58 1;