Allow creating of new buckets by using the importer
Matthew Horsfall [Mon, 25 Jul 2011 17:25:34 +0000 (13:25 -0400)]
lib/App/IdiotBox/Importer.pm
lib/App/IdiotBox/Store/SQLite.pm

index a19451c..4600777 100644 (file)
@@ -18,30 +18,50 @@ sub run {
   my @buckets = $ib->buckets->flatten;
   my %bucket_by_slug;
   log_info { "Available buckets to import into:" };
-  foreach my $idx (0 .. $#buckets) {
-    my $bucket = $buckets[$idx];
-    $bucket_by_slug{$bucket->slug} = $bucket;
-    log_info { "(${idx}) ${\$bucket->slug} : ${\$bucket->name}" };
-  }
 
   my $bucket;
     
-  CHOOSE: {
-    my $choice = prompt("Which bucket to import into (by number or slug) ?");
-    if ($choice =~ /^\d+$/) {
-      $bucket = $buckets[$choice];
-    } else {
-      $bucket = $bucket_by_slug{$choice};
+  BUCKETS: {
+    foreach my $idx (0 .. $#buckets) {
+      my $bucket = $buckets[$idx];
+      $bucket_by_slug{$bucket->slug} = $bucket;
+      log_info { "(${idx}) ${\$bucket->slug} : ${\$bucket->name}" };
     }
-    unless ($bucket) {
-      log_info {
-         "No bucket for ${choice} - valid options are 0 to ${\$#buckets}"
-         ." or slug (e.g. ${\$buckets[0]->slug})"
-       };
-       redo CHOOSE;
+    log_info { sprintf("(%d) new : Create new bucket\n", $#buckets + 1) };
+
+    CHOOSE: {
+      my $choice = prompt("Which bucket to import into (by number or slug) ?");
+      if ($choice =~ /^\d+$/) {
+        $bucket = $buckets[$choice];
+      } else {
+        $bucket = $bucket_by_slug{$choice};
+      }
+
+      if (!$bucket && ($choice eq 'new' || $choice eq $#buckets + 1)) {
+        my $sn = prompt("What's the new short name (url path) for the slug ?");
+        my $ln = prompt("What's the new long name (description) for the slug ?");
+
+        $sn =~ s/ /-/g;
+
+        my $nb = $ib->buckets->add(bless({
+          slug => $sn,
+          name => $ln,
+        }, 'App::IdiotBox::Bucket'));
+
+        log_info { "Created new bucket" };
+        push @buckets, $nb;
+        redo BUCKETS;
+      }
+
+      unless ($bucket) {
+        log_info {
+           "No bucket for ${choice} - valid options are 0 to ${\$#buckets + 1}"
+           ." or slug (e.g. ${\$buckets[0]->slug})"
+         };
+         redo CHOOSE;
+      }
     }
   }
-
   my $ann = $ib->recent_announcements->add(bless({
     bucket => $bucket,
     made_at => strftime("%Y-%m-%d %H:%M:%S",localtime),
index dfd0949..104472b 100644 (file)
@@ -108,6 +108,7 @@ sub _bind_buckets {
     $_[0],
     App::IdiotBox::Inflator::Bucket->new({
       all_columns => [ qw(slug name) ],
+      body_columns => [ qw(slug name) ],
       spec_columns => [ qw(slug) ],
       class => 'App::IdiotBox::Bucket',
       raw => $_[0],
@@ -122,6 +123,13 @@ sub _bind_buckets {
         FROM buckets
         WHERE slug = ?
       },
+      insert_one => q{
+        INSERT INTO buckets
+          (slug, name)
+        VALUES
+          (?, ?)
+      }
+
     }
   )
 }