Commit | Line | Data |
c0e7b4e5 |
1 | package # hide from PAUSE Indexer |
478e7094 |
2 | DBIx::Class::CDBICompat::AccessorMapping; |
ea2e61bf |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
b8e1e21f |
7 | sub mk_group_accessors { |
478e7094 |
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 | } |
ea2e61bf |
23 | } |
ea2e61bf |
24 | } |
25 | |
e60dc79f |
26 | |
05ccf064 |
27 | sub accessor_name_for { |
28 | my ($class, $column) = @_; |
29 | if ($class->can('accessor_name')) { |
30 | return $class->accessor_name($column) |
31 | } |
e60dc79f |
32 | |
05ccf064 |
33 | return $column; |
e60dc79f |
34 | } |
35 | |
05ccf064 |
36 | sub mutator_name_for { |
37 | my ($class, $column) = @_; |
38 | if ($class->can('mutator_name')) { |
39 | return $class->mutator_name($column) |
40 | } |
e60dc79f |
41 | |
05ccf064 |
42 | return $column; |
e60dc79f |
43 | } |
44 | |
45 | |
75a23b3e |
46 | sub new { |
478e7094 |
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); |
9bc6db13 |
57 | } |
58 | |
ea2e61bf |
59 | 1; |