[Bast-commits] r5054 - in DBIx-Class/0.08/trunk: lib/DBIx/Class/CDBICompat t/cdbi-t

schwern at dev.catalyst.perl.org schwern at dev.catalyst.perl.org
Tue Nov 4 18:26:41 GMT 2008


Author: schwern
Date: 2008-11-04 18:26:41 +0000 (Tue, 04 Nov 2008)
New Revision: 5054

Added:
   DBIx-Class/0.08/trunk/t/cdbi-t/mk_group_accessors.t
Modified:
   DBIx-Class/0.08/trunk/lib/DBIx/Class/CDBICompat/AccessorMapping.pm
Log:
[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.

Modified: DBIx-Class/0.08/trunk/lib/DBIx/Class/CDBICompat/AccessorMapping.pm
===================================================================
--- DBIx-Class/0.08/trunk/lib/DBIx/Class/CDBICompat/AccessorMapping.pm	2008-11-04 18:26:29 UTC (rev 5053)
+++ DBIx-Class/0.08/trunk/lib/DBIx/Class/CDBICompat/AccessorMapping.pm	2008-11-04 18:26:41 UTC (rev 5054)
@@ -8,9 +8,17 @@
     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
               $wo_meth eq $col)     # or only the accessor is custom

Added: DBIx-Class/0.08/trunk/t/cdbi-t/mk_group_accessors.t
===================================================================
--- DBIx-Class/0.08/trunk/t/cdbi-t/mk_group_accessors.t	                        (rev 0)
+++ DBIx-Class/0.08/trunk/t/cdbi-t/mk_group_accessors.t	2008-11-04 18:26:41 UTC (rev 5054)
@@ -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;
+}




More information about the Bast-commits mailing list