User/Identity/Collection.pm

filter: Statementstmt 73.4% 58 / 79 Branchbran 31.2% 10 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 76.1% 16 / 21 Podpod 88.8% 8 / 9 totaltotal 65.0% 95 / 146 SCAR 36.8
CC
19
Cov
61%
renameRole
20.0
removeRole
12.0
find
7.5
CRAP 39.8
« User/Identity/Item.pm ↑ Summary User/Identity/Location.pm »
1 # Copyrights 2003-2022 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution User-Identity. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package User::Identity::Collection;
10 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = '1.01';
12  
13 1 ▶use base 'User::Identity::Item';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use User::Identity;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 use overload '""' => sub {
24 29 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
25 29 $self->name . ": " . join(", ", sort map {$_->name} $self->roles);
26 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 1 ▶use overload '@{}' => sub { [ shift->roles ] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 #-----------------------------------------
32  
33  
34 0 ▶sub type { "people" }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
35  
36  
37 sub init($)
38 2 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=6 SCAR=19.6
Pod undocumented
39  
40 2 ▶ defined($self->SUPER::init($args)) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->SUPER::init($args) or return 0 2 2
41
42 2 ▶ $self->{UIC_itype} = delete $args->{item_type} or die;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"UIC_itype"} = delete $args->{"item_type"} or die 0 2 2
43 2 $self->{UIC_roles} = { };
44 2 my $roles = $args->{roles};
45
46 my @roles
47 2 ▶ = ! defined $roles ? ()
Branch: 2 branches
BranchTrueFalseTotal
!defined($roles) ? : 2 0 2
ref $roles eq 'ARRAY' ? : 0 0 0
48 : ref $roles eq 'ARRAY' ? @$roles
49 : $roles;
50
51 2 $self->addRole($_) foreach @roles;
52 2 $self;
53 }
54  
55 #-----------------------------------------
56  
57  
58 37 ▶sub roles() { values %{shift->{UIC_roles}} }
Subroutine roles: called CC=1 SCAR=0.0
Pod documented
59  
60  
61 3 ▶sub itemType { shift->{UIC_itype} }
Subroutine itemType: called CC=1 SCAR=0.0
Pod documented
62  
63 #-----------------------------------------
64  
65  
66 sub addRole(@)
67 3 ▶{ my $self = shift;
Subroutine addRole: called CC=6 SCAR=18.2
Pod documented
68 3 my $maintains = $self->itemType;
69  
70 3 my $role;
71 3 ▶ if(ref $_[0] && ref $_[0] ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] ne 'ARRAY') { } 1 2 3
Condition66%2/3
ref $_[0] and ref $_[0] ne "ARRAY"
!ll&&!rl&&r
021
MC/DC50%1/2
ref $_[0] and ref $_[0] ne "ARRAY"
ref $_[0] ref $_[0] ne "ARRAY"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] ne "ARRAY"
ABresult
0X0
100
111
72 1 { $role = shift;
73 1 ▶ croak "ERROR: Wrong type of role for ".ref($self)
Branch: 1 branch
BranchTrueFalseTotal
unless $role->isa($maintains) 0 1 1
74 . ": requires a $maintains but got a ". ref($role)
75 unless $role->isa($maintains);
76 }
77 else
78 2 ▶ { $role = $maintains->new(ref $_[0] ? @{$_[0]} : @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
79 2 ▶ croak "ERROR: Cannot create a $maintains to add this to my collection."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $role 0 2 2
80 unless defined $role;
81 }
82  
83 3 $role->parent($self);
84 3 $self->{UIC_roles}{$role->name} = $role;
85 3 $role;
86 }
87  
88  
89 sub removeRole($)
90 0 ▶{ my ($self, $which) = @_;
Subroutine removeRole: not called CC=3 SCAR=24.8
Pod documented
91 0 ▶ my $name = ref $which ? $which->name : $which;
Branch: 1 branch
BranchTrueFalseTotal
ref $which ? : 0 0 0
92 0 ▶ my $role = delete $self->{UIC_roles}{$name} or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $role = delete $self->{"UIC_roles"}{$name} or return () 0 0 0
93 0 $role->parent(undef);
94 0 $role;
95 }
96  
97  
98 sub renameRole($$$)
99 0 ▶{ my ($self, $which, $newname) = @_;
Subroutine renameRole: not called CC=4 SCAR=30.0
Pod documented
100 0 ▶ my $name = ref $which ? $which->name : $which;
Branch: 1 branch
BranchTrueFalseTotal
ref $which ? : 0 0 0
101  
102 0 ▶ if(exists $self->{UIC_roles}{$newname})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"UIC_roles"}{$newname}) 0 0 0
103 0 { $self->log(ERROR=>"Cannot rename $name into $newname: already exists");
104 0 return ();
105 }
106  
107 0 my $role = delete $self->{UIC_roles}{$name};
108 0 ▶ unless(defined $role)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $role) 0 0 0
109 0 { $self->log(ERROR => "Cannot rename $name into $newname: doesn't exist");
110 0 return ();
111 }
112  
113 0 $role->name($newname); # may imply change other attributes.
114 0 $self->{UIC_roles}{$newname} = $role;
115 }
116  
117  
118 0 ▶sub sorted() { sort {$a->name cmp $b->name} shift->roles}
Subroutine sorted: not called CC=1 SCAR=6.9
Pod documented
119  
120 #-----------------------------------------
121  
122  
123 sub find($)
124 3 ▶{ my ($self, $select) = @_;
Subroutine find: called CC=4 SCAR=20.1
Pod documented
125  
126 !defined $select ? ($self->roles)[0]
127 : !ref $select ? $self->{UIC_roles}{$select}
128 0 : wantarray ? grep ({ $select->($_, $self) } $self->roles)
129 3 ▶ : first { $select->($_, $self) } $self->roles;
Subroutine __ANON__: not called CC=1 SCAR=2.2
Branch: 3 branches
BranchTrueFalseTotal
!defined($select) ? : 0 3 3
!ref($select) ? : 3 0 3
wantarray ? : 0 0 0
130 }
131  
132 1 1;
133  
« User/Identity/Item.pm ↑ Summary User/Identity/Location.pm »