From: Rafael Kitover Date: Sat, 1 May 2010 21:47:32 +0000 (-0400) Subject: normalize accessor names for CamelCase columns in v7 mode X-Git-Tag: 0.07000~39 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f3a657ef586cfc7a47a4b72080acd18d9f9c305b;p=dbsrgits%2FDBIx-Class-Schema-Loader.git normalize accessor names for CamelCase columns in v7 mode --- diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index d5e70a8..61c8f4f 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -142,6 +142,10 @@ How to name relationship accessors. How to name Result classes. +=item column_accessors + +How to name column accessors in Result classes. + =back The values can be: @@ -180,6 +184,9 @@ table names is also done correctly. CamelCase column names in case-preserving mode will also be handled correctly for relationship name inflection. See L. +In this mode, CamelCase L are normalized based on case +transition instead of just being lowercased, so C becomes C. + If you don't have any CamelCase table or column names, you can upgrade without breaking any of your code. @@ -551,6 +558,7 @@ sub new { $self->{naming} = { relationships => $naming_ver, monikers => $naming_ver, + column_accessors => $naming_ver, }; } @@ -691,8 +699,9 @@ EOF last; } - $self->naming->{relationships} ||= $v; - $self->naming->{monikers} ||= $v; + $self->naming->{relationships} ||= $v; + $self->naming->{monikers} ||= $v; + $self->naming->{column_accessors} ||= $v; $self->schema_version_to_dump($real_ver); @@ -1471,6 +1480,12 @@ sub _resolve_col_accessor_collisions { } } +sub _make_column_accessor_name { + my ($self, $column_name) = @_; + + return join '_', map lc, split /(?<=[[:lower:]])[\W_]*(?=[[:upper:]])|[\W_]+/, $column_name; +} + # Set up metadata (cols, pks, etc) sub _setup_src_meta { my ($self, $table) = @_; @@ -1494,8 +1509,14 @@ sub _setup_src_meta { my $col_info = $self->__columns_info_for($table); if ($self->preserve_case) { for my $col (keys %$col_info) { - $col_info->{$col}{accessor} = lc $col - if $col ne lc($col); + if ($col ne lc($col)) { + if ((not exists $self->naming->{column_accessors}) || (($self->naming->{column_accessors} =~ /(\d+)/)[0] >= 7)) { + $col_info->{$col}{accessor} = $self->_make_column_accessor_name($col); + } + else { + $col_info->{$col}{accessor} = lc $col; + } + } } } else { diff --git a/t/25backcompat.t b/t/25backcompat.t index 75fad57..2e1213d 100644 --- a/t/25backcompat.t +++ b/t/25backcompat.t @@ -1105,6 +1105,9 @@ sub run_v4_tests { isa_ok eval { $foo->email_to_ids }, 'DBIx::Class::ResultSet', 'correct rel name inflection in 0.04006 mode'; + + ok (($schema->resultset('Routechange')->find(1)->can('quuxsid')), + 'correct column accessor in 0.04006 mode'); } sub run_v5_tests { @@ -1129,6 +1132,9 @@ sub run_v5_tests { isa_ok eval { $foo->email_to_ids }, 'DBIx::Class::ResultSet', 'correct rel name inflection in v5 mode'; + + ok (($schema->resultset('Routechange')->find(1)->can('quuxsid')), + 'correct column accessor in v5 mode'); } sub run_v6_tests { @@ -1158,6 +1164,9 @@ sub run_v6_tests { isa_ok eval { $route_change->quuxsid }, $res->{classes}{quuxs}, 'correct rel name in v6 mode'; + + ok (($schema->resultset('Routechange')->find(1)->can('quuxsid')), + 'correct column accessor in v6 mode'); } sub run_v7_tests { @@ -1187,6 +1196,9 @@ sub run_v7_tests { isa_ok eval { $route_change->quux }, $res->{classes}{quuxs}, 'correct rel name based on mixed-case column name in current mode'; + + ok (($schema->resultset('RouteChange')->find(1)->can('quuxs_id')), + 'correct column accessor in current mode'); } { diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 7b83ea9..cceab93 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -635,7 +635,7 @@ sub test_schema { eval { $rsobj5->find({id1 => 1, id2 => 1}) }; die $@ if $@; - is( $obj5->id2, 1, "Find on multi-col PK" ); + is( (eval { $obj5->id2 } || eval { $obj5->i_d2 }), 1, "Find on multi-col PK" ); # mulit-col fk def my $obj6 = $rsobj6->find(1);