Commit | Line | Data |
f29e9b6f |
1 | package App::IdiotBox::Bucket; |
2 | |
3 | use Moo; |
4 | |
5 | sub fields { return qw(slug name) } |
6 | |
7 | with 'App::IdiotBox::Clonable'; |
8 | |
9 | sub slug { shift->{slug} } |
10 | sub name { shift->{name} } |
11 | sub video_count { |
12 | exists $_[0]->{video_count} |
13 | ? $_[0]->{video_count} |
14 | : $_[0]->{videos}->count |
15 | } |
16 | sub videos { shift->{videos} } |
17 | |
18 | sub create { |
19 | my ($class, %args) = @_; |
20 | |
21 | unless ($args{name} && $args{slug}) { |
22 | return (undef, "Please enter a name and a bucket"); |
23 | } |
24 | if ($args{name} =~ /^\s+$/ || $args{slug} =~ /^\s+$/) { |
25 | return (undef, "Names/buckets must not be all whitespace"); |
26 | } |
27 | |
28 | $args{slug} =~ s/\s+/-/g; |
29 | |
30 | return bless { |
31 | name => $args{name}, |
32 | slug => $args{slug}, |
33 | }, $class; |
34 | } |
35 | |
36 | sub update { |
37 | my ($self, %args) = @_; |
38 | |
39 | unless ($args{name}) { |
40 | return (undef, "Please enter a new name"); |
41 | } |
42 | |
43 | if ($args{name} =~ /^\s+$/) { |
44 | return (undef, "Names must not be all whitespace"); |
45 | } |
46 | |
47 | $self->{name} = $args{name}; |
48 | |
49 | $self->clone; |
50 | } |
51 | |
52 | 1; |