File: AccessorMapping.pm

package info (click to toggle)
libdbix-class-perl 0.08123-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,520 kB
  • ctags: 1,695
  • sloc: perl: 19,821; sql: 353; makefile: 10
file content (68 lines) | stat: -rw-r--r-- 1,838 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package # hide from PAUSE Indexer
  DBIx::Class::CDBICompat::AccessorMapping;

use strict;
use warnings;

sub mk_group_accessors {
    my ($class, $group, @cols) = @_;

    foreach my $col (@cols) {
        my($accessor, $col) = ref $col ? @$col : (undef, $col);

        my($ro_meth, $wo_meth);
        if( defined $accessor and ($accessor ne $col)) {
            $ro_meth = $wo_meth = $accessor;
        }
        else {
            $ro_meth = $class->accessor_name_for($col);
            $wo_meth = $class->mutator_name_for($col);
        }

        # warn "class: $class / col: $col / ro: $ro_meth / wo: $wo_meth\n";
        if ($ro_meth eq $wo_meth or # they're the same
            $wo_meth eq $col)     # or only the accessor is custom
        {
            $class->next::method($group => [ $ro_meth => $col ]);
        }
        else {
            $class->mk_group_ro_accessors($group => [ $ro_meth => $col ]);
            $class->mk_group_wo_accessors($group => [ $wo_meth => $col ]);
        }
    }
}


sub accessor_name_for {
    my ($class, $column) = @_;
    if ($class->can('accessor_name')) { 
        return $class->accessor_name($column) 
    }

    return $column;
}

sub mutator_name_for {
    my ($class, $column) = @_;
    if ($class->can('mutator_name')) { 
        return $class->mutator_name($column) 
    }

    return $column;
}


sub new {
    my ($class, $attrs, @rest) = @_;
    $class->throw_exception( "create needs a hashref" ) unless ref $attrs eq 'HASH';
    foreach my $col ($class->columns) {
        my $acc = $class->accessor_name_for($col);
        $attrs->{$col} = delete $attrs->{$acc} if exists $attrs->{$acc};

        my $mut = $class->mutator_name_for($col);
        $attrs->{$col} = delete $attrs->{$mut} if exists $attrs->{$mut};
    }
    return $class->next::method($attrs, @rest);
}

1;