File Coverage

blib/lib/User/Identity/Item.pm
Criterion Covered Total %
statement 65 75 86.6
branch 25 44 56.8
condition 9 17 52.9
subroutine 14 17 82.3
pod 11 12 91.6
total 124 165 75.1


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::Item;
10 3     3   25 use vars '$VERSION';
  3         6  
  3         141  
11             $VERSION = '1.02';
12              
13              
14 3     3   16 use strict;
  3         4  
  3         71  
15 3     3   14 use warnings;
  3         6  
  3         90  
16              
17 3     3   16 use Scalar::Util qw/weaken/;
  3         12  
  3         147  
18 3     3   18 use Carp;
  3         6  
  3         2989  
19              
20              
21             sub new(@)
22 15     15 1 2620 { my $class = shift;
23 15 100       49 return undef unless @_; # no empty users.
24              
25 14 100       53 unshift @_, 'name' if @_ %2; # odd-length list: starts with nick
26              
27 14         85 my %args = @_;
28 14         63 my $self = (bless {}, $class)->init(\%args);
29              
30 14 50       49 if(my @missing = keys %args)
31 0         0 { local $" = '", "';
32 0 0       0 warn "WARNING: Unknown ".(@missing==1? 'option' : 'options' )
33             . " \"@missing\" for a $class\n";
34             }
35              
36 14         44 $self;
37             }
38              
39             sub init($)
40 14     14 0 28 { my ($self, $args) = @_;
41              
42 14 50       160 unless(defined($self->{UII_name} = delete $args->{name}))
43 0         0 { croak "ERROR: Each item requires a name";
44             }
45              
46 14         31 $self->{UII_description} = delete $args->{description};
47 14         30 $self;
48             }
49              
50             #-----------------------------------------
51              
52              
53             sub name(;$)
54 69     69 1 729 { my $self = shift;
55 69 50       393 @_ ? ($self->{UII_name} = shift) : $self->{UII_name};
56             }
57              
58             #-----------------------------------------
59              
60              
61 0     0 1 0 sub description() {shift->{UII_description}}
62              
63             #-----------------------------------------
64              
65              
66             our %collectors =
67             ( emails => 'User::Identity::Collection::Emails'
68             , locations => 'User::Identity::Collection::Locations'
69             , systems => 'User::Identity::Collection::Systems'
70             , users => 'User::Identity::Collection::Users'
71             ); # *s is tried as well, so email, system, and location will work
72              
73             sub addCollection(@)
74 2     2 1 5 { my $self = shift;
75 2 50       7 return unless @_;
76              
77 2         3 my $object;
78 2 100       6 if(ref $_[0])
79 1         2 { $object = shift;
80 1 50       4 croak "ERROR: $object is not a collection"
81             unless $object->isa('User::Identity::Collection');
82             }
83             else
84 1 50       7 { unshift @_, 'type' if @_ % 2;
85 1         3 my %args = @_;
86 1         3 my $type = delete $args{type};
87              
88 1 50       4 croak "ERROR: Don't know what type of collection you want to add"
89             unless $type;
90              
91 1   33     8 my $class = $collectors{$type} || $collectors{$type.'s'} || $type;
92 1         74 eval "require $class";
93 1 50       7 croak "ERROR: Cannot load collection module $type ($class); $@\n"
94             if $@;
95              
96 1         9 $object = $class->new(%args);
97 1 50       4 croak "ERROR: Creation of a collection via $class failed\n"
98             unless defined $object;
99             }
100              
101 2         11 $object->parent($self);
102 2         8 $self->{UI_col}{$object->name} = $object;
103             }
104              
105              
106              
107             sub removeCollection($)
108 0     0 1 0 { my $self = shift;
109 0 0       0 my $name = ref $_[0] ? $_[0]->name : $_[0];
110              
111             delete $self->{UI_col}{$name}
112 0 0       0 || delete $self->{UI_col}{$name.'s'};
113             }
114              
115              
116              
117             sub collection($;$)
118 6     6 1 1474 { my $self = shift;
119 6         13 my $collname = shift;
120             my $collection
121 6   100     38 = $self->{UI_col}{$collname} || $self->{UI_col}{$collname.'s'} || return;
122              
123 4 50       17 wantarray ? $collection->roles : $collection;
124             }
125              
126              
127              
128             sub add($$)
129 2     2 1 287 { my ($self, $collname) = (shift, shift);
130 2 50 33     13 my $collection
      66        
131             = ref $collname && $collname->isa('User::Identity::Collection')
132             ? $collname
133             : ($self->collection($collname) || $self->addCollection($collname));
134              
135 2 50       24 unless($collection)
136 0         0 { carp "No collection $collname";
137 0         0 return;
138             }
139              
140 2         22 $collection->addRole(@_);
141             }
142              
143              
144 0     0 1 0 sub type { "item" }
145              
146              
147             sub parent(;$)
148 18     18 1 939 { my $self = shift;
149 18 100       60 return $self->{UII_parent} unless @_;
150              
151 6         13 $self->{UII_parent} = shift;
152 6         22 weaken($self->{UII_parent});
153 6         13 $self->{UII_parent};
154             }
155              
156              
157             sub user()
158 9     9 1 932 { my $self = shift;
159 9         25 my $parent = $self->parent;
160 9 100       39 defined $parent ? $parent->user : undef;
161             }
162              
163             #-----------------------------------------
164              
165              
166             sub find($$)
167 5     5 1 2357 { my $all = shift->{UI_col};
168 5         10 my $collname = shift;
169             my $collection
170             = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
171 5 50 33     38 : ($all->{$collname} || $all->{$collname.'s'});
      66        
172              
173 5 100       27 defined $collection ? $collection->find(shift) : ();
174             }
175              
176             1;
177