From: Rafael Kitover Date: Thu, 13 May 2010 11:42:20 +0000 (-0400) Subject: fix splitting uppercase names X-Git-Tag: 0.07000~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cc4f11a26119d73c6af01bef015c6b5f1b98d189;p=dbsrgits%2FDBIx-Class-Schema-Loader.git fix splitting uppercase names --- diff --git a/TODO b/TODO index a8846e2..0a020f3 100644 --- a/TODO +++ b/TODO @@ -30,10 +30,7 @@ - add hashref form of generate_pod to control which POD is generated - add hashref form of components to control which components are added to which classes - - add original => {} to all type info rewrites - add common tests for preserve_case option - - correct handling of CamelCase names with numbers (eg. foo2Bar -> Foo2Bar, - foo2_bar) - check rel accessors for method conflicts - add an option to add extra code to Result classes diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 659017b..090e429 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -20,7 +20,7 @@ use Class::Inspector (); use Data::Dumper::Concise; use Scalar::Util 'looks_like_number'; use File::Slurp 'slurp'; -use DBIx::Class::Schema::Loader::Constants 'BY_CASE_TRANSITION'; +use DBIx::Class::Schema::Loader::Utils 'split_name'; require DBIx::Class; our $VERSION = '0.07000'; @@ -1483,7 +1483,7 @@ sub _resolve_col_accessor_collisions { sub _make_column_accessor_name { my ($self, $column_name) = @_; - return join '_', map lc, split BY_CASE_TRANSITION, $column_name; + return join '_', map lc, split_name $column_name; } # Set up metadata (cols, pks, etc) @@ -1602,7 +1602,7 @@ sub _default_table2moniker { return join '', map ucfirst, split /\W+/, $inflected; } - my @words = map lc, split BY_CASE_TRANSITION, $table; + my @words = map lc, split_name $table; my $as_phrase = join ' ', @words; my $inflected = Lingua::EN::Inflect::Phrase::to_S($as_phrase); diff --git a/lib/DBIx/Class/Schema/Loader/Constants.pm b/lib/DBIx/Class/Schema/Loader/Constants.pm deleted file mode 100644 index a1d9af2..0000000 --- a/lib/DBIx/Class/Schema/Loader/Constants.pm +++ /dev/null @@ -1,13 +0,0 @@ -package # hide from PAUSE - DBIx::Class::Schema::Loader::Constants; - -use strict; -use warnings; -use Exporter 'import'; - -our @EXPORT_OK = qw/BY_CASE_TRANSITION/; - -use constant BY_CASE_TRANSITION => - qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/; - -1; diff --git a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm index 7847c77..a12c035 100644 --- a/lib/DBIx/Class/Schema/Loader/RelBuilder.pm +++ b/lib/DBIx/Class/Schema/Loader/RelBuilder.pm @@ -5,7 +5,7 @@ use warnings; use Class::C3; use Carp::Clan qw/^DBIx::Class/; use Lingua::EN::Inflect::Phrase (); -use DBIx::Class::Schema::Loader::Constants 'BY_CASE_TRANSITION'; +use DBIx::Class::Schema::Loader::Utils 'split_name'; our $VERSION = '0.07000'; @@ -216,7 +216,7 @@ sub _remote_attrs { sub _normalize_name { my ($self, $name) = @_; - my @words = split BY_CASE_TRANSITION, $name; + my @words = split_name $name; return join '_', map lc, @words; } diff --git a/lib/DBIx/Class/Schema/Loader/Utils.pm b/lib/DBIx/Class/Schema/Loader/Utils.pm new file mode 100644 index 0000000..aaea3aa --- /dev/null +++ b/lib/DBIx/Class/Schema/Loader/Utils.pm @@ -0,0 +1,23 @@ +package # hide from PAUSE + DBIx::Class::Schema::Loader::Utils; + +use strict; +use warnings; +use Exporter 'import'; + +our @EXPORT_OK = qw/split_name/; + +use constant BY_CASE_TRANSITION => + qr/(?<=[[:lower:]\d])[\W_]*(?=[[:upper:]])|[\W_]+/; + +use constant BY_NON_ALPHANUM => + qr/[\W_]+/; + +sub split_name($) { + my $name = shift; + + split $name =~ /[[:upper:]]/ && $name =~ /[[:lower:]]/ ? BY_CASE_TRANSITION : BY_NON_ALPHANUM, $name; +} + +1; +# vim:et sts=4 sw=4 tw=0: diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index c439224..2ac6097 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -633,12 +633,14 @@ sub test_schema { 'might_have does not have is_deferrable'); # find on multi-col pk - my $obj5 = - eval { $rsobj5->find({id1 => 1, iD2 => 1}) } || - eval { $rsobj5->find({id1 => 1, id2 => 1}) }; - die $@ if $@; - - is( (eval { $obj5->id2 } || eval { $obj5->i_d2 }), 1, "Find on multi-col PK" ); + if ($conn->_loader->preserve_case) { + my $obj5 = $rsobj5->find({id1 => 1, iD2 => 1}); + is $obj5->i_d2, 1, 'Find on multi-col PK'; + } + else { + my $obj5 = $rsobj5->find({id1 => 1, id2 => 1}); + is $obj5->id2, 1, 'Find on multi-col PK'; + } # mulit-col fk def my $obj6 = $rsobj6->find(1);