pod fixes for UpgradingFromV4
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index e1387e9..ea22c4f 100644 (file)
@@ -16,7 +16,7 @@ use File::Temp qw//;
 use Class::Unload;
 require DBIx::Class;
 
-our $VERSION = '0.04999_10';
+our $VERSION = '0.04999_12';
 
 __PACKAGE__->mk_ro_accessors(qw/
                                 schema
@@ -49,10 +49,13 @@ __PACKAGE__->mk_ro_accessors(qw/
                                 classes
                                 monikers
                                 dynamic
+                                naming
+                                _upgrading_from
                              /);
 
 __PACKAGE__->mk_accessors(qw/
                                 version_to_dump
+                                schema_version_to_dump
 /);
 
 =head1 NAME
@@ -96,7 +99,21 @@ overwriting a dump made with an earlier version.
 
 The option also takes a hashref:
 
-    naming => { relationships => 'v5', results => 'v4' }
+    naming => { relationships => 'v5', monikers => 'v4' }
+
+The keys are:
+
+=over 4
+
+=item relationships
+
+How to name relationship accessors.
+
+=item monikers
+
+How to name Result classes.
+
+=back
 
 The values can be:
 
@@ -155,10 +172,11 @@ a scalar moniker.  If the hash entry does not exist, or the function
 returns a false value, the code falls back to default behavior
 for that table name.
 
-The default behavior is: C<join '', map ucfirst, split /[\W_]+/, lc $table>,
-which is to say: lowercase everything, split up the table name into chunks
-anywhere a non-alpha-numeric character occurs, change the case of first letter
-of each chunk to upper case, and put the chunks back together.  Examples:
+The default behavior is to singularize the table name, and: C<join '', map
+ucfirst, split /[\W_]+/, lc $table>, which is to say: lowercase everything,
+split up the table name into chunks anywhere a non-alpha-numeric character
+occurs, change the case of first letter of each chunk to upper case, and put
+the chunks back together.  Examples:
 
     Table Name  | Moniker Name
     ---------------------------
@@ -184,7 +202,8 @@ Base class for your schema classes. Defaults to 'DBIx::Class::Schema'.
 
 =head2 result_base_class
 
-Base class for your table classes (aka result classes). Defaults to 'DBIx::Class'.
+Base class for your table classes (aka result classes). Defaults to
+'DBIx::Class::Core'.
 
 =head2 additional_base_classes
 
@@ -327,6 +346,15 @@ sub new {
     $self->{dump_directory} ||= $self->{temp_directory};
 
     $self->version_to_dump($DBIx::Class::Schema::Loader::VERSION);
+    $self->schema_version_to_dump($DBIx::Class::Schema::Loader::VERSION);
+
+    if (not ref $self->naming && defined $self->naming) {
+        my $naming_ver = $self->naming;
+        $self->{naming} = {
+            relationships => $naming_ver,
+            monikers => $naming_ver,
+        };
+    }
 
     $self->_check_back_compat;
 
@@ -336,16 +364,14 @@ sub new {
 sub _check_back_compat {
     my ($self) = @_;
 
-# dynamic schemas will always be in 0.04006 mode
+# dynamic schemas will always be in 0.04006 mode, unless overridden
     if ($self->dynamic) {
-        no strict 'refs';
-        my $class = ref $self || $self;
-        require DBIx::Class::Schema::Loader::Compat::v0_040;
-        unshift @{"${class}::ISA"},
-            'DBIx::Class::Schema::Loader::Compat::v0_040';
-        Class::C3::reinitialize;
 # just in case, though no one is likely to dump a dynamic schema
-        $self->version_to_dump('0.04006');
+        $self->schema_version_to_dump('0.04006');
+
+        $self->naming->{relationships} ||= 'v4';
+        $self->naming->{monikers}      ||= 'v4';
+
         return;
     }
 
@@ -359,19 +385,16 @@ sub _check_back_compat {
     while (<$fh>) {
         if (/^# Created by DBIx::Class::Schema::Loader v((\d+)\.(\d+))/) {
             my $real_ver = $1;
-            my $ver      = "v${2}_${3}";
-            while (1) {
-                my $compat_class = "DBIx::Class::Schema::Loader::Compat::${ver}";
-                if ($self->load_optional_class($compat_class)) {
-                    no strict 'refs';
-                    my $class = ref $self || $self;
-                    unshift @{"${class}::ISA"}, $compat_class;
-                    Class::C3::reinitialize;
-                    $self->version_to_dump($real_ver);
-                    last;
-                }
-                $ver =~ s/\d\z// or last;
-            }
+
+            $self->schema_version_to_dump($real_ver);
+
+            # XXX when we go past .0 this will need fixing
+            my ($v) = $real_ver =~ /([1-9])/;
+            $v = "v$v";
+
+            $self->naming->{relationships} ||= $v;
+            $self->naming->{monikers}      ||= $v;
+
             last;
         }
     }
@@ -438,13 +461,12 @@ sub _load_external {
     close($fh)
         or croak "Failed to close $real_inc_path: $!";
 
-# load the class too
-    {
+    if ($self->dynamic) { # load the class too
         # turn off redefined warnings
-        $SIG{__WARN__} = sub {};
+        local $SIG{__WARN__} = sub {};
         do $real_inc_path;
+        die $@ if $@;
     }
-    die $@ if $@;
 }
 
 =head2 load
@@ -498,6 +520,14 @@ sub _relbuilder {
 
     return if $self->{skip_relationships};
 
+    if ($self->naming->{relationships} eq 'v4') {
+        require DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040;
+        return $self->{relbuilder} ||=
+            DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040->new(
+                $self->schema, $self->inflect_plural, $self->inflect_singular
+            );
+    }
+
     $self->{relbuilder} ||= DBIx::Class::Schema::Loader::RelBuilder->new(
         $self->schema, $self->inflect_plural, $self->inflect_singular
     );
@@ -664,9 +694,12 @@ sub _dump_to_dir {
         $schema_text .= qq|__PACKAGE__->load_classes;\n|;
     }
 
-    $self->_write_classfile($schema_class, $schema_text);
+    {
+        local $self->{version_to_dump} = $self->schema_version_to_dump;
+        $self->_write_classfile($schema_class, $schema_text);
+    }
 
-    my $result_base_class = $self->result_base_class || 'DBIx::Class';
+    my $result_base_class = $self->result_base_class || 'DBIx::Class::Core';
 
     foreach my $src_class (@classes) {
         my $src_text = 
@@ -841,7 +874,9 @@ sub _make_src_class {
     $self->_use   ($table_class, @{$self->additional_classes});
     $self->_inject($table_class, @{$self->left_base_classes});
 
-    $self->_dbic_stmt($table_class, 'load_components', @{$self->components}, 'Core');
+    if (my @components = @{ $self->components }) {
+        $self->_dbic_stmt($table_class, 'load_components', @components);
+    }
 
     $self->_dbic_stmt($table_class, 'load_resultset_components', @{$self->resultset_components})
         if @{$self->resultset_components};
@@ -930,6 +965,10 @@ sub tables {
 sub _default_table2moniker {
     my ($self, $table) = @_;
 
+    if ($self->naming->{monikers} eq 'v4') {
+        return join '', map ucfirst, split /[\W_]+/, lc $table;
+    }
+
     return join '', map ucfirst, split /[\W_]+/,
         Lingua::EN::Inflect::Number::to_S(lc $table);
 }
@@ -996,15 +1035,59 @@ sub _dbic_stmt {
     my $self = shift;
     my $class = shift;
     my $method = shift;
-
+    if ( $method eq 'table' ) {
+        my ($table) = @_;
+        $self->_pod( $class, "=head1 NAME" );
+        my $table_descr = $class;
+        if ( $self->can('_table_comment') ) {
+            my $comment = $self->_table_comment($table);
+            $table_descr .= " - " . $comment if $comment;
+        }
+        $self->{_class2table}{ $class } = $table;
+        $self->_pod( $class, $table_descr );
+        $self->_pod_cut( $class );
+    } elsif ( $method eq 'add_columns' ) {
+        $self->_pod( $class, "=head1 ACCESSORS" );
+        my $i = 0;
+        foreach ( @_ ) {
+            $i++;
+            next unless $i % 2;
+            $self->_pod( $class, '=head2 ' . $_  );
+            my $comment;
+            $comment = $self->_column_comment( $self->{_class2table}{$class}, ($i - 1) / 2 + 1  ) if $self->can('_column_comment');
+            $self->_pod( $class, $comment ) if $comment;
+        }
+        $self->_pod_cut( $class );
+    } elsif ( $method =~ /^(belongs_to|has_many|might_have)$/ ) {
+        $self->_pod( $class, "=head1 RELATIONS" ) unless $self->{_relations_started} { $class } ;
+        my ( $accessor, $rel_class ) = @_;
+        $self->_pod( $class, "=head2 $accessor" );
+        $self->_pod( $class, 'Type: ' . $method );
+        $self->_pod( $class, "Related object: L<$rel_class>" );
+        $self->_pod_cut( $class );
+        $self->{_relations_started} { $class } = 1;
+    }
     my $args = dump(@_);
     $args = '(' . $args . ')' if @_ < 2;
     my $stmt = $method . $args . q{;};
 
     warn qq|$class\->$stmt\n| if $self->debug;
     $self->_raw_stmt($class, '__PACKAGE__->' . $stmt);
+    return;
 }
 
+# Stores a POD documentation
+sub _pod {
+    my ($self, $class, $stmt) = @_;
+    $self->_raw_stmt( $class, "\n" . $stmt  );
+}
+
+sub _pod_cut {
+    my ($self, $class ) = @_;
+    $self->_raw_stmt( $class, "\n=cut\n" );
+}
+
+
 # Store a raw source line for a class (for dumping purposes)
 sub _raw_stmt {
     my ($self, $class, $stmt) = @_;
@@ -1053,7 +1136,7 @@ L<DBIx::Class::Schema::Loader>
 
 =head1 AUTHOR
 
-See L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
+See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
 
 =head1 LICENSE