fully implement naming=v8 and force_ascii
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index b42584b..0af7c1d 100644 (file)
@@ -168,7 +168,7 @@ overwriting a dump made with an earlier version.
 
 The option also takes a hashref:
 
-    naming => { relationships => 'v7', monikers => 'v7' }
+    naming => { relationships => 'v8', monikers => 'v8' }
 
 The keys are:
 
@@ -189,8 +189,8 @@ How to name column accessors in Result classes.
 =item force_ascii
 
 For L</v8> mode and later, uses L<String::ToIdentifier::EN> instead of
-L<String::ToIdentifier::EM::Unicode> to force monikers and other identifiers
-such as relationship names to ASCII.
+L<String::ToIdentifier::EM::Unicode> to force monikers and other identifiers to
+ASCII.
 
 =back
 
@@ -243,9 +243,14 @@ breaking any of your code.
 The default mode is L</v7>, to get L</v8> mode, you have to specify it in
 L</naming> explictly until C<0.08> comes out.
 
-L</monikers> are created using L<String::ToIdentifier::EN::Unicode> or
-L<String::ToIdentifier::EN> if L</force_ascii> is set; this is only significant
-for table names with non C<\w> characters such as C<.>.
+L</monikers> and L</column_accessors> are created using
+L<String::ToIdentifier::EN::Unicode> or L<String::ToIdentifier::EN> if
+L</force_ascii> is set; this is only significant for names with non-C<\w>
+characters such as C<.>.
+
+For relationships, belongs_to accessors are made from column names by stripping
+postfixes other than C<_id> as well, just C<id>, C<_?ref>, C<_?cd>, C<_?code>
+and C<_num>.
 
 =item preserve
 
@@ -291,10 +296,11 @@ metadata for the text if available and supported.
 Comment metadata can be stored in two ways.
 
 The first is that you can create two tables named C<table_comments> and
-C<column_comments> respectively.  They both need to have columns named
-C<table_name> and C<comment_text>.  The second one needs to have a column
-named C<column_name>.  Then data stored in these tables will be used as a
-source of metadata about tables and comments.
+C<column_comments> respectively. These tables must exist in the same database
+and schema as the tables they describe. They both need to have columns named
+C<table_name> and C<comment_text>. The second one needs to have a column named
+C<column_name>. Then data stored in these tables will be used as a source of
+metadata about tables and comments.
 
 (If you wish you can change the name of these tables with the parameters
 L</table_comments_table> and L</column_comments_table>.)
@@ -346,11 +352,17 @@ The default is C<60>
 The table to look for comments about tables in.  By default C<table_comments>.
 See L</generate_pod> for details.
 
+This must not be a fully qualified name, the table will be looked for in the
+same database and schema as the table whose comment is being retrieved.
+
 =head2 column_comments_table
 
 The table to look for comments about columns in.  By default C<column_comments>.
 See L</generate_pod> for details.
 
+This must not be a fully qualified name, the table will be looked for in the
+same database and schema as the table/column whose comment is being retrieved.
+
 =head2 relationship_attrs
 
 Hashref of attributes to pass to each generated relationship, listed
@@ -1398,20 +1410,18 @@ sub _relbuilder {
     return if $self->{skip_relationships};
 
     return $self->{relbuilder} ||= do {
-
-        no warnings 'uninitialized';
         my $relbuilder_suff =
             {qw{
                 v4  ::Compat::v0_040
                 v5  ::Compat::v0_05
                 v6  ::Compat::v0_06
+                v7  ::Compat::v0_07
             }}
-            ->{ $self->naming->{relationships}};
+            ->{$self->naming->{relationships}||$CURRENT_V} || '';
 
         my $relbuilder_class = 'DBIx::Class::Schema::Loader::RelBuilder'.$relbuilder_suff;
         $self->ensure_class_loaded($relbuilder_class);
-        $relbuilder_class->new( $self );
-
+        $relbuilder_class->new($self);
     };
 }
 
@@ -2155,8 +2165,10 @@ sub _run_user_map {
 sub _default_column_accessor_name {
     my ( $self, $column_name ) = @_;
 
-    my $accessor_name = $column_name;
-    $accessor_name =~ s/\W+/_/g;
+    my $accessor_name = $self->_to_identifier('column_accessors', $column_name, '_');
+
+    $accessor_name =~ s/\W+/_/g; # only if naming < v8, otherwise to_identifier
+                                 # takes care of it
 
     if ((($self->naming->{column_accessors}||'') =~ /(\d+)/ && $1 < 7) || (not $self->preserve_case)) {
         # older naming just lc'd the col accessor and that's all.
@@ -2315,6 +2327,18 @@ sub tables {
     return values %{$self->_tables};
 }
 
+sub _to_identifier {
+    my ($self, $naming_key, $name, $sep_char) = @_;
+
+    my ($v) = ($self->naming->{$naming_key}||$CURRENT_V) =~ /^v(\d+)\z/;
+
+    my $to_identifier = $self->naming->{force_ascii} ?
+        \&String::ToIdentifier::EN::to_identifier
+        : \&String::ToIdentifier::EN::Unicode::to_identifier;
+
+    return $v >= 8 ? $to_identifier->($name, $sep_char) : $name;
+}
+
 # Make a moniker from a table
 sub _default_table2moniker {
     my ($self, $table) = @_;
@@ -2325,17 +2349,13 @@ sub _default_table2moniker {
 
     my $name_idx = firstidx { $_ eq 'name' } @{ $self->moniker_parts };
 
-    my $to_identifier = $self->naming->{force_ascii} ?
-        \&String::ToIdentifier::EN::to_identifier
-        : \&String::ToIdentifier::EN::Unicode::to_identifier;
-
     my @all_parts;
 
     foreach my $i (0 .. $#name_parts) {
         my $part = $name_parts[$i];
 
-        if ($i != $name_idx || $v > 7) {
-            $part = $to_identifier->($part, '_');
+        if ($i != $name_idx || $v >= 8) {
+            $part = $self->_to_identifier->('monikers', $part, '_');
         }
 
         if ($i == $name_idx && $v == 5) {