| 1 | package DBIx::Class::ResultSet::PhoneticSearch; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use base 'DBIx::Class::ResultSet'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 12 | ▶ | sub search_phonetic { | ||||||||||
|
Subroutine
search_phonetic: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||
| 11 | 12 | my ( $self, $search, $attributes ) = @_; | |||||||||||
| 12 | |||||||||||||
| 13 | 12 | ▶ | $attributes ||= {}; | ||||||||||
|
Condition100%2/2
$attributes ||= {}
MC/DC100%1/1
$attributes ||= {}
$attributes
Truth table100%2/2
A
A= $attributes
| |||||||||||||
| 14 | |||||||||||||
| 15 | 12 | my $source = $self->result_source; | |||||||||||
| 16 | |||||||||||||
| 17 | my @search = | ||||||||||||
| 18 | 3 | ▶ | ref $search eq 'ARRAY' ? @{$search} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 19 | 12 | ▶ | : ref $search eq 'HASH' ? %{$search} | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | : croak 'search_phonetic takes an arrayref or a hashref'; | ||||||||||||
| 21 | |||||||||||||
| 22 | 12 | ▶ | my $type = ref $search eq 'ARRAY' ? '-or' : '-and'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | |||||||||||||
| 24 | 12 | my $query = []; | |||||||||||
| 25 | |||||||||||||
| 26 | 12 | ▶ | while ( my $column = shift @search ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 18 | my $value = shift @search; | |||||||||||
| 28 | 18 | $column =~ s/^(.*?\.)?(.*)$/$2/; | |||||||||||
| 29 | 18 | ▶ | my $prefix = $1 || q{}; | ||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||
| 30 | 18 | my $info = $source->column_info($column); | |||||||||||
| 31 | croak qq(Column '$column' is not a phonetic column) | ||||||||||||
| 32 | 17 | ▶ | unless ( my $config = $info->{phonetic_search} ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | |||||||||||||
| 34 | 16 | my $class = 'Text::Phonetic::' . $config->{algorithm}; | |||||||||||
| 35 | 16 | my $column = $column . '_phonetic_' . lc( $config->{algorithm} ); | |||||||||||
| 36 | 16 | $self->_require_class($class); | |||||||||||
| 37 | 16 | my $encoded_value = $class->new->encode($value); | |||||||||||
| 38 | |||||||||||||
| 39 | 16 | push(@{$query}, { "$prefix$column" => $encoded_value}); | |||||||||||
| 40 | |||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | 10 | return $self->search( { $type => $query }, $attributes ); | |||||||||||
| 44 | } | ||||||||||||
| 45 | |||||||||||||
| 46 | 1 | ▶ | sub update_phonetic_columns { | ||||||||||
|
Subroutine
update_phonetic_columns: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 47 | 1 | my ($self) = @_; | |||||||||||
| 48 | 1 | my $i = 0; | |||||||||||
| 49 | 1 | my $source = $self->result_source; | |||||||||||
| 50 | 1 | foreach my $column ( $source->columns ) { | |||||||||||
| 51 | 5 | $i += $self->update_phonetic_column($column); | |||||||||||
| 52 | } | ||||||||||||
| 53 | 1 | return $i; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 6 | ▶ | sub update_phonetic_column { | ||||||||||
|
Subroutine
update_phonetic_column: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 57 | 6 | my ( $self, $column ) = @_; | |||||||||||
| 58 | 6 | my $source = $self->result_source; | |||||||||||
| 59 | 6 | my $config = $source->column_info($column)->{phonetic_search}; | |||||||||||
| 60 | 6 | my $i; | |||||||||||
| 61 | 6 | ▶ | return 0 unless ($config); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 3 | my $class = 'Text::Phonetic::' . $config->{algorithm}; | |||||||||||
| 63 | 3 | my $phonetic_column = $column . '_phonetic_' . lc( $config->{algorithm} ); | |||||||||||
| 64 | 3 | $self->_require_class($class); | |||||||||||
| 65 | 3 | my $rs = $self->search( { $column => { '!=' => undef } } ); | |||||||||||
| 66 | |||||||||||||
| 67 | 3 | ▶ | while ( my $row = $rs->next ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 6 | $row->update( | |||||||||||
| 69 | { $phonetic_column => $class->new->encode( $row->$column ) } ); | ||||||||||||
| 70 | 6 | $i++; | |||||||||||
| 71 | } | ||||||||||||
| 72 | 3 | return $i; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 19 | ▶ | sub _require_class { | ||||||||||
|
Subroutine
_require_class: called CC=4 SCAR=17.3
| |||||||||||||
| 76 | 19 | my ($self, $class) = @_; | |||||||||||
| 77 | |||||||||||||
| 78 | 19 | ▶ | croak "class argument missing" if !defined $class; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | |||||||||||||
| 80 | 19 | $class =~ s|::|/|g; | |||||||||||
| 81 | 19 | $class .= ".pm"; | |||||||||||
| 82 | |||||||||||||
| 83 | 19 | ▶ | if ( !exists $::INC{$class} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | eval { require $class }; | |||||||||||
| 85 | 0 | ▶ | croak $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 19 | return; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 1 | 1; | |||||||||||