change sub to method
Justin Hunter [Wed, 19 Aug 2009 09:25:48 +0000 (02:25 -0700)]
lib/SQL/Translator/Producer/YAML.pm

index e86d7f7..6027d3a 100644 (file)
 use MooseX::Declare;
 role SQL::Translator::Producer::YAML {
-use YAML qw(Dump);
-use SQL::Translator::Types qw(Table);
-
-method produce {
-    my $translator = $self;
-    my $schema     = $translator->schema;
-#    use Data::Dumper; warn Dumper($schema);
-
-    my $r = Dump({
-        schema => {
-            tables => { 
-                map { ($_->name => view_table($_)) }
-                    $schema->get_tables,
+    use YAML qw(Dump);
+    use SQL::Translator::Types qw(Column Constraint Index Procedure Table Trigger View);
+    
+    method produce {
+        my $schema = $self->schema;
+        my $translator = $self->translator;
+
+        return Dump({
+            schema => {
+                tables =>     { map { ($_->name => $self->_create_table($_)) } $schema->get_tables, },
+                views =>      { map { ($_->name => $self->_create_view($_)) } $schema->get_views, },
+                triggers =>   { map { ($_->name => $self->_create_trigger($_)) } $schema->get_triggers, },
+                procedures => { map { ($_->name => $self->_create_procedure($_)) } $schema->get_procedures, },
             },
-            views => { 
-#                map { ($_->name => view_view($_)) }
-#                    $schema->get_views,
+            translator => {
+                add_drop_table => $translator->add_drop_table,
+                filename       => $translator->filename,
+                no_comments    => $translator->no_comments,
+                parser_args    => $translator->parser_args,
+                producer_args  => $translator->producer_args,
+                parser_type    => $translator->parser_type,
+                producer_type  => $translator->producer_type,
+                show_warnings  => $translator->show_warnings,
+                trace          => $translator->trace,
+                version        => $translator->version,
             },
-            triggers => { 
-#                map { ($_->name => view_trigger($_)) }
-#                    $schema->get_triggers,
+    #        keys %{$schema->extra} ? ('extra' => { $schema->extra } ) : (),
+        });
+    }
+    
+    method _create_table(Table $table) {
+        return {
+            'name'        => $table->name,
+    #        'order'       => $table->order,
+            'options'     => $table->options  || [],
+            $table->comments ? ('comments'    => $table->comments ) : (),
+            'constraints' => [ map { $self->_create_constraint($_) } $table->get_constraints ],
+            'indices'     => [ map { $self->_create_index($_) } $table->get_indices ],
+            'fields'      => { map { ($_->name => $self->_create_field($_)) } $table->get_fields 
             },
-            procedures => { 
-#                map { ($_->name => view_procedure($_)) } 
-#                    $schema->get_procedures,
-            },
-        },
-#        translator => {
-#            add_drop_table => $translator->add_drop_table,
-#            filename       => $translator->filename,
-#            no_comments    => $translator->no_comments,
-#            parser_args    => $translator->parser_args,
-#            producer_args  => $translator->producer_args,
-#            parser_type    => $translator->parser_type,
-#            producer_type  => $translator->producer_type,
-#            show_warnings  => $translator->show_warnings,
-#            trace          => $translator->trace,
-#            version        => $translator->version,
-#        },
-#        keys %{$schema->extra} ? ('extra' => { $schema->extra } ) : (),
-    });
-#    use Data::Dumper; warn Dumper($r);
-    $r;
-}
-
-sub view_table {
-    my ($table) = shift;
-
-    return {
-        'name'        => $table->name,
-#        'order'       => $table->order,
-        'options'     => $table->options  || [],
-        $table->comments ? ('comments'    => $table->comments ) : (),
-        'constraints' => [
-            map { view_constraint($_) } $table->get_constraints
-        ],
-        'indices'     => [
-            map { view_index($_) } $table->get_indices
-        ],
-        'fields'      => { 
-            map { ($_->name => view_field($_)) }
-                $table->get_fields 
-        },
-        #keys %{$table->extra} ? ('extra' => { $table->extra } ) : (),
-    };
-}
-
-sub view_constraint {
-    my $constraint = shift;
-
-    return {
-        'deferrable'       => scalar $constraint->deferrable,
-        'expression'       => scalar $constraint->expression,
-        'fields'           => [ map { ref $_ ? $_->name : $_ } $constraint->field_names ],
-        'match_type'       => scalar $constraint->match_type,
-        'name'             => scalar $constraint->name,
-        'options'          => scalar $constraint->options,
-#        'on_delete'        => scalar $constraint->on_delete,
-#        'on_update'        => scalar $constraint->on_update,
-#        'reference_fields' => [ map { ref $_ ? $_->name : $_ } $constraint->reference_fields ],
-#        'reference_table'  => scalar $constraint->reference_table,
-        'type'             => scalar $constraint->type,
-        #keys %{$constraint->extra} ? ('extra' => { $constraint->extra } ) : (),
-    };
-}
-
-sub view_field {
-    my $field = shift;
-
-    return {
-        'order'             => scalar $field->order,
-        'name'              => scalar $field->name,
-        'data_type'         => scalar $field->data_type,
-        'size'              => [ $field->size ],
-        'default_value'     => scalar $field->default_value,
-        'is_nullable'       => scalar $field->is_nullable,
-        'is_primary_key'    => scalar $field->is_primary_key,
-        'is_unique'         => scalar $field->is_unique,
-        $field->is_auto_increment ? ('is_auto_increment' => 1) : (),
-        $field->comments ? ('comments' => $field->comments) : (),
-        #keys %{$field->extra} ? ('extra' => { $field->extra } ) : (),
-    };
-}
-
-sub view_procedure {
-    my $procedure = shift;
-
-    return {
-        'order'      => scalar $procedure->order,
-        'name'       => scalar $procedure->name,
-        'sql'        => scalar $procedure->sql,
-        'parameters' => scalar $procedure->parameters,
-        'owner'      => scalar $procedure->owner,
-        'comments'   => scalar $procedure->comments,
-        #keys %{$procedure->extra} ? ('extra' => { $procedure->extra } ) : (),
-    };
-}
-
-sub view_trigger {
-    my $trigger = shift;
-
-    return {
-        'order'               => scalar $trigger->order,
-        'name'                => scalar $trigger->name,
-        'perform_action_when' => scalar $trigger->perform_action_when,
-        'database_events'     => scalar $trigger->database_events,
-        'fields'              => scalar $trigger->fields,
-        'on_table'            => scalar $trigger->on_table,
-        'action'              => scalar $trigger->action,
-        #keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (),
-    };
-}
-
-sub view_view {
-    my $view = shift;
-
-    return {
-        'order'  => scalar $view->order,
-        'name'   => scalar $view->name,
-        'sql'    => scalar $view->sql,
-        'fields' => scalar $view->fields,
-        #keys %{$view->extra} ? ('extra' => { $view->extra } ) : (),
-    };
-}
-
-sub view_index {
-    my $index = shift;
-
-    return {
-        'name'      => scalar $index->name,
-        'type'      => scalar $index->type,
-        'fields'    => scalar $index->fields,
-        'options'   => scalar $index->options,
-        #keys %{$index->extra} ? ('extra' => { $index->extra } ) : (),
-    };
-}
+            #keys %{$table->extra} ? ('extra' => { $table->extra } ) : (),
+        };
+    }
+    
+    method _create_constraint(Constraint $constraint) {
+        return {
+            'deferrable'       => scalar $constraint->deferrable,
+            'expression'       => scalar $constraint->expression,
+            'fields'           => [ map { ref $_ ? $_->name : $_ } $constraint->field_names ],
+            'match_type'       => scalar $constraint->match_type,
+            'name'             => scalar $constraint->name,
+            'options'          => scalar $constraint->options,
+    #        'on_delete'        => scalar $constraint->on_delete,
+    #        'on_update'        => scalar $constraint->on_update,
+    #        'reference_fields' => [ map { ref $_ ? $_->name : $_ } $constraint->reference_fields ],
+    #        'reference_table'  => scalar $constraint->reference_table,
+            'type'             => scalar $constraint->type,
+            #keys %{$constraint->extra} ? ('extra' => { $constraint->extra } ) : (),
+        };
+    }
+    
+    method _create_field(Column $field) {
+        return {
+    #        'order'             => scalar $field->order,
+            'name'              => scalar $field->name,
+            'data_type'         => scalar $field->data_type,
+            'size'              => [ $field->size ],
+            'default_value'     => scalar $field->default_value,
+            'is_nullable'       => scalar $field->is_nullable,
+            'is_primary_key'    => scalar $field->is_primary_key,
+            'is_unique'         => scalar $field->is_unique,
+            $field->is_auto_increment ? ('is_auto_increment' => 1) : (),
+            $field->comments ? ('comments' => $field->comments) : (),
+            #keys %{$field->extra} ? ('extra' => { $field->extra } ) : (),
+        };
+    }
+    
+    method _create_procedure(Procedure $procedure) {
+        return {
+    #        'order'      => scalar $procedure->order,
+            'name'       => scalar $procedure->name,
+            'sql'        => scalar $procedure->sql,
+            'parameters' => scalar $procedure->parameters,
+            'owner'      => scalar $procedure->owner,
+            'comments'   => scalar $procedure->comments,
+            #keys %{$procedure->extra} ? ('extra' => { $procedure->extra } ) : (),
+        };
+    }
+    
+    method _create_trigger(Trigger $trigger) {
+        return {
+    #        'order'               => scalar $trigger->order,
+            'name'                => scalar $trigger->name,
+            'perform_action_when' => scalar $trigger->perform_action_when,
+            'database_events'     => scalar $trigger->database_events,
+            'fields'              => scalar $trigger->fields,
+            'on_table'            => scalar $trigger->on_table,
+            'action'              => scalar $trigger->action,
+            #keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (),
+        };
+    }
+    
+    method _create_view(View $view) {
+        return {
+    #        'order'  => scalar $view->order,
+            'name'   => scalar $view->name,
+            'sql'    => scalar $view->sql,
+            'fields' => scalar $view->fields,
+            #keys %{$view->extra} ? ('extra' => { $view->extra } ) : (),
+        };
+    }
+    
+    method _create_index(Index $index) {
+        return {
+            'name'      => scalar $index->name,
+            'type'      => scalar $index->type,
+            'fields'    => scalar $index->fields,
+            'options'   => scalar $index->options,
+            #keys %{$index->extra} ? ('extra' => { $index->extra } ) : (),
+        };
+    }
 }