Minor fixes to make updating actually work.
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Video.pm
CommitLineData
f29e9b6f 1package App::IdiotBox::Video;
2
3use Moo;
4
5sub fields { return qw(slug bucket_slug name author details announcement_id) }
6
56640bce 7with 'App::IdiotBox::Clonable';
8
9sub announcement_id { shift->{announcement_id} }
10sub bucket_slug { shift->{bucket_slug} }
f29e9b6f 11sub slug { shift->{slug} }
12sub name { shift->{name} }
13sub author { shift->{author} }
14sub details { shift->{details} }
15sub bucket { shift->{bucket} }
16sub file_name {
17 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
18 $s;
19}
20sub url_path {
21 join('/', $_[0]->bucket->slug, $_[0]->slug);
22}
23
2da7f57f 24sub 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
f29e9b6f 581;