Merge branch 'master' into topic/constructor_rewrite
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / CDBICompat / AccessorMapping.pm
CommitLineData
c0e7b4e5 1package # hide from PAUSE Indexer
478e7094 2 DBIx::Class::CDBICompat::AccessorMapping;
ea2e61bf 3
4use strict;
5use warnings;
6
b8e1e21f 7sub mk_group_accessors {
478e7094 8 my ($class, $group, @cols) = @_;
9
10 foreach my $col (@cols) {
7da06023 11 my($accessor, $col) = ref $col ? @$col : (undef, $col);
12
13 my($ro_meth, $wo_meth);
8c1220a4 14 if( defined $accessor and ($accessor ne $col)) {
7da06023 15 $ro_meth = $wo_meth = $accessor;
16 }
17 else {
18 $ro_meth = $class->accessor_name_for($col);
19 $wo_meth = $class->mutator_name_for($col);
20 }
478e7094 21
22 # warn "class: $class / col: $col / ro: $ro_meth / wo: $wo_meth\n";
23 if ($ro_meth eq $wo_meth or # they're the same
8c1220a4 24 $wo_meth eq $col) # or only the accessor is custom
25 {
26 $class->next::method($group => [ $ro_meth => $col ]);
27 }
28 else {
29 $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]);
30 $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]);
31 }
ea2e61bf 32 }
ea2e61bf 33}
34
e60dc79f 35
05ccf064 36sub accessor_name_for {
37 my ($class, $column) = @_;
8273e845 38 if ($class->can('accessor_name')) {
39 return $class->accessor_name($column)
05ccf064 40 }
e60dc79f 41
05ccf064 42 return $column;
e60dc79f 43}
44
05ccf064 45sub mutator_name_for {
46 my ($class, $column) = @_;
8273e845 47 if ($class->can('mutator_name')) {
48 return $class->mutator_name($column)
05ccf064 49 }
e60dc79f 50
05ccf064 51 return $column;
e60dc79f 52}
53
54
75a23b3e 55sub new {
478e7094 56 my ($class, $attrs, @rest) = @_;
57 $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH';
58 foreach my $col ($class->columns) {
59 my $acc = $class->accessor_name_for($col);
60 $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc};
61
62 my $mut = $class->mutator_name_for($col);
63 $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut};
64 }
65 return $class->next::method($attrs, @rest);
9bc6db13 66}
67
ea2e61bf 681;