Commit | Line | Data |
ea2e61bf |
1 | package DBIx::Class::CDBICompat::AccessorMapping; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
b8e1e21f |
6 | sub mk_group_accessors { |
7 | my ($class, $group, @cols) = @_; |
ea2e61bf |
8 | unless ($class->can('accessor_name') || $class->can('mutator_name')) { |
147dd158 |
9 | return $class->next::method($group => @cols); |
ea2e61bf |
10 | } |
11 | foreach my $col (@cols) { |
12 | my $ro_meth = ($class->can('accessor_name') |
13 | ? $class->accessor_name($col) |
14 | : $col); |
15 | my $wo_meth = ($class->can('mutator_name') |
16 | ? $class->mutator_name($col) |
17 | : $col); |
c687b87e |
18 | #warn "$col $ro_meth $wo_meth"; |
ea2e61bf |
19 | if ($ro_meth eq $wo_meth) { |
147dd158 |
20 | $class->next::method($group => [ $ro_meth => $col ]); |
ea2e61bf |
21 | } else { |
b8e1e21f |
22 | $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]); |
23 | $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]); |
ea2e61bf |
24 | } |
25 | } |
26 | } |
27 | |
75a23b3e |
28 | sub new { |
9bc6db13 |
29 | my ($class, $attrs, @rest) = @_; |
701da8c4 |
30 | $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH'; |
103647d5 |
31 | foreach my $col ($class->columns) { |
9bc6db13 |
32 | if ($class->can('accessor_name')) { |
33 | my $acc = $class->accessor_name($col); |
75a23b3e |
34 | $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc}; |
9bc6db13 |
35 | } |
36 | if ($class->can('mutator_name')) { |
37 | my $mut = $class->mutator_name($col); |
75a23b3e |
38 | $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut}; |
9bc6db13 |
39 | } |
40 | } |
75a23b3e |
41 | return $class->next::method($attrs, @rest); |
9bc6db13 |
42 | } |
43 | |
ea2e61bf |
44 | 1; |