X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FAccessorMapping.pm;h=e235440ec0c2292d35cf323ac3b582c90e5dd3a4;hb=5e0eea3522876a30453af24097507198bbbc9409;hp=8beaae6a63277b636ba8651849674c7813d3eed0;hpb=2d8ced3984d471f9e735619faeba3c55bd9867fa;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm index 8beaae6..e235440 100644 --- a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm +++ b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm @@ -1,74 +1,77 @@ package # hide from PAUSE Indexer - DBIx::Class::CDBICompat::AccessorMapping; + DBIx::Class::CDBICompat::AccessorMapping; use strict; use warnings; +use base 'DBIx::Class'; + +use Scalar::Util 'blessed'; +use namespace::clean; + sub mk_group_accessors { - my ($class, $group, @cols) = @_; - 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->_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 ]); - $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]); - } - } -} + my ($class, $group, @cols) = @_; -# 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_for") || $class->can("accessor_name"); -} + foreach my $col (@cols) { + my($accessor, $col) = ref $col eq 'ARRAY' ? @$col : (undef, $col); + + my($ro_meth, $wo_meth); + if (defined blessed $col and $col->isa('Class::DBI::Column')) { + $ro_meth = $col->accessor; + $wo_meth = $col->mutator; + } + elsif (defined $accessor and ($accessor ne $col)) { + $ro_meth = $wo_meth = $accessor; + } + else { + $ro_meth = $class->accessor_name_for($col); + $wo_meth = $class->mutator_name_for($col); + } -sub _can_mutator_name_for { - my $class = shift; - return $class->can("mutator_name_for") || $class->can("mutator_name"); + # 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 ]); + $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]); + } + } } -sub _try_accessor_name_for { - my($class, $column) = @_; - my $method = $class->_can_accessor_name_for; - return $column unless $method; - return $class->$method($column); +sub accessor_name_for { + my ($class, $column) = @_; + if ($class->can('accessor_name')) { + return $class->accessor_name($column) + } + + return $column; } -sub _try_mutator_name_for { - my($class, $column) = @_; +sub mutator_name_for { + my ($class, $column) = @_; + if ($class->can('mutator_name')) { + return $class->mutator_name($column) + } - my $method = $class->_can_mutator_name_for; - return $column unless $method; - return $class->$method($column); + return $column; } sub new { - my ($class, $attrs, @rest) = @_; - $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH'; - foreach my $col ($class->columns) { - 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_for) { - my $mut = $class->_try_mutator_name_for($col); - $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut}; + my ($class, $attrs, @rest) = @_; + $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH'; + foreach my $col ($class->columns) { + my $acc = $class->accessor_name_for($col); + $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc}; + + my $mut = $class->mutator_name_for($col); + $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut}; } - } - return $class->next::method($attrs, @rest); + return $class->next::method($attrs, @rest); } 1;