better inflection using Lingua::EN::Inflect::Phrase
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / RelBuilder / Compat / v0_040.pm
1 package DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040;
2
3 use strict;
4 use warnings;
5 use Class::C3;
6 use Lingua::EN::Inflect::Number ();
7
8 use base 'DBIx::Class::Schema::Loader::RelBuilder';
9
10 sub _default_relationship_attrs { +{} }
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 _relnames_and_method {
25     my ( $self, $local_moniker, $rel, $cond, $uniqs, $counters ) = @_;
26
27     my $remote_moniker = $rel->{remote_source};
28     my $remote_table   = $self->{schema}->source( $remote_moniker )->from;
29
30     my $local_table = $self->{schema}->source($local_moniker)->from;
31     my $local_cols  = $rel->{local_columns};
32
33     # for single-column case, set the remote relname to just the column name
34     my $remote_relname =
35         scalar keys %{$cond} == 1
36             ? $self->_inflect_singular( values %$cond  )
37             : $self->_inflect_singular( lc $remote_table );
38
39     # If more than one rel between this pair of tables, use the local
40     # col names to distinguish
41     my $local_relname;
42     if ($counters->{$remote_moniker} > 1) {
43         my $colnames = '_' . join( '_', @$local_cols );
44         $remote_relname .= $colnames if keys %$cond > 1;
45         $local_relname = $self->_inflect_plural( lc($local_table) . $colnames );
46     } else {
47         $local_relname = $self->_inflect_plural(lc $local_table);
48     }
49
50     return ( $local_relname, $remote_relname, 'has_many' );
51 }
52
53 sub _remote_attrs { }
54
55 1;
56
57 =head1 NAME
58
59 DBIx::Class::Schema::Loader::RelBuilder::Compat::v0_040 - RelBuilder for
60 compatibility with DBIx::Class::Schema::Loader version 0.04006
61
62 =head1 DESCRIPTION
63
64 See L<DBIx::Class::Schema::Loader::Base/naming>.
65
66 =cut