release 0.07004
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / RelBuilder / Compat / v0_05.pm
1 package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05;
2
3 use strict;
4 use warnings;
5 use mro 'c3';
6 use base 'DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_06';
7 use Carp::Clan qw/^DBIx::Class/;
8 use Lingua::EN::Inflect::Number ();
9
10 our $VERSION = '0.07004';
11
12 sub _to_PL {
13     my ($self, $name) = @_;
14
15     return Lingua::EN::Inflect::Number::to_PL($name);
16 }
17
18 sub _to_S {
19     my ($self, $name) = @_;
20
21     return Lingua::EN::Inflect::Number::to_S($name);
22 }
23
24 sub _default_relationship_attrs { +{} }
25
26 sub _relnames_and_method {
27     my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_;
28
29     my $remote_moniker = $rel->{remote_source};
30     my $remote_obj     = $self->{schema}->source( $remote_moniker );
31     my $remote_class   = $self->{schema}->class(  $remote_moniker );
32     my $remote_relname = $self->_remote_relname( $remote_obj->from, $cond);
33
34     my $local_cols  = $rel->{local_columns};
35     my $local_table = $self->{schema}->source($local_moniker)->from;
36
37     # If more than one rel between this pair of tables, use the local
38     # col names to distinguish
39     my ($local_relname, $local_relname_uninflected);
40     if ( $counters->{$remote_moniker} > 1) {
41         my $colnames = lc(q{_} . join(q{_}, map lc($_), @$local_cols));
42         $remote_relname .= $colnames if keys %$cond > 1;
43
44         $local_relname = lc($local_table) . $colnames;
45
46         $local_relname_uninflected = $local_relname;
47         $local_relname = $self->_inflect_plural( $local_relname );
48     } else {
49         $local_relname_uninflected = lc $local_table;
50         $local_relname = $self->_inflect_plural(lc $local_table);
51     }
52
53     my $remote_method = 'has_many';
54
55     # If the local columns have a UNIQUE constraint, this is a one-to-one rel
56     my $local_source = $self->{schema}->source($local_moniker);
57     if ($self->_array_eq([ $local_source->primary_columns ], $local_cols) ||
58             grep { $self->_array_eq($_->[1], $local_cols) } @$uniqs) {
59         $remote_method = 'might_have';
60         $local_relname = $self->_inflect_singular($local_relname_uninflected);
61     }
62
63     return ( $local_relname, $remote_relname, $remote_method );
64 }
65
66 =head1 NAME
67
68 DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_05 - RelBuilder for
69 compatibility with DBIx::Class::Schema::Loader version 0.05003
70
71 =head1 DESCRIPTION
72
73 See L<DBIx::Class::Schema::Loader::Base/naming> and
74 L<DBIx::Class::Schema::Loader::RelBuilder>.
75
76 =head1 AUTHOR
77
78 See L<DBIx::Class::Schema::Loader/AUTHOR> and L<DBIx::Class::Schema::Loader/CONTRIBUTORS>.
79
80 =head1 LICENSE
81
82 This library is free software; you can redistribute it and/or modify it under
83 the same terms as Perl itself.
84
85 =cut
86
87 1;