File Coverage

blib/lib/User/Identity/Item.pm
Criterion Covered Total %
statement 60 66 90.9
branch 23 38 60.5
condition 10 20 50.0
subroutine 13 16 81.2
pod 11 12 91.6
total 117 152 76.9


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution User-Identity version 4.00.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2003-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package User::Identity::Item;{
13             our $VERSION = '4.00';
14             }
15              
16              
17 3     3   1731 use strict;
  3         9  
  3         148  
18 3     3   22 use warnings;
  3         8  
  3         225  
19              
20 3     3   1967 use Log::Report 'user-identity';
  3         406111  
  3         19  
21              
22 3     3   1145 use Scalar::Util qw/weaken/;
  3         7  
  3         4390  
23              
24             #--------------------
25              
26             sub new(@)
27 15     15 1 470911 { my $class = shift;
28 15 100       53 @_ or return undef; # no empty users.
29              
30 14 100       61 unshift @_, 'name' if @_ %2; # odd-length list: starts with nick
31              
32 14         106 my %args = @_;
33 14         82 my $self = (bless {}, $class)->init(\%args);
34              
35 14 50       48 if(my @missing = keys %args)
36 0         0 { warning __xn"unknown option '{name}' for {class}.", "unknown options {names} for {class}",
37             scalar @missing, name => $missing[0], names => \@missing, class => $class;
38             }
39              
40 14         59 $self;
41             }
42              
43             sub init($)
44 14     14 0 32 { my ($self, $args) = @_;
45              
46             $self->{UII_name} = delete $args->{name}
47 14   33     162 // error __x"each item requires a name.";
48              
49 14         36 $self->{UII_description} = delete $args->{description};
50 14         33 $self;
51             }
52              
53             #--------------------
54              
55             sub name(;$)
56 63     63 1 1623 { my $self = shift;
57 63 50       352 @_ ? ($self->{UII_name} = shift) : $self->{UII_name};
58             }
59              
60              
61 0     0 1 0 sub description() { $_[0]->{UII_description} }
62              
63             #--------------------
64              
65             our %collectors = (
66             emails => 'User::Identity::Collection::Emails',
67             locations => 'User::Identity::Collection::Locations',
68             systems => 'User::Identity::Collection::Systems',
69             users => 'User::Identity::Collection::Users',
70             ); # *s is tried as well, so email, system, and location will work
71              
72             sub addCollection(@)
73 2     2 1 5 { my $self = shift;
74 2 50       6 @_ or return;
75              
76 2         4 my $object;
77 2 100       6 if(ref $_[0])
78 1         2 { $object = shift;
79 1 50       7 $object->isa('User::Identity::Collection') or error __x"this {object} is not a collection.", object => $object;
80             }
81             else
82 1 50       3 { unshift @_, 'type' if @_ % 2;
83 1         3 my %args = @_;
84 1 50       3 my $type = delete $args{type} or panic "no collection type specified";
85              
86 1   33     6 my $class = $collectors{$type} || $collectors{$type.'s'} || $type;
87 1         83 eval "require $class";
88 1 50       5 $@ and error __x"cannot load collection module {type} ({class}): {err}", type => $type, class => $class, err => $@;
89              
90 1         8 $object = $class->new(%args);
91             }
92              
93 2         8 $object->parent($self);
94 2         7 $self->{UI_col}{$object->name} = $object;
95             }
96              
97              
98              
99             sub removeCollection($)
100 0     0 1 0 { my $self = shift;
101 0 0       0 my $name = ref $_[0] ? $_[0]->name : $_[0];
102              
103             delete $self->{UI_col}{$name}
104 0 0       0 || delete $self->{UI_col}{$name.'s'};
105             }
106              
107              
108              
109             sub collection($;$)
110 6     6 1 1699 { my $self = shift;
111 6         10 my $collname = shift;
112 6   100     37 my $collection = $self->{UI_col}{$collname} || $self->{UI_col}{$collname.'s'} || return;
113              
114 4 50       16 wantarray ? $collection->roles : $collection;
115             }
116              
117              
118             sub add($$)
119 2     2 1 249 { my ($self, $collname) = (shift, shift);
120 2 50 33     9 my $collection
      66        
121             = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
122             : ($self->collection($collname) || $self->addCollection($collname));
123              
124 2 50       21 defined $collection
125             or error __x"invalid collection {name}.", name => $collname;
126              
127 2         6 $collection->addRole(@_);
128             }
129              
130              
131 0     0 1 0 sub type { "item" }
132              
133              
134             sub parent(;$)
135 18     18 1 1260 { my $self = shift;
136 18 100       94 @_ or return $self->{UII_parent};
137              
138 6         15 $self->{UII_parent} = shift;
139 6         11 weaken($self->{UII_parent});
140 6         15 $self->{UII_parent};
141             }
142              
143              
144             sub user()
145 9     9 1 993 { my $self = shift;
146 9         22 my $parent = $self->parent;
147 9 100       43 defined $parent ? $parent->user : undef;
148             }
149              
150             #--------------------
151              
152             sub find($$)
153 5     5 1 2740 { my $all = shift->{UI_col};
154 5         7 my $collname = shift;
155             my $collection
156             = ref $collname && $collname->isa('User::Identity::Collection') ? $collname
157 5 50 33     33 : ($all->{$collname} || $all->{$collname.'s'});
      66        
158              
159 5 100       28 defined $collection ? $collection->find(shift) : ();
160             }
161              
162             1;