X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FAccessorMapping.pm;h=1792a13a33db5ce4b6f32348597946d9c85f2046;hb=d03c070640a8a9a187ba4fa21d645287029abfca;hp=e1300a3488ffd8a7f17ab0cf8061d6ef3b31ff1a;hpb=147dd158cf91465b8a48adce738d56b85f7d1b9b;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm index e1300a3..1792a13 100644 --- a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm +++ b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm @@ -1,24 +1,22 @@ -package DBIx::Class::CDBICompat::AccessorMapping; +package # hide from PAUSE Indexer + DBIx::Class::CDBICompat::AccessorMapping; use strict; use warnings; -use NEXT; - sub mk_group_accessors { my ($class, $group, @cols) = @_; - unless ($class->can('accessor_name') || $class->can('mutator_name')) { + unless ($class->_can_accessor_name_for || $class->_can_mutator_name_for) { return $class->next::method($group => @cols); } foreach my $col (@cols) { - my $ro_meth = ($class->can('accessor_name') - ? $class->accessor_name($col) - : $col); - my $wo_meth = ($class->can('mutator_name') - ? $class->mutator_name($col) - : $col); - #warn "$col $ro_meth $wo_meth"; - if ($ro_meth eq $wo_meth) { + my $ro_meth = $class->_try_accessor_name_for($col); + my $wo_meth = $class->_try_mutator_name_for($col); + + # warn "class: $class / col: $col / ro: $ro_meth / wo: $wo_meth\n"; + if ($ro_meth eq $wo_meth or # they're the same + $wo_meth eq $col) # or only the accessor is custom + { $class->next::method($group => [ $ro_meth => $col ]); } else { $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]); @@ -27,23 +25,50 @@ sub mk_group_accessors { } } -sub create { +# CDBI 3.0.7 decided to change "accessor_name" and "mutator_name" to +# "accessor_name_for" and "mutator_name_for". This is recent enough +# that we should support both. CDBI does. +sub _can_accessor_name_for { + my $class = shift; + return $class->can("accessor_name") || $class->can("accessor_name_for"); +} + +sub _can_mutator_name_for { + my $class = shift; + return $class->can("mutator_name") || $class->can("mutator_name_for"); +} + +sub _try_accessor_name_for { + my($class, $column) = @_; + + my $method = $class->_can_accessor_name_for; + return $column unless $method; + return $class->$method($column); +} + +sub _try_mutator_name_for { + my($class, $column) = @_; + + my $method = $class->_can_mutator_name_for; + return $column unless $method; + return $class->$method($column); +} + + +sub new { my ($class, $attrs, @rest) = @_; - $class->throw( "create needs a hashref" ) unless ref $attrs eq 'HASH'; - $attrs = { %$attrs }; - my %att; + $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH'; foreach my $col ($class->columns) { - if ($class->can('accessor_name')) { - my $acc = $class->accessor_name($col); -#warn "$col $acc"; - $att{$col} = delete $attrs->{$acc} if exists $attrs->{$acc}; + if ($class->_can_accessor_name_for) { + my $acc = $class->_try_accessor_name_for($col); + $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc}; } - if ($class->can('mutator_name')) { - my $mut = $class->mutator_name($col); - $att{$col} = delete $attrs->{$mut} if exists $attrs->{$mut}; + if ($class->_can_mutator_name_for) { + my $mut = $class->_try_mutator_name_for($col); + $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut}; } } - return $class->next::method({ %$attrs, %att }, @rest); + return $class->next::method($attrs, @rest); } 1;