lib/Template/Plugin/TruncateByWord.pm

filter: Statementstmt 100% 37 / 37 Branchbran 60.0% 6 / 10 Conditioncond 71.4% 10 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 78.2% 61 / 78 CC 11 SCAR 16.5
CC
11
Cov
87%
filter
8.4
init
4.2
BEGIN
1.0
CRAP 5.2
↑ Summary
1 #
2 # $Id: TruncateByWord.pm,v 1.3 2008/06/20 06:17:12 oneroad Exp $
3 #
4 package Template::Plugin::TruncateByWord;
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.11';
10  
11 6 ▶use Template::Plugin::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use base 'Template::Plugin::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 our $FILTER_NAME_DEFAULT = 'truncate_by_word';
17 6 our $ORG_ENC_DEFAULT = 'utf8';
18  
19 54 ▶sub init {
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
20 54 my $self = shift;
21 54 $self->{_DYNAMIC} = 1;
22 54 ▶ $self->install_filter($self->{_CONFIG}->{name}||$FILTER_NAME_DEFAULT);
Condition66%2/3
$self->{'_CONFIG'}{'name'} || $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
l!l&&r!l&&!r
18360
MC/DC0%0/2
$self->{'_CONFIG'}{'name'} || $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
$self->{'_CONFIG'}{'name'} $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
Truth table66%2/3
A || B
A= $self->{'_CONFIG'}{'name'}B= $Template::Plugin::TruncateByWord::FILTER_NAME_DEFAULT
ABresult
1X1
011
000
23 54 ▶ $self->{_CONFIG}->{enc} ||= $self->{_ARGS}->[0] || $ORG_ENC_DEFAULT;
Condition66%4/6
$self->{"_CONFIG"}{"enc"} ||= $self->{"_ARGS"}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
l!l&&r!l&&!r
6480
$self->{'_ARGS'}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
l!l&&r!l&&!r
4080
MC/DC0%0/3
$self->{"_CONFIG"}{"enc"} ||= $self->{"_ARGS"}[0] || $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
$self->{"_CONFIG"}{"enc"} $self->{'_ARGS'}[0] $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
Truth table0%0/4
A ||= B || C
A= $self->{"_CONFIG"}{"enc"}B= $self->{'_ARGS'}[0]C= $Template::Plugin::TruncateByWord::ORG_ENC_DEFAULT
ABCresult
1XX1
01X1
0011
0000
24 54 return $self;
25 }
26  
27 54 ▶sub filter {
Subroutine filter: called CC=8 SCAR=21.2
Pod undocumented
28 54 my($self, $string, $args, $conf) = @_;
29  
30 54 ▶ return '' unless $string;
Branch: 1 branch
BranchTrueFalseTotal
unless $string 0 54 54
31  
32 # decode
33 54 my $org_enc;
34 54 ▶ unless ( utf8::is_utf8($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($string)) 54 0 54
35 54 $org_enc = $self->{_CONFIG}->{enc};
36 54 $string = Encode::decode($org_enc, $string);
37 }
38  
39 54 my $org_length = CORE::length($string);
40 54 ▶ my $length = $args->[0] || $org_length;
Condition66%2/3
$args->[0] || $org_length
l!l&&r!l&&!r
4860
MC/DC0%0/2
$args->[0] || $org_length
$args->[0] $org_length
Truth table66%2/3
A || B
A= $args->[0]B= $org_length
ABresult
1X1
011
000
41 54 ▶ return if $length =~ /\D/;
Branch: 1 branch
BranchTrueFalseTotal
if $length =~ /\D/ 0 54 54
42 54 $string = CORE::substr($string, 0, $length);
43  
44 54 ▶ my $suffix = $args->[1]||'';
Condition100%2/2
$args->[1] || ''
l!l
2430
MC/DC100%1/1
$args->[1] || ''
$args->[1]
Truth table100%2/2
A
A= $args->[1]
Aresult
11
00
45 # revive encode
46 54 ▶ $string = Encode::encode($org_enc, $string) if $org_enc;
Branch: 1 branch
BranchTrueFalseTotal
if $org_enc 54 0 54
47 54 ▶ return $org_length > $length ? $string.$suffix : $string ;
Branch: 1 branch
BranchTrueFalseTotal
$org_length > $length ? : 42 12 54
48 }
49  
50 6 1;
51 __END__
↑ Summary