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