File Coverage

blib/lib/User/Identity/Collection.pm
Criterion Covered Total %
statement 56 77 72.7
branch 10 32 31.2
condition 2 3 66.6
subroutine 16 21 76.1
pod 8 9 88.8
total 92 142 64.7


line stmt bran cond sub pod time code
1             # Copyrights 2003-2020 by [Mark Overmeer ].
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.02.
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     1   8 use vars '$VERSION';
  1         2  
  1         55  
11             $VERSION = '1.00';
12              
13 1     1   5 use base 'User::Identity::Item';
  1         2  
  1         444  
14              
15 1     1   7 use strict;
  1         3  
  1         20  
16 1     1   5 use warnings;
  1         2  
  1         23  
17              
18 1     1   426 use User::Identity;
  1         3  
  1         33  
19 1     1   8 use Carp;
  1         11  
  1         70  
20 1     1   8 use List::Util qw/first/;
  1         3  
  1         148  
21              
22              
23             use overload '""' => sub {
24 29     29   359 my $self = shift;
25 29         67 $self->name . ": " . join(", ", sort map {$_->name} $self->roles);
  29         58  
26 1     1   8 };
  1         2  
  1         7  
27              
28              
29 1     1   75 use overload '@{}' => sub { [ shift->roles ] };
  1     5   2  
  1         6  
  5         15  
30              
31             #-----------------------------------------
32              
33              
34 0     0 1 0 sub type { "people" }
35              
36              
37             sub init($)
38 2     2 0 6 { my ($self, $args) = @_;
39              
40 2 50       9 defined($self->SUPER::init($args)) or return;
41            
42 2 50       8 $self->{UIC_itype} = delete $args->{item_type} or die;
43 2         8 $self->{UIC_roles} = { };
44 2         4 my $roles = $args->{roles};
45            
46             my @roles
47 2 0       8 = ! defined $roles ? ()
    50          
48             : ref $roles eq 'ARRAY' ? @$roles
49             : $roles;
50            
51 2         7 $self->addRole($_) foreach @roles;
52 2         6 $self;
53             }
54              
55             #-----------------------------------------
56              
57              
58 37     37 1 1079 sub roles() { values %{shift->{UIC_roles}} }
  37         146  
59              
60              
61 3     3 1 8 sub itemType { shift->{UIC_itype} }
62              
63             #-----------------------------------------
64              
65              
66             sub addRole(@)
67 3     3 1 7 { my $self = shift;
68 3         12 my $maintains = $self->itemType;
69              
70 3         6 my $role;
71 3 100 66     19 if(ref $_[0] && ref $_[0] ne 'ARRAY')
72 1         3 { $role = shift;
73 1 50       7 croak "ERROR: Wrong type of role for ".ref($self)
74             . ": requires a $maintains but got a ". ref($role)
75             unless $role->isa($maintains);
76             }
77             else
78 2 50       7 { $role = $maintains->new(ref $_[0] ? @{$_[0]} : @_);
  2         11  
79 2 50       7 croak "ERROR: Cannot create a $maintains to add this to my collection."
80             unless defined $role;
81             }
82              
83 3         28 $role->parent($self);
84 3         14 $self->{UIC_roles}{$role->name} = $role;
85 3         12 $role;
86             }
87              
88              
89             sub removeRole($)
90 0     0 1 0 { my ($self, $which) = @_;
91 0 0       0 my $name = ref $which ? $which->name : $which;
92 0 0       0 my $role = delete $self->{UIC_roles}{$name} or return ();
93 0         0 $role->parent(undef);
94 0         0 $role;
95             }
96              
97              
98             sub renameRole($$$)
99 0     0 1 0 { my ($self, $which, $newname) = @_;
100 0 0       0 my $name = ref $which ? $which->name : $which;
101              
102 0 0       0 if(exists $self->{UIC_roles}{$newname})
103 0         0 { $self->log(ERROR=>"Cannot rename $name into $newname: already exists");
104 0         0 return ();
105             }
106              
107 0         0 my $role = delete $self->{UIC_roles}{$name};
108 0 0       0 unless(defined $role)
109 0         0 { $self->log(ERROR => "Cannot rename $name into $newname: doesn't exist");
110 0         0 return ();
111             }
112              
113 0         0 $role->name($newname); # may imply change other attributes.
114 0         0 $self->{UIC_roles}{$newname} = $role;
115             }
116              
117              
118 0     0 1 0 sub sorted() { sort {$a->name cmp $b->name} shift->roles}
  0         0  
119              
120             #-----------------------------------------
121              
122              
123             sub find($)
124 3     3 1 10 { my ($self, $select) = @_;
125              
126             !defined $select ? ($self->roles)[0]
127             : !ref $select ? $self->{UIC_roles}{$select}
128 0           : wantarray ? grep ({ $select->($_, $self) } $self->roles)
129 3 0   0   15 : first { $select->($_, $self) } $self->roles;
  0 50          
    50          
130             }
131              
132             1;
133