rename column_accessor_map -> col_accessor_map
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 5e6da78..86cacf5 100644 (file)
@@ -25,7 +25,7 @@ use DBIx::Class ();
 use Class::Load 'load_class';
 use namespace::clean;
 
-our $VERSION = '0.07002';
+our $VERSION = '0.07007';
 
 __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 schema
@@ -40,7 +40,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 skip_relationships
                                 skip_load_external
                                 moniker_map
-                                column_accessor_map
+                                col_accessor_map
                                 custom_column_info
                                 inflect_singular
                                 inflect_plural
@@ -86,8 +86,10 @@ __PACKAGE__->mk_group_accessors('simple', qw/
                                 pod_comment_spillover_length
                                 preserve_case
                                 col_collision_map
+                                rel_collision_map
                                 real_dump_directory
                                 datetime_undef_if_invalid
+                                _result_class_methods
 /);
 
 =head1 NAME
@@ -308,7 +310,7 @@ together. Examples:
     stations_visited | StationVisited
     routeChange      | RouteChange
 
-=head2 column_accessor_map
+=head2 col_accessor_map
 
 Same as moniker_map, but for column accessor names.  If a coderef is
 passed, the code is called with arguments of
@@ -517,6 +519,14 @@ Examples:
 
     col_collision_map => { '(foo).*(bar)' => 'column_%s_%s' }
 
+=head2 rel_collision_map
+
+Works just like L</col_collision_map>, but for relationship names/accessors
+rather than column names/accessors.
+
+The default is to just append C<_rel> to the relationship name, see
+L</RELATIONSHIP NAME COLLISIONS>.
+
 =head1 METHODS
 
 None of these methods are intended for direct invocation by regular
@@ -554,6 +564,10 @@ by L<DBIx::Class::Schema::Loader>.
 sub new {
     my ( $class, %args ) = @_;
 
+    if (exists $args{column_accessor_map}) {
+        $args{col_accessor_map} = delete $args{column_accessor_map};
+    }
+
     my $self = { %args };
 
     # don't lose undef options
@@ -1567,36 +1581,45 @@ sub _make_src_class {
     $self->_inject($table_class, @{$self->additional_base_classes});
 }
 
-sub _resolve_col_accessor_collisions {
-    my ($self, $table, $col_info) = @_;
+sub _is_result_class_method {
+    my ($self, $name) = @_;
 
-    my $base       = $self->result_base_class || 'DBIx::Class::Core';
-    my @components = map { /^\+/ ? substr($_,1) : "DBIx::Class::$_" } @{ $self->components || [] };
+    if (not $self->_result_class_methods) {
+        my (@methods, %methods);
+        my $base       = $self->result_base_class || 'DBIx::Class::Core';
+        my @components = map { /^\+/ ? substr($_,1) : "DBIx::Class::$_" } @{ $self->components || [] };
 
-    my $table_name = ref $table ? $$table : $table;
+        for my $class ($base, @components, $self->use_moose ? 'Moose::Object' : ()) {
+            load_class $class;
+
+            push @methods, @{ Class::Inspector->methods($class) || [] };
+        }
 
-    my @methods;
+        push @methods, @{ Class::Inspector->methods('UNIVERSAL') };
 
-    for my $class ($base, @components, $self->use_moose ? 'Moose::Object' : ()) {
-        load_class $class;
+        @methods{@methods} = ();
 
-        push @methods, @{ Class::Inspector->methods($class) || [] };
+        # futureproof meta
+        $methods{meta} = undef;
+
+        $self->_result_class_methods(\%methods);
     }
+    my $result_methods = $self->_result_class_methods;
 
-    push @methods, @{ Class::Inspector->methods('UNIVERSAL') };
+    return exists $result_methods->{$name};
+}
 
-    my %methods;
-    @methods{@methods} = ();
+sub _resolve_col_accessor_collisions {
+    my ($self, $table, $col_info) = @_;
 
-    # futureproof meta
-    $methods{meta} = undef;
+    my $table_name = ref $table ? $$table : $table;
 
     while (my ($col, $info) = each %$col_info) {
         my $accessor = $info->{accessor} || $col;
 
         next if $accessor eq 'id'; # special case (very common column)
 
-        if (exists $methods{$accessor}) {
+        if ($self->_is_result_class_method($accessor)) {
             my $mapped = 0;
 
             if (my $map = $self->col_collision_map) {
@@ -1610,7 +1633,7 @@ sub _resolve_col_accessor_collisions {
 
             if (not $mapped) {
                 warn <<"EOF";
-Column $col in table $table_name collides with an inherited method.
+Column '$col' in table '$table_name' collides with an inherited method.
 See "COLUMN ACCESSOR COLLISIONS" in perldoc DBIx::Class::Schema::Loader::Base .
 EOF
                 $info->{accessor} = undef;
@@ -1619,7 +1642,7 @@ EOF
     }
 }
 
-# use the same logic to run moniker_map, column_accessor_map, and
+# use the same logic to run moniker_map, col_accessor_map, and
 # relationship_name_map
 sub _run_user_map {
     my ( $self, $map, $default_code, $ident, @extra ) = @_;
@@ -1657,7 +1680,7 @@ sub _make_column_accessor_name {
     my ($self, $column_name, $column_context_info ) = @_;
 
     my $accessor = $self->_run_user_map(
-        $self->column_accessor_map,
+        $self->col_accessor_map,
         sub { $self->_default_column_accessor_name( shift ) },
         $column_name,
         $column_context_info,
@@ -2085,6 +2108,20 @@ below the md5:
 
 Another option is to use the L</col_collision_map> option.
 
+=head1 RELATIONSHIP NAME COLLISIONS
+
+In very rare cases, you may get a collision between a generated relationship
+name and a method in your Result class, for example if you have a foreign key
+called C<belongs_to>.
+
+This is a problem because relationship names are also relationship accessor
+methods in L<DBIx::Class>.
+
+The default behavior is to append C<_rel> to the relationship name and print
+out a warning that refers to this text.
+
+You can also control the renaming with the L</rel_collision_map> option.
+
 =head1 SEE ALSO
 
 L<DBIx::Class::Schema::Loader>