Separate out App::IdiotBox::* DB objects and put create/update
[catagits/App-IdiotBox.git] / lib / App / IdiotBox / Bucket.pm
diff --git a/lib/App/IdiotBox/Bucket.pm b/lib/App/IdiotBox/Bucket.pm
new file mode 100644 (file)
index 0000000..9af2507
--- /dev/null
@@ -0,0 +1,52 @@
+package App::IdiotBox::Bucket;
+
+use Moo;
+
+sub fields { return qw(slug name) }
+
+with 'App::IdiotBox::Clonable';
+
+sub slug { shift->{slug} }
+sub name { shift->{name} }
+sub video_count {
+  exists $_[0]->{video_count}
+    ? $_[0]->{video_count}
+    : $_[0]->{videos}->count
+}
+sub videos { shift->{videos} }
+
+sub create {
+       my ($class, %args) = @_;
+
+       unless ($args{name} && $args{slug}) {
+               return (undef, "Please enter a name and a bucket");
+       }
+       if ($args{name} =~ /^\s+$/ || $args{slug} =~ /^\s+$/) {
+               return (undef, "Names/buckets must not be all whitespace");
+       }
+
+        $args{slug} =~ s/\s+/-/g;
+
+       return bless {
+               name => $args{name},
+               slug => $args{slug},
+       }, $class;
+}
+
+sub update {
+       my ($self, %args) = @_;
+
+       unless ($args{name}) {
+               return (undef, "Please enter a new name");
+       }
+
+       if ($args{name} =~ /^\s+$/) {
+               return (undef, "Names must not be all whitespace");
+       }
+
+       $self->{name} = $args{name};
+
+       $self->clone;
+}
+
+1;