| 1 | package # hide from PAUSE | ||||||||||||||||||||
| 2 | DBIx::Class::CDBICompat::ColumnGroups; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Sub::Name (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Storable 'dclone'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use List::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | ▶ | use base qw/DBIx::Class::Row/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | __PACKAGE__->mk_classdata('_column_groups' => { }); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | ▶ | sub columns { | ||||||||||||||||||
|
Subroutine
columns: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 15 | 0 | my $proto = shift; | |||||||||||||||||||
| 16 | 0 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||
|
Condition0%0/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 17 | 0 | ▶ | my $group = shift || "All"; | ||||||||||||||||||
|
Condition0%0/2
shift() || 'All'
MC/DC0%0/1
shift() || 'All'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 18 | 0 | $class->_init_result_source_instance(); | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | $class->_add_column_group($group => @_) if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 0 | ▶ | return $class->all_columns if $group eq "All"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | ▶ | return $class->primary_column if $group eq "Primary"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | my $grp = $class->_column_groups->{$group}; | |||||||||||||||||||
| 25 | 0 | my @grp_cols = sort { $grp->{$b} <=> $grp->{$a} } (keys %$grp); | |||||||||||||||||||
| 26 | 0 | return @grp_cols; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub _add_column_group { | ||||||||||||||||||
|
Subroutine
_add_column_group: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 30 | 0 | my ($class, $group, @cols) = @_; | |||||||||||||||||||
| 31 | 0 | $class->mk_group_accessors(column => @cols); | |||||||||||||||||||
| 32 | 0 | $class->add_columns(@cols); | |||||||||||||||||||
| 33 | 0 | $class->_register_column_group($group => @cols); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 0 | ▶ | sub add_columns { | ||||||||||||||||||
|
Subroutine
add_columns: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 37 | 0 | my ($class, @cols) = @_; | |||||||||||||||||||
| 38 | 0 | $class->result_source_instance->add_columns(@cols); | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | sub _register_column_group { | ||||||||||||||||||
|
Subroutine
_register_column_group: not called CC=9 SCAR=45.0
| |||||||||||||||||||||
| 42 | 0 | my ($class, $group, @cols) = @_; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # Must do a complete deep copy else column groups | ||||||||||||||||||||
| 45 | # might accidentally be shared. | ||||||||||||||||||||
| 46 | 0 | my $groups = dclone $class->_column_groups; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | ▶ | if ($group eq 'Primary') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | $class->set_primary_key(@cols); | |||||||||||||||||||
| 50 | 0 | delete $groups->{'Essential'}{$_} for @cols; | |||||||||||||||||||
| 51 | 0 | my $first = List::Util::max(values %{$groups->{'Essential'}}); | |||||||||||||||||||
| 52 | 0 | $groups->{'Essential'}{$_} = ++$first for reverse @cols; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | if ($group eq 'All') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 0 | ▶ | unless (exists $class->_column_groups->{'Primary'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $groups->{'Primary'}{$cols[0]} = 1; | |||||||||||||||||||
| 58 | 0 | $class->set_primary_key($cols[0]); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | 0 | ▶ | unless (exists $class->_column_groups->{'Essential'}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | $groups->{'Essential'}{$cols[0]} = 1; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | delete $groups->{$group}{$_} for @cols; | |||||||||||||||||||
| 66 | 0 | my $first = List::Util::max(values %{$groups->{$group}}); | |||||||||||||||||||
| 67 | 0 | $groups->{$group}{$_} = ++$first for reverse @cols; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | $class->_column_groups($groups); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # CDBI will never overwrite an accessor, but it only uses one | ||||||||||||||||||||
| 73 | # accessor for all column types. DBIC uses many different | ||||||||||||||||||||
| 74 | # accessor types so, for example, if you declare a column() | ||||||||||||||||||||
| 75 | # and then a has_a() for that same column it must overwrite. | ||||||||||||||||||||
| 76 | # | ||||||||||||||||||||
| 77 | # To make this work CDBICompat has decide if an accessor | ||||||||||||||||||||
| 78 | # method was put there by itself and only then overwrite. | ||||||||||||||||||||
| 79 | { | ||||||||||||||||||||
| 80 | 2 | my %our_accessors; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 4 | ▶ | sub _has_custom_accessor { | ||||||||||||||||||
|
Subroutine
_has_custom_accessor: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 83 | 4 | my($class, $name) = @_; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 4 | my $existing_accessor = *{$class .'::'. $name}{CODE}; | |||||||||||||||||||
| 87 | 4 | ▶ | return $existing_accessor && !$our_accessors{$existing_accessor}; | ||||||||||||||||||
|
Condition33%1/3
$existing_accessor && !$our_accessors{$existing_accessor}
MC/DC0%0/2
$existing_accessor && !$our_accessors{$existing_accessor}
$existing_accessor
!$our_accessors{$existing_accessor}
Truth table33%1/3
A && not(B)
A= $existing_accessorB= !$our_accessors{$existing_accessor}
| |||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 4 | ▶ | sub _deploy_accessor { | ||||||||||||||||||
|
Subroutine
_deploy_accessor: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 91 | 4 | my($class, $name, $accessor) = @_; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 4 | ▶ | return if $class->_has_custom_accessor($name); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | { | ||||||||||||||||||||
| 96 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 97 | 2 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 98 | 4 | my $fullname = join '::', $class, $name; | |||||||||||||||||||
| 99 | 4 | *$fullname = Sub::Name::subname $fullname, $accessor; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 4 | $our_accessors{$accessor}++; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 4 | return 1; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 2 | ▶ | sub _mk_group_accessors { | ||||||||||||||||||
|
Subroutine
_mk_group_accessors: called CC=5 SCAR=16.4
| |||||||||||||||||||||
| 109 | 2 | my ($class, $type, $group, @fields) = @_; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # So we don't have to do lots of lookups inside the loop. | ||||||||||||||||||||
| 112 | 2 | ▶ | my $maker = $class->can($type) unless ref $type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # warn "$class $type $group\n"; | ||||||||||||||||||||
| 115 | 2 | foreach my $field (@fields) { | |||||||||||||||||||
| 116 | 2 | ▶ | if( $field eq 'DESTROY' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 0 | carp("Having a data accessor named DESTROY in ". | |||||||||||||||||||
| 118 | "'$class' is unwise."); | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 2 | my $name = $field; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 2 | ▶ | ($name, $field) = @$field if ref $field; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 2 | my $accessor = $class->$maker($group, $field); | |||||||||||||||||||
| 126 | 2 | my $alias = "_${name}_accessor"; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # warn " $field $alias\n"; | ||||||||||||||||||||
| 129 | { | ||||||||||||||||||||
| 130 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 2 | $class->_deploy_accessor($name, $accessor); | |||||||||||||||||||
| 133 | 2 | $class->_deploy_accessor($alias, $accessor); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | ▶ | sub all_columns { return shift->result_source_instance->columns; } | ||||||||||||||||||
|
Subroutine
all_columns: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | sub primary_column { | ||||||||||||||||||
|
Subroutine
primary_column: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 141 | 0 | my ($class) = @_; | |||||||||||||||||||
| 142 | 0 | my @pri = $class->primary_columns; | |||||||||||||||||||
| 143 | 0 | ▶ | return wantarray ? @pri : $pri[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 0 | ▶ | sub _essential { | ||||||||||||||||||
|
Subroutine
_essential: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 147 | 0 | return shift->columns("Essential"); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 0 | ▶ | sub find_column { | ||||||||||||||||||
|
Subroutine
find_column: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 151 | 0 | my ($class, $col) = @_; | |||||||||||||||||||
| 152 | 0 | ▶ | return $col if $class->has_column($col); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 0 | ▶ | sub __grouper { | ||||||||||||||||||
|
Subroutine
__grouper: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 156 | 0 | my ($class) = @_; | |||||||||||||||||||
| 157 | 0 | my $grouper = { class => $class }; | |||||||||||||||||||
| 158 | 0 | return bless($grouper, 'DBIx::Class::CDBICompat::ColumnGroups::GrouperShim'); | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | ▶ | sub _find_columns { | ||||||||||||||||||
|
Subroutine
_find_columns: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 162 | 0 | my ($class, @col) = @_; | |||||||||||||||||||
| 163 | 0 | return map { $class->find_column($_) } @col; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | package # hide from PAUSE (should be harmless, no POD no Version) | ||||||||||||||||||||
| 167 | DBIx::Class::CDBICompat::ColumnGroups::GrouperShim; | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | ▶ | sub groups_for { | ||||||||||||||||||
|
Subroutine
groups_for: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 170 | 0 | my ($self, @cols) = @_; | |||||||||||||||||||
| 171 | 0 | my %groups; | |||||||||||||||||||
| 172 | 0 | foreach my $col (@cols) { | |||||||||||||||||||
| 173 | 0 | foreach my $group (keys %{$self->{class}->_column_groups}) { | |||||||||||||||||||
| 174 | 0 | ▶ | $groups{$group} = 1 if $self->{class}->_column_groups->{$group}->{$col}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | 0 | return keys %groups; | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 2 | 1; | |||||||||||||||||||