File Coverage

blib/lib/User/Identity/Collection.pm
Criterion Covered Total %
statement 60 81 74.0
branch 10 32 31.2
condition 2 3 66.6
subroutine 17 22 77.2
pod 8 9 88.8
total 97 147 65.9


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