better dynamic schema_base_class implementation
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 75165bf..3b74da4 100644 (file)
@@ -168,12 +168,26 @@ overwriting a dump made with an earlier version.
 
 The option also takes a hashref:
 
-    naming => { relationships => 'v8', monikers => 'v8' }
+    naming => {
+        relationships    => 'v8',
+        monikers         => 'v8',
+        column_accessors => 'v8',
+        force_ascii      => 1,
+    }
+
+or
+
+    naming => { ALL => 'v8', force_ascii => 1 }
 
 The keys are:
 
 =over 4
 
+=item ALL
+
+Set L</relationships>, L</monikers> and L</column_accessors> to the specified
+value.
+
 =item relationships
 
 How to name relationship accessors.
@@ -189,8 +203,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 +257,10 @@ 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>
@@ -530,6 +545,10 @@ Default behavior is to utilize L<Lingua::EN::Inflect::Phrase/to_S>.
 
 Base class for your schema classes. Defaults to 'DBIx::Class::Schema'.
 
+=head2 schema_components
+
+List of components to load into the Schema class.
+
 =head2 result_base_class
 
 Base class for your table classes (aka result classes). Defaults to
@@ -548,10 +567,6 @@ that need to be leftmost.
 
 List of additional classes which all of your table classes will use.
 
-=head2 schema_components
-
-List of components to load into the Schema class.
-
 =head2 components
 
 List of additional components to be loaded into all of your Result
@@ -946,10 +961,16 @@ sub new {
             column_accessors => $naming_ver,
         };
     }
+    elsif (ref $self->naming eq 'HASH' && exists $self->naming->{ALL}) {
+        my $val = delete $self->naming->{ALL};
+
+        $self->naming->{$_} = $val
+            foreach qw/relationships monikers column_accessors/;
+    }
 
     if ($self->naming) {
-        for (values %{ $self->naming }) {
-            $_ = $CURRENT_V if $_ eq 'current';
+        foreach my $key (qw/relationships monikers column_accessors/) {
+            $self->naming->{$key} = $CURRENT_V if $self->naming->{$key} eq 'current';
         }
     }
     $self->{naming} ||= {};
@@ -2164,8 +2185,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.
@@ -2324,6 +2347,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) = @_;
@@ -2334,17 +2369,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) {