A better YAML producer, actually using the YAML module. Updated test
Darren Chamberlain [Wed, 8 Oct 2003 17:27:40 +0000 (17:27 +0000)]
lib/SQL/Translator/Producer/YAML.pm
t/24yaml.t

index 12a08c5..bfaa7ed 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Producer::YAML;
 
 # -------------------------------------------------------------------
-# $Id: YAML.pm,v 1.1 2003-10-08 16:33:13 dlc Exp $
+# $Id: YAML.pm,v 1.2 2003-10-08 17:27:40 dlc Exp $
 # -------------------------------------------------------------------
 # Copyright (C) 2003 darren chamberlain <darren@cpan.org>,
 #
@@ -22,56 +22,44 @@ package SQL::Translator::Producer::YAML;
 
 use strict;
 use vars qw($VERSION);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
 
-use SQL::Translator::Utils qw(header_comment);
+use YAML qw(Dump);
 
 sub produce {
     my $translator  = shift;
     my $schema      = $translator->schema;
 
-    return 
-        join "\n" => 
-            '--- #YAML:1.0',
-            #header_comment('', '# '),
-            map { view_table($_) } $schema->get_tables;
+    return Dump({
+        schema => {
+            map { ($_->name => view_table($_)) } $schema->get_tables
+        }
+    });
 }
 
 sub view_table {
     my $table = shift;
+    my $name = $table->name;
 
-    return
-        sprintf "%s:\n%s\n",
-            $table->name,
-            join "\n" =>
-                map { "    $_" }
-                map { view_field($_) } $table->get_fields;
+    return {
+        map { ($_->name => view_field($_)) } $table->get_fields
+    };
 }
 
 sub view_field {
     my $field = shift;
 
-    return
-        sprintf("%s: %s" => $field->name),
-        map {
-            sprintf "    %s: %s" => $_->[0], view($_->[1])
-        } (
-            [ 'order' =>   $field->order        ],
-            [ 'name'  =>   $field->name         ],
-            [ 'type'  =>   $field->data_type    ],
-            [ 'size'  => [ $field->size  ]      ],
-            [ 'extra' => { $field->extra }      ],
-        );
+    return {
+        'order' => scalar $field->order,
+        'name'  => scalar $field->name,
+        'type'  => scalar $field->data_type,
+        'size'  => [ $field->size ],
+        'extra' => { $field->extra },
+    };
 }
 
-sub view {
-    my $thingie = shift;
+1;
 
-    {   ''       => sub { $_[0] },
-        'SCALAR' => sub { ${$_[0]} },
-        'ARRAY'  => sub { join "\n    - $_", @{$_[0]} },
-        'HASH'   => sub { join "\n    " => map { "$_: $_[0]->{$_}" } keys %{$_[0]} },
-    }->{ref $thingie}->($thingie);
-}
+=head1 NAME
 
-1;
+SQL::Translator::Producer::YAML - A YAML producer for SQL::Translator
index be6e333..7626cd1 100644 (file)
@@ -15,25 +15,29 @@ CREATE TABLE random (
 |;
 
 my $yaml = q|--- #YAML:1.0
-random:
-    id: 
-        order: 1
-        name: id
-        type: int
-        size: 11
-        extra: 
-    foo: 
-        order: 2
-        name: foo
-        type: varchar
-        size: 255
-        extra: 
-    updated: 
-        order: 3
-        name: updated
-        type: timestamp
-        size: 0
-        extra: 
+schema:
+  random:
+    foo:
+      extra: {}
+      name: foo
+      order: 2
+      size:
+        - 255
+      type: varchar
+    id:
+      extra: {}
+      name: id
+      order: 1
+      size:
+        - 11
+      type: int
+    updated:
+      extra: {}
+      name: updated
+      order: 3
+      size:
+        - 0
+      type: timestamp
 |;
 
 my $out;