object|DBIx::Class::Schema::Loader::Table> argument (which stringifies to the
unqualified table name) and returning a scalar moniker
+The function is also passed a coderef that can be called with either
+of the hashref forms to get the moniker mapped accordingly. This is
+useful if you need to handle some monikers specially, but want to use
+the hashref form for the rest.
+
=back
If the hash entry does not exist, or the function returns a false
},
Given the table C<foo.bar>, the code ref would be called with the
-arguments C<foo> and C<schema>.
+arguments C<foo> and C<schema>, plus a coderef similar to the one
+described in L</moniker_map>.
L</moniker_map> takes precedence over this.
schema_class => name of the schema class we are building,
column_info => hashref of column info (data_type, is_nullable, etc),
}
+ coderef ref that can be called with a hashref map
the L<table object|DBIx::Class::Schema::Loader::Table> stringifies to the
unqualified table name.
and relationships that would have been named C<bar> will now be named C<baz>
except that in the table whose moniker is C<Foo> it will be named C<blat>.
-If it is a coderef, the argument passed will be a hashref of this form:
+If it is a coderef, it will be passed a hashref of this form:
{
name => default relationship name,
link_rel_name => name of the relationship to the link table
}
+In addition it is passed a coderef that can be called with a hashref map.
+
DBICSL will try to use the value returned as the relationship name.
=head2 inflect_plural
}
}
elsif( $map && ref $map eq 'CODE' ) {
- $new_ident = $map->( $ident, $default_ident, @extra );
+ my $cb = sub {
+ my ($cb_map) = @_;
+ croak "reentered map must be a hashref"
+ unless 'HASH' eq ref($cb_map);
+ return $self->_run_user_map($cb_map, $default_code, $ident, @extra);
+ };
+ $new_ident = $map->( $ident, $default_ident, @extra, $cb );
}
$new_ident ||= $default_ident;
remote_columns => $remote_cols,
};
- my $new_name = $relname;
+ $self->_run_user_map($self->rel_name_map, $info);
+}
+
+sub _run_user_map {
+ my ($self, $map, $info) = @_;
- my $map = $self->rel_name_map;
+ my $new_name = $info->{name};
my $mapped = 0;
if ('HASH' eq ref($map)) {
}
}
elsif ('CODE' eq ref($map)) {
- my $name = $map->($info);
+ my $cb = sub {
+ my ($cb_map) = @_;
+ croak "reentered rel_name_map must be a hashref"
+ unless 'HASH' eq ref($cb_map);
+ my ($cb_name, $cb_mapped) = $self->_run_user_map($cb_map, $info);
+ return $cb_mapped && $cb_name;
+ };
+ my $name = $map->($info, $cb);
if ($name) {
$new_name = $name;
$mapped = 1;
# test coderef as rel_name_map
my $code_relationship = schema_with(
rel_name_map => sub {
- my ($args) = @_;
+ my ($args, $orig) = @_;
if ($args->{local_moniker} eq 'Foo') {
is_deeply(
},
'correct args for Foo passed'
);
- return 'bars_caught';
}
elsif ($args->{local_moniker} eq 'Bar') {
is_deeply(
},
'correct args for Foo passed'
);
-
- return 'fooref_caught';
}
+ else {
+ fail( 'correct args passed to rel_name_map' );
+ diag "args were: ", explain $args;
+ }
+ return $orig->({
+ Bar => { fooref => 'fooref_caught' },
+ Foo => { bars => 'bars_caught' },
+ });
}
);
is( ref($code_relationship->source('Foo')->relationship_info('bars_caught')),
'rel_name_map overrode remote_info correctly'
);
+throws_ok {
+ schema_with( rel_name_map => sub { $_[-1]->(sub{}) } ),
+} qr/reentered rel_name_map must be a hashref/, 'throws error for invalid (code) rel_name_map callback map';
# test relationship_attrs