Thesaurus.pm

filter: Statementstmt 97.1% 69 / 71 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 92.7% 103 / 111 CC 17 SCAR 11.6
CC
17
Cov
91%
find
5.3
_find
5.0
delete
4.0
CRAP 3.2
↑ Summary Thesaurus/CSV.pm »
1 package Thesaurus;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 $VERSION = '0.23';
8  
9 3 ▶use Params::Validate qw( validate_with BOOLEAN );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new
12 6 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
13 6 my $class = shift;
14 6 my %p = validate_with( params => \@_,
15 spec => { ignore_case => { type => BOOLEAN, default => 0 },
16 },
17 allow_extra => 1,
18 );
19  
20 my $self = bless { params => { ignore_case => delete $p{ignore_case},
21 }
22 6 }, $class;
23  
24 6 $self->_init(%p);
25  
26 6 return bless $self, $class;
27 }
28  
29 sub _init
30 2 ▶{
Subroutine _init: called CC=1 SCAR=0.0
31 2 my $self = shift;
32  
33 2 $self->{data} = {};
34 }
35  
36 sub add
37 9 ▶{
Subroutine add: called CC=2 SCAR=6.9
Pod documented
38 9 my $self = shift;
39  
40 9 foreach my $list (@_)
41 {
42 16 $self->_add_list($list);
43 }
44 }
45  
46 sub _add_list
47 16 ▶{
Subroutine _add_list: called CC=2 SCAR=6.9
48 16 my $self = shift;
49 16 my $list = shift;
50  
51 16 my %items = $self->_hash_from_list($list);
52  
53 16 ▶ while ( my ($k, $v) = each %items )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %items) 72 16 88
54 {
55 72 $self->{data}{$k} = $v;
56 }
57 }
58  
59 sub _hash_from_list
60 16 ▶{
Subroutine _hash_from_list: called CC=2 SCAR=6.9
61 16 my $self = shift;
62 16 my $list = shift;
63  
64 # if we have any of these already, we need to create new lists for
65 # them
66 my @new =
67 16 map { $self->find($_) } $self->_normalize_list($list);
68  
69 # return a unique list
70 16 my @unique = keys %{ { map { $_ => 1 } @new, @$list } };
71  
72 # a hash of keys to values
73 16 ▶ return map { ( $self->{params}{ignore_case} ? lc $_ : $_ ) => \@unique } @unique;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'params'}{'ignore_case'} ? : 42 34 76
74 }
75  
76 sub find
77 79 ▶{
Subroutine find: called CC=4 SCAR=16.6
Pod documented
78 79 my $self = shift;
79  
80 79 my $lists = $self->_find(@_);
81  
82 79 ▶ if (@_ > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 79 79
83 {
84 0 ▶ return unless %$lists;
Branch: 1 branch
BranchTrueFalseTotal
unless %$lists 0 0 0
85 0 return %$lists;
86 }
87 else
88 {
89 79 ▶ return unless exists $lists->{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$lists{$_[0]} 0 79 79
90 79 return @{ $lists->{ $_[0] } };
91 }
92 }
93  
94 sub _find
95 79 ▶{
Subroutine _find: called CC=5 SCAR=16.1
96 79 my $self = shift;
97  
98 79 my %lists;
99 79 foreach my $key (@_)
100 {
101 79 ▶ my $search_key = $self->{params}{ignore_case} ? lc $key : $key;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'params'}{'ignore_case'} ? : 38 41 79
102  
103 # ignore duplicates
104 79 ▶ next if exists $lists{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $lists{$key} 0 79 79
105  
106 # Anonymize to keep people away from our lists!
107 $lists{$key} =
108 79 ▶ exists $self->{data}{$search_key} ? [ @{ $self->{data}{$search_key} } ] : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'data'}{$search_key} ? : 13 66 79
109 }
110  
111 79 return \%lists;
112 }
113  
114 sub delete
115 2 ▶{
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
116 2 my $self = shift;
117  
118 2 foreach my $item ( $self->_normalize_list(\@_) )
119 {
120 2 ▶ next unless exists $self->{data}{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{$item} 0 2 2
121  
122 2 foreach my $key ( @{ $self->{data}{$item} } )
123 {
124 12 delete $self->{data}{$key};
125 }
126 }
127 }
128  
129 sub all
130 1 ▶{
Subroutine all: called CC=3 SCAR=11.0
Pod documented
131 1 my $self = shift;
132  
133 1 my (%done, @data);
134 1 foreach my $key ( keys %{ $self->{data} } )
135 {
136 6 ▶ next if exists $done{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $done{$key} 4 2 6
137  
138 2 @done{ @{ $self->{data}{$key} } } = ();
139  
140 2 push @data, $self->{data}{$key};
141 }
142  
143 return @data
144 1 }
145  
146 sub _normalize_list
147 18 ▶{
Subroutine _normalize_list: called CC=2 SCAR=6.9
148 18 my $self = shift;
149 18 my $list = shift;
150  
151 18 ▶ return map { lc } @$list if $self->{params}{ignore_case};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"params"}{"ignore_case"} 8 10 18
152 10 return @$list;
153 }
154  
155 3 1;
156  
157 __END__
↑ Summary Thesaurus/CSV.pm »