NestedGroups.pm

filter: Statementstmt 51.0% 74 / 145 Branchbran 18.1% 8 / 44 Conditioncond 34.7% 8 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 66.6% 14 / 21 Podpod 64.2% 9 / 14 totaltotal 44.8% 117 / 261 CC 38 SCAR 29.1
CC
38
Cov
42%
list
42.0
save
41.9
members
30.0
CRAP 18.4
↑ Summary NestedGroups/Member.pm »
1 package Set::NestedGroups;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Set::NestedGroups::Member;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 @ISA = qw();
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
12 1 @EXPORT = qw(
13
14 );
15 1 $VERSION = '0.01';
16  
17 # Constructor
18 1 ▶sub new {
Subroutine new: called CC=7 SCAR=30.1
Pod documented
19 1 my $proto=shift;
20 1 my $fh=shift;
21 1 ▶ my $class=ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
22 1 my $self = {};
23 1 bless($self,$class);
24
25 1 ▶ if(defined $fh){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) 0 1 1
26 0 ▶ if(ref($fh) eq "DBI::st"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq 'DBI::st') { } 0 0 0
27 0 $fh->execute();
28 0 ▶ for(my $i=0;$i<$fh->rows();$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $fh->rows) 0 0 0
29 0 my ($member,$group)=$fh->fetchrow();
30 0 $self->add($member,$group);
31 }
32 } else {
33 1 ▶ no strict "refs"; # Can't use strict here,
Subroutine BEGIN: called CC=1 SCAR=0.0
34 # incase called with (DATA) instead
35 # of \*DATA
36 0 $fh=to_filehandle($fh);
37 0 ▶ while(<$fh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
38 0 chomp;
39 0 ▶ last if(/^=$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^=$/ 0 0 0
40 0 my ($member,$group)=split(/=/,$_,2);
41 0 $self->add(unescape($member),unescape($group));
42 }
43 }
44 }
45 1 return $self;
46 }
47  
48 # Add a member to a group
49 2 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
50 2 my $self=shift;
51 2 my ($member,$group)=@_;
52 2 my $was= $self->{'MEMBERS'}{$member}{$group};
53 2 $self->{'MEMBERS'}{$member}{$group}=1;
54 2 return $was;
55 }
56  
57 # And remove a member from a group
58 0 ▶sub remove {
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
59 0 my $self=shift;
60 0 my ($member,$group)=@_;
61 0 my $was=$self->{'MEMBERS'}{$member}{$group};
62 0 delete $self->{'MEMBERS'}{$member}{$group};
63 0 $self->{'GROUPS'}{$group}--;
64 0 ▶ if($self->{'GROUPS'}{$group} == 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"GROUPS"}{$group} == 0) 0 0 0
65 0 delete $self->{'GROUPS'}{$group};
66 }
67 0 return $was;
68 }
69  
70 # Create some sort of list object
71 0 ▶sub list {
Subroutine list: not called CC=6 SCAR=37.4
Pod documented
72 0 my $self=shift;
73 0 my %options=@_;
74 0 my $member_list=new Set::NestedGroups::MemberList;
75 0 ▶ my $nogroups = $options{'-nogroups'} || 0;
Condition0%0/2
$options{'-nogroups'} || 0
l!l
00
MC/DC0%0/1
$options{'-nogroups'} || 0
$options{'-nogroups'}
Truth table0%0/2
A
A= $options{'-nogroups'}
Aresult
11
00
76
77 0 foreach my $user (keys %{$self->{'MEMBERS'}}){
78 0 ▶ next if($nogroups && $self->group($user));
Branch: 1 branch
BranchTrueFalseTotal
if $nogroups and $self->group($user) 0 0 0
Condition0%0/3
$nogroups and $self->group($user)
!ll&&!rl&&r
000
MC/DC0%0/2
$nogroups and $self->group($user)
$nogroups $self->group($user)
Truth table0%0/3
A and B
A= $nogroupsB= $self->group($user)
ABresult
0X0
100
111
79 0 foreach my $group ($self->groups($user,%options)){
80 0 $member_list->add($user,$group);
81 }
82 }
83 0 return $member_list;
84 }
85  
86 0 ▶sub to_filehandle {
Subroutine to_filehandle: not called CC=5 SCAR=29.7
Pod undocumented
87 1 ▶ no strict "refs"; # Can't use strict here,
Subroutine BEGIN: called CC=1 SCAR=0.0
88 # incase called with (DATA) instead
89 # of \*DATA
90 0 my $string = shift;
91 0 ▶ if ($string && !ref($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string and not ref $string) 0 0 0
Condition0%0/3
$string and not ref $string
!ll&&!rl&&r
000
MC/DC0%0/2
$string and not ref $string
$string not ref $string
Truth table0%0/3
A and not(B)
A= $stringB= not ref $string
ABresult
0X0
100
111
92 0 my($package) = caller(1);
93 0 ▶ my($tmp) = $string=~/[':]/ ? $string : "$package\:\:$string";
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /[':]/ ? : 0 0 0
94 0 ▶ return $tmp if defined(fileno($tmp));
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $tmp 0 0 0
95 }
96 0 return $string;
97 }
98  
99 # unescape URL-encoded data
100 0 ▶sub unescape {
Subroutine unescape: not called CC=1 SCAR=6.9
Pod undocumented
101 0 my($todecode) = @_;
102 0 $todecode =~ tr/+/ /; # pluses become spaces
103 0 $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
104 0 return $todecode;
105 }
106  
107 # URL-encode data
108 0 ▶sub escape {
Subroutine escape: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my($toencode) = @_;
110 0 $toencode=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
111 0 return $toencode;
112 }
113  
114 # Save the current object
115 0 ▶sub save {
Subroutine save: not called CC=7 SCAR=37.3
Pod documented
116 0 my $self=shift;
117 0 my $fh=shift;
118  
119 0 ▶ if(ref($fh) eq "DBI::st"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh eq 'DBI::st') { } 0 0 0
120 0 my $members=$self->list('-norecurse'=>1,-nomiddles=>0);
121 0 ▶ for(my $i=0;$i<$members->rows();$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $members->rows) 0 0 0
122 0 ▶ $fh->execute($members->next()) or return;
Branch: 1 branch
BranchTrueFalseTotal
$fh->execute($members->next) or return 0 0 0
123 }
124 0 return 1;
125 } else {
126 1 ▶ no strict "refs"; # Can't use strict here,
Subroutine BEGIN: called CC=1 SCAR=0.0
127 # incase called with (DATA) instead
128 # of \*DATA
129 0 $fh=to_filehandle($fh);
130  
131 0 my $members=$self->list('-norecurse'=>1,-nomiddles=>0);
132 0 ▶ for(my $i=0;$i<$members->rows();$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $members->rows) 0 0 0
133 0 my ($member,$group)=$members->next();
134 0 ▶ print $fh escape($member),'=',escape($group),"\n" or return;
Branch: 1 branch
BranchTrueFalseTotal
print $fh escape($member), "=", escape($group), "\n" or return 0 0 0
135 }
136 0 ▶ print $fh "=\n" or return;
Branch: 1 branch
BranchTrueFalseTotal
print $fh "=\n" or return 0 0 0
137 }
138 }
139  
140 # Check a member
141 4 ▶sub member {
Subroutine member: called CC=4 SCAR=18.7
Pod documented
142 4 my $self=shift;
143 4 my $member=shift;
144 4 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
145 0 my $want_group=shift;
146 0 foreach my $got_group ($self->groups($member,-norecurse=>0,-nomiddles=>0)){
147 0 ▶ return 1 if($got_group eq $want_group);
Branch: 1 branch
BranchTrueFalseTotal
if $got_group eq $want_group 0 0 0
148 }
149 0 return undef;
150 }
151
152 4 return (keys %{$self->{'MEMBERS'}{$member}})
153 }
154  
155 # Check a group
156 2 ▶sub group {
Subroutine group: called CC=1 SCAR=0.0
Pod documented
157 2 my $self=shift;
158 2 my $group=shift;
159  
160 return
161 2 grep {$_ eq $group} $self->allgroups();
162 }
163  
164 # Return all the members
165 2 ▶sub allmembers {
Subroutine allmembers: called CC=1 SCAR=0.0
Pod undocumented
166 2 my $self=shift;
167 2 return (keys %{$self->{'MEMBERS'}});
168 }
169  
170 # Return all the groups
171 2 ▶sub allgroups {
Subroutine allgroups: called CC=1 SCAR=0.0
Pod undocumented
172 2 my $self=shift;
173 2 my $group=shift;
174 2 my %seen;
175  
176 return
177 4 grep { !$seen{$_}++ }
178 2 map { keys %{$self->{'MEMBERS'}{$_}} }
179 $self->allmembers()
180 };
181  
182  
183 # Returns the groups a member belongs to
184 3 ▶sub groups {
Subroutine groups: called CC=9 SCAR=22.0
Pod documented
185 3 my $self=shift;
186 3 my $member=shift;
187 3 my %options=@_;
188 3 ▶ my $norecurse = $options{'-norecurse'} || 0;
Condition100%2/2
$options{'-norecurse'} || 0
l!l
12
MC/DC100%1/1
$options{'-norecurse'} || 0
$options{'-norecurse'}
Truth table100%2/2
A
A= $options{'-norecurse'}
Aresult
11
00
189 3 ▶ my $nomiddles= $options{'-nomiddles'} || 0;
Condition100%2/2
$options{'-nomiddles'} || 0
l!l
12
MC/DC100%1/1
$options{'-nomiddles'} || 0
$options{'-nomiddles'}
Truth table100%2/2
A
A= $options{'-nomiddles'}
Aresult
11
00
190
191 3 my %group=%{$self->{'MEMBERS'}{$member}};
192  
193 3 ▶ if(!$norecurse){
Branch: 1 branch
BranchTrueFalseTotal
unless ($norecurse) 2 1 3
194 2 my $again = 1;
195 2 ▶ while($again){
Branch: 1 branch
BranchTrueFalseTotal
if ($again) 4 2 6
196 4 $again=0;
197 4 foreach my $group (keys %group){
198 6 foreach my $newgroup ( keys %{$self->{'MEMBERS'}{$group}}){
199 4 ▶ if(!$group{$newgroup}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($group{$newgroup}) 2 2 4
200 2 $again=$group{$newgroup}=1;
201 }
202 }
203 }
204 }
205 }
206 3 ▶ return grep { !$nomiddles || !$self->member($_) }keys %group;
Condition100%3/3
$nomiddles && $self->member($_)
!ll&&!rl&&r
311
MC/DC100%2/2
$nomiddles && $self->member($_)
$nomiddles $self->member($_)
Truth table100%3/3
A && B
A= $nomiddlesB= $self->member($_)
ABresult
0X0
100
111
207 }
208  
209 # Returns the members in a group
210 0 ▶sub members {
Subroutine members: not called CC=5 SCAR=34.0
Pod documented
211 0 my $self=shift;
212 0 my $group=shift;
213 0 my %options=@_;
214 0 ▶ my $nomiddles= $options{'-nomiddles'} || 0;
Condition0%0/2
$options{'-nomiddles'} || 0
l!l
00
MC/DC0%0/1
$options{'-nomiddles'} || 0
$options{'-nomiddles'}
Truth table0%0/2
A
A= $options{'-nomiddles'}
Aresult
11
00
215 0 my %members;
216  
217 0 foreach my $member ($self -> allmembers()){
218 0 ▶ $members{$member}++ if(grep {$_ eq $group} $self->groups($member,%options));
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $group;} $self->groups($member, %options) 0 0 0
219 }
220  
221 0 ▶ return grep { !$nomiddles || !$self->group($_) }keys %members;
Condition0%0/3
$nomiddles && $self->group($_)
!ll&&!rl&&r
000
MC/DC0%0/2
$nomiddles && $self->group($_)
$nomiddles $self->group($_)
Truth table0%0/3
A && B
A= $nomiddlesB= $self->group($_)
ABresult
0X0
100
111
222 }
223  
224 # Autoload methods go after =cut, and are processed by the autosplit program.
225  
226 1 1;
227 __END__
↑ Summary NestedGroups/Member.pm »