Revision history for Perl extension DBIx::Class::Schema::Loader
+0.02002 Sat Feb 18 19:53:12 UTC 2006
+ - Added moniker_map and inflect_map
+
0.02001 Fri Feb 17 20:25:40 UTC 2006
- tests fixed up a bit
- auto-loading of on-disk class definitions layered on top
---
name: DBIx-Class-Schema-Loader
-version: 0.02001
+version: 0.02002
author:
- 'Brandon Black, C<blblack@gmail.com>'
abstract: Dynamic definition of a DBIx::Class::Schema
provides:
DBIx::Class::Schema::Loader:
file: lib/DBIx/Class/Schema/Loader.pm
- version: 0.02001
+ version: 0.02002
DBIx::Class::Schema::Loader::DB2:
file: lib/DBIx/Class/Schema/Loader/DB2.pm
DBIx::Class::Schema::Loader::Generic:
package My::Schema;
use base qw/DBIx::Class::Schema::Loader/;
+ sub _monikerize {
+ my $name = shift;
+ $name = join '', map ucfirst, split /[\W_]+/, lc $name;
+ $name;
+ }
+
__PACKAGE__->load_from_connection(
dsn => "dbi:mysql:dbname",
user => "root",
constraint => '^foo.*',
relationships => 1,
options => { AutoCommit => 1 },
- inflect => { child => 'children' },
+ inflect_map => { child => 'children' },
+ moniker_map => \&_monikerize,
debug => 1,
);
# Always remember to do all digits for the version even if they're 0
# i.e. first release of 0.XX *must* be 0.XX000. This avoids fBSD ports
# brain damage and presumably various other packaging systems too
-our $VERSION = '0.02001';
+our $VERSION = '0.02002';
__PACKAGE__->mk_classaccessor('loader');
package My::Schema;
use base qw/DBIx::Class::Schema::Loader/;
+ sub _monikerize {
+ my $name = shift;
+ $name = join '', map ucfirst, split /[\W_]+/, lc $name;
+ $name;
+ }
+
__PACKAGE__->load_from_connection(
dsn => "dbi:mysql:dbname",
user => "root",
constraint => '^foo.*',
relationships => 1,
options => { AutoCommit => 1 },
- inflect => { child => 'children' },
+ inflect_map => { child => 'children' },
+ moniker_map => \&_monikerize,
debug => 1,
);
components
resultset_components
relationships
- inflect
+ inflect_map
+ moniker_map
db_schema
drop_db_schema
debug
Try to automatically detect/setup has_a and has_many relationships.
+=head2 moniker_map
+
+Overrides the default tablename -> moniker translation. Can be either
+a hashref of table => moniker names, or a coderef for a translator
+function taking a single scalar table name argument and returning
+a scalar moniker. If the hash entry does not exist, or the function
+returns a false/undef value, the code falls back to default behavior
+for that table name.
+
+=head2 inflect_map
+
+Just like L</moniker_map> above, but for inflecting (pluralizing)
+relationship names.
+
=head2 inflect
-An hashref, which contains exceptions to Lingua::EN::Inflect::PL().
-Useful for foreign language column names.
+Deprecated. Equivalent to L</inflect_map>, but previously only took
+a hashref argument, not a coderef. If you set C<inflect> to anything,
+that setting will be copied to L</inflect_map>.
=head2 user
$self->{db_schema} ||= '';
$self->{constraint} ||= '.*';
- $self->{inflect} ||= {};
$self->_ensure_arrayref(qw/additional_classes
additional_base_classes
left_base_classes
$self->{monikers} = {};
$self->{classes} = {};
+ # Support deprecated argument name
+ $self->{inflect_map} ||= $self->{inflect};
+
$self;
}
sub _db_classes { croak "ABSTRACT METHOD" }
# Inflect a relationship name
-# XXX (should pluralize, but currently also tends to de-pluralize plurals)
sub _inflect_relname {
my ($self, $relname) = @_;
- return $self->inflect->{$relname} if exists $self->inflect->{$relname};
+ if( ref $self->{inflect_map} eq 'HASH' ) {
+ return $self->inflect_map->{$relname}
+ if exists $self->inflect_map->{$relname};
+ }
+ elsif( ref $self->{inflect_map} eq 'CODE' ) {
+ my $inflected = $self->inflect_map->($relname);
+ return $inflected if $inflected;
+ }
+
return Lingua::EN::Inflect::PL($relname);
}
$table = $db_schema;
}
- my $moniker = join '', map ucfirst, split /[\W_]+/, lc $table;
+ my $moniker;
+
+ if( ref $self->moniker_map eq 'HASH' ) {
+ $moniker = $self->moniker_map->{$table};
+ }
+ elsif( ref $self->moniker_map eq 'CODE' ) {
+ $moniker = $self->moniker_map->($table);
+ }
+
+ $moniker ||= join '', map ucfirst, split /[\W_]+/, lc $table;
+
$moniker = $db_schema_ns ? $db_schema_ns . $moniker : $moniker;
return $moniker;
plan skip_all => $why;
}
+sub _monikerize {
+ my $name = shift;
+ return 'LoaderTest2X' if $name =~ /^loader_test2$/i;
+ return undef;
+}
+
sub run_tests {
my $self = shift;
- plan tests => 49;
+ plan tests => 50;
$self->create();
left_base_classes => [ qw/TestLeftBase/ ],
components => [ qw/TestComponent/ ],
resultset_components => [ qw/TestRSComponent/ ],
+ inflect_map => { loader_test4 => 'loader_test4zes' },
+ moniker_map => \&_monikerize,
debug => $debug,
);
isa_ok( $rsobj1, "DBIx::Class::ResultSet" );
isa_ok( $rsobj2, "DBIx::Class::ResultSet" );
+ is($moniker2, 'LoaderTest2X', "moniker_map testing");
+
{
my ($skip_tab, $skip_tabo, $skip_taba, $skip_cmeth,
$skip_rsmeth, $skip_tcomp, $skip_trscomp);
can_ok( $rsobj1, 'dbix_class_testrscomponent' ) or $skip_trscomp = 1;
can_ok( $class1, 'loader_test1_classmeth' ) or $skip_cmeth = 1;
- TODO: {
- local $TODO = "Not yet supported by ResultSetManger code";
+ TODO: {
+ local $TODO = "Not yet supported by ResultSetManger code";
can_ok( $rsobj1, 'loader_test1_rsmeth' ) or $skip_rsmeth = 1;
- }
+ }
SKIP: {
skip "Pre-requisite test failed", 1 if $skip_tab;
isa_ok( $obj4->fkid, $class3);
my $obj3 = $rsobj3->find(1);
- my $rs_rel4 = $obj3->search_related('loader_test4s');
+ my $rs_rel4 = $obj3->search_related('loader_test4zes');
isa_ok( $rs_rel4->first, $class4);
# fk def in comments should not be parsed