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 | |
f29e9b6f |
9 | sub name { shift->{name} } |
2da7f57f |
10 | sub slug { shift->{slug} } |
11 | sub videos { shift->{videos} } |
12 | #has 'name' => (is => 'rw'); |
13 | #has 'slug' => (is => 'rw'); |
14 | #has 'videos' => (is => 'rw'); |
15 | has '_video_count' => (is => 'rw'); |
16 | |
f29e9b6f |
17 | sub video_count { |
2da7f57f |
18 | my $self = shift; |
19 | |
20 | return $self->_video_count if defined $self->_video_count; |
21 | |
22 | return $self->{video_count} if defined $self->{video_count}; |
23 | |
24 | return $self->videos->count; |
f29e9b6f |
25 | } |
f29e9b6f |
26 | |
27 | sub create { |
28 | my ($class, %args) = @_; |
29 | |
30 | unless ($args{name} && $args{slug}) { |
31 | return (undef, "Please enter a name and a bucket"); |
32 | } |
33 | if ($args{name} =~ /^\s+$/ || $args{slug} =~ /^\s+$/) { |
34 | return (undef, "Names/buckets must not be all whitespace"); |
35 | } |
36 | |
37 | $args{slug} =~ s/\s+/-/g; |
38 | |
2da7f57f |
39 | return App::IdiotBox::Bucket->new( |
f29e9b6f |
40 | name => $args{name}, |
41 | slug => $args{slug}, |
2da7f57f |
42 | ); |
f29e9b6f |
43 | } |
44 | |
45 | sub update { |
46 | my ($self, %args) = @_; |
47 | |
48 | unless ($args{name}) { |
49 | return (undef, "Please enter a new name"); |
50 | } |
51 | |
52 | if ($args{name} =~ /^\s+$/) { |
53 | return (undef, "Names must not be all whitespace"); |
54 | } |
55 | |
2da7f57f |
56 | my $new = $self->clone; |
57 | |
58 | $new->{name} = $args{name}; |
f29e9b6f |
59 | |
2da7f57f |
60 | return $new; |
f29e9b6f |
61 | } |
62 | |
63 | 1; |