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 | |
5e0eea35 |
7 | use base 'DBIx::Class'; |
8 | |
7ad80222 |
9 | use Scalar::Util 'blessed'; |
10 | use namespace::clean; |
11 | |
b8e1e21f |
12 | sub mk_group_accessors { |
478e7094 |
13 | my ($class, $group, @cols) = @_; |
14 | |
15 | foreach my $col (@cols) { |
7ad80222 |
16 | my($accessor, $col) = ref $col eq 'ARRAY' ? @$col : (undef, $col); |
7da06023 |
17 | |
18 | my($ro_meth, $wo_meth); |
7ad80222 |
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)) { |
7da06023 |
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 | } |
478e7094 |
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 |
8c1220a4 |
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 | } |
ea2e61bf |
41 | } |
ea2e61bf |
42 | } |
43 | |
e60dc79f |
44 | |
05ccf064 |
45 | sub accessor_name_for { |
46 | my ($class, $column) = @_; |
8273e845 |
47 | if ($class->can('accessor_name')) { |
48 | return $class->accessor_name($column) |
05ccf064 |
49 | } |
e60dc79f |
50 | |
05ccf064 |
51 | return $column; |
e60dc79f |
52 | } |
53 | |
05ccf064 |
54 | sub mutator_name_for { |
55 | my ($class, $column) = @_; |
8273e845 |
56 | if ($class->can('mutator_name')) { |
57 | return $class->mutator_name($column) |
05ccf064 |
58 | } |
e60dc79f |
59 | |
05ccf064 |
60 | return $column; |
e60dc79f |
61 | } |
62 | |
63 | |
75a23b3e |
64 | sub new { |
478e7094 |
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); |
9bc6db13 |
75 | } |
76 | |
ea2e61bf |
77 | 1; |