Remove Class::Data::Inheritable and use CAG 'inherited' style accessors
[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 use base 'DBIx::Class';
8
9 use Scalar::Util 'blessed';
10 use namespace::clean;
11
12 sub mk_group_accessors {
13     my ($class, $group, @cols) = @_;
14
15     foreach my $col (@cols) {
16         my($accessor, $col) = ref $col eq 'ARRAY' ? @$col : (undef, $col);
17
18         my($ro_meth, $wo_meth);
19         if (defined blessed $col and $col->isa('Class::DBI::Column')) {
20             $ro_meth = $col->accessor;
21             $wo_meth = $col->mutator;
22         }
23         elsif (defined $accessor and ($accessor ne $col)) {
24             $ro_meth = $wo_meth = $accessor;
25         }
26         else {
27             $ro_meth = $class->accessor_name_for($col);
28             $wo_meth = $class->mutator_name_for($col);
29         }
30
31         # warn "class: $class / col: $col / ro: $ro_meth / wo: $wo_meth\n";
32         if ($ro_meth eq $wo_meth or # they're the same
33             $wo_meth eq $col)     # or only the accessor is custom
34         {
35             $class->next::method($group => [ $ro_meth => $col ]);
36         }
37         else {
38             $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]);
39             $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]);
40         }
41     }
42 }
43
44
45 sub accessor_name_for {
46     my ($class, $column) = @_;
47     if ($class->can('accessor_name')) {
48         return $class->accessor_name($column)
49     }
50
51     return $column;
52 }
53
54 sub mutator_name_for {
55     my ($class, $column) = @_;
56     if ($class->can('mutator_name')) {
57         return $class->mutator_name($column)
58     }
59
60     return $column;
61 }
62
63
64 sub new {
65     my ($class, $attrs, @rest) = @_;
66     $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH';
67     foreach my $col ($class->columns) {
68         my $acc = $class->accessor_name_for($col);
69         $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc};
70
71         my $mut = $class->mutator_name_for($col);
72         $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut};
73     }
74     return $class->next::method($attrs, @rest);
75 }
76
77 1;