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-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::Item;
10 3     3   25 use vars '$VERSION';
  3         7  
  3         153  
11             $VERSION = '1.00';
12              
13              
14 3     3   19 use strict;
  3         7  
  3         75  
15 3     3   16 use warnings;
  3         6  
  3         111  
16              
17 3     3   30 use Scalar::Util qw/weaken/;
  3         8  
  3         155  
18 3     3   18 use Carp;
  3         5  
  3         3209  
19              
20              
21             sub new(@)
22 15     15 1 2773 { my $class = shift;
23 15 100       53 return undef unless @_; # no empty users.
24              
25 14 100       59 unshift @_, 'name' if @_ %2; # odd-length list: starts with nick
26              
27 14         90 my %args = @_;
28 14         69 my $self = (bless {}, $class)->init(\%args);
29              
30 14 50       51 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         47 $self;
37             }
38              
39             sub init($)
40 14     14 0 33 { my ($self, $args) = @_;
41              
42 14 50       166 unless(defined($self->{UII_name} = delete $args->{name}))
43 0         0 { croak "ERROR: Each item requires a name";
44             }
45              
46 14         33 $self->{UII_description} = delete $args->{description};
47 14         38 $self;
48             }
49              
50             #-----------------------------------------
51              
52              
53             sub name(;$)
54 69     69 1 837 { my $self = shift;
55 69 50       338 @_ ? ($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 4 { my $self = shift;
75 2 50       9 return unless @_;
76              
77 2         3 my $object;
78 2 100       6 if(ref $_[0])
79 1         2 { $object = shift;
80 1 50       6 croak "ERROR: $object is not a collection"
81             unless $object->isa('User::Identity::Collection');
82             }
83             else
84 1 50       6 { unshift @_, 'type' if @_ % 2;
85 1         4 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     9 my $class = $collectors{$type} || $collectors{$type.'s'} || $type;
92 1         65 eval "require $class";
93 1 50       6 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         10 $object->parent($self);
102 2         9 $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 1532 { my $self = shift;
119 6         12 my $collname = shift;
120             my $collection
121 6   100     40 = $self->{UI_col}{$collname} || $self->{UI_col}{$collname.'s'} || return;
122              
123 4 50       16 wantarray ? $collection->roles : $collection;
124             }
125              
126              
127              
128             sub add($$)
129 2     2 1 320 { 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       6 unless($collection)
136 0         0 { carp "No collection $collname";
137 0         0 return;
138             }
139              
140 2         11 $collection->addRole(@_);
141             }
142              
143              
144 0     0 1 0 sub type { "item" }
145              
146              
147             sub parent(;$)
148 18     18 1 36 { my $self = shift;
149 18 100       59 return $self->{UII_parent} unless @_;
150              
151 6         32 $self->{UII_parent} = shift;
152 6         26 weaken($self->{UII_parent});
153 6         14 $self->{UII_parent};
154             }
155              
156              
157             sub user()
158 9     9 1 954 { my $self = shift;
159 9         27 my $parent = $self->parent;
160 9 100       45 defined $parent ? $parent->user : undef;
161             }
162              
163             #-----------------------------------------
164              
165              
166             sub find($$)
167 5     5 1 2427 { my $all = shift->{UI_col};
168 5         11 my $collname = shift;
169             my $collection
170             = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
171 5 50 33     36 : ($all->{$collname} || $all->{$collname.'s'});
      66        
172              
173 5 100       33 defined $collection ? $collection->find(shift) : ();
174             }
175              
176             1;
177