Simplify and speed up the accessor_name_for() code.
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / AccessorMapping.pm
1 package # hide from PAUSE Indexer
2     DBIx::Class::CDBICompat::AccessorMapping;
3
4 use strict;
5 use warnings;
6
7 sub mk_group_accessors {
8   my ($class, $group, @cols) = @_;
9
10   foreach my $col (@cols) {
11     my $ro_meth = $class->accessor_name_for($col);
12     my $wo_meth = $class->mutator_name_for($col);
13
14     # warn "class: $class / col: $col / ro: $ro_meth / wo: $wo_meth\n";
15     if ($ro_meth eq $wo_meth or     # they're the same
16         $wo_meth eq $col)           # or only the accessor is custom
17     {
18       $class->next::method($group => [ $ro_meth => $col ]);
19     } else {
20       $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]);
21       $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]);
22     }
23   }
24 }
25
26
27 sub accessor_name_for {
28     my ($class, $column) = @_;
29     if ($class->can('accessor_name')) { 
30         return $class->accessor_name($column) 
31     }
32
33     return $column;
34 }
35
36 sub mutator_name_for {
37     my ($class, $column) = @_;
38     if ($class->can('mutator_name')) { 
39         return $class->mutator_name($column) 
40     }
41
42     return $column;
43 }
44
45
46 sub new {
47   my ($class, $attrs, @rest) = @_;
48   $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH';
49   foreach my $col ($class->columns) {
50       my $acc = $class->accessor_name_for($col);
51       $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc};
52
53       my $mut = $class->mutator_name_for($col);
54       $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut};
55   }
56   return $class->next::method($attrs, @rest);
57 }
58
59 1;