From: Michael G Schwern Date: Tue, 4 Nov 2008 18:26:41 +0000 (+0000) Subject: [rt.cpan.org 36863] X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7da0602386cfd7c10aa24cfa053b32bdcb27c081;p=dbsrgits%2FDBIx-Class-Historic.git [rt.cpan.org 36863] Fix mk_group_accessors() to handle [$field, $name]. It ignores accessor/mutator_name_for() because if you pass in your own accessor name you probably mean it. --- diff --git a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm index 8b15db4..c09140f 100644 --- a/lib/DBIx/Class/CDBICompat/AccessorMapping.pm +++ b/lib/DBIx/Class/CDBICompat/AccessorMapping.pm @@ -8,8 +8,16 @@ sub mk_group_accessors { my ($class, $group, @cols) = @_; foreach my $col (@cols) { - my $ro_meth = $class->accessor_name_for($col); - my $wo_meth = $class->mutator_name_for($col); + my($accessor, $col) = ref $col ? @$col : (undef, $col); + + my($ro_meth, $wo_meth); + if( defined $accessor ) { + $ro_meth = $wo_meth = $accessor; + } + else { + $ro_meth = $class->accessor_name_for($col); + $wo_meth = $class->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 diff --git a/t/cdbi-t/mk_group_accessors.t b/t/cdbi-t/mk_group_accessors.t new file mode 100644 index 0000000..3a04ff5 --- /dev/null +++ b/t/cdbi-t/mk_group_accessors.t @@ -0,0 +1,71 @@ +#!/usr/bin/perl -w + +use strict; +use Test::More; + +BEGIN { + eval "use DBIx::Class::CDBICompat;"; + plan skip_all => 'Class::Trigger and DBIx::ContextualFetch required' if $@; + + eval "use DBD::SQLite"; + plan skip_all => 'needs DBD::SQLite for testing' if $@; + + plan 'no_plan'; +} + +INIT { + use lib 't/testlib'; + require Film; +} + +sub Film::get_test { + my $self = shift; + my $key = shift; + $self->{get_test}++; + return $self->{$key}; +} + +sub Film::set_test { + my($self, $key, $val) = @_; + $self->{set_test}++; + return $self->{$key} = $val; +} + + +my $film = Film->create({ Title => "No Wolf McQuade" }); + +# Test mk_group_accessors() with a list of fields. +{ + Film->mk_group_accessors(test => qw(foo bar)); + $film->foo(42); + is $film->foo, 42; + + $film->bar(23); + is $film->bar, 23; +} + + +# An explicit accessor passed to mk_group_accessors should +# ignore accessor/mutator_name_for. +sub Film::accessor_name_for { + my($class, $col) = @_; + return "hlaglagh" if $col eq "wibble"; + return $col; +} + +sub Film::mutator_name_for { + my($class, $col) = @_; + return "hlaglagh" if $col eq "wibble"; + return $col; +} + + +# Test with a mix of fields and field specs +{ + Film->mk_group_accessors(test => ("baz", [wibble_thing => "wibble"])); + $film->baz(42); + is $film->baz, 42; + + $film->wibble_thing(23); + is $film->wibble_thing, 23; +}