File Coverage

blib/lib/Data/Person.pm
Criterion Covered Total %
statement 31 31 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 40 41 97.5


line stmt bran cond sub pod time code
1             package Data::Person;
2              
3 8     8   241181 use strict;
  8         17  
  8         320  
4 8     8   74 use warnings;
  8         13  
  8         469  
5              
6 8     8   3458 use Mo qw(build default is);
  8         3999  
  8         46  
7 8     8   22220 use Mo::utils 0.21 qw(check_length check_strings);
  8         105880  
  8         265  
8 8     8   5655 use Mo::utils::Array qw(check_array_object);
  8         21621  
  8         185  
9 8     8   4956 use Mo::utils::Number qw(check_positive_natural);
  8         22844  
  8         245  
10 8     8   4897 use Mo::utils::Email qw(check_email);
  8         1233728  
  8         328  
11 8     8   908 use Readonly;
  8         29  
  8         1936  
12              
13             Readonly::Array our @SEX => qw(female male unknown);
14              
15             our $VERSION = 0.06;
16              
17             has email => (
18             is => 'ro',
19             );
20              
21             has external_ids => (
22             default => [],
23             is => 'ro',
24             );
25              
26             has id => (
27             is => 'ro',
28             );
29              
30             has name => (
31             is => 'ro',
32             );
33              
34             has sex => (
35             is => 'ro',
36             );
37              
38             sub BUILD {
39 17     17 0 2094871 my $self = shift;
40              
41             # Check email.
42 17         97 check_email($self, 'email');
43              
44             # Check external_ids.
45 16         8432 check_array_object($self, 'external_ids', 'Data::ExternalId');
46              
47             # Check id.
48 14         234 check_positive_natural($self, 'id');
49              
50             # Check name.
51 13         235 check_length($self, 'name', 255);
52              
53             # Check sex.
54 12         213 check_strings($self, 'sex', \@SEX);
55              
56 12         278 return;
57             }
58              
59             1;
60              
61             __END__
62              
63             =pod
64              
65             =encoding utf8
66              
67             =head1 NAME
68              
69             Data::Person - Data object for person.
70              
71             =head1 SYNOPSIS
72              
73             use Data::Person;
74              
75             my $obj = Data::Person->new(%params);
76             my $email = $obj->email;
77             my $external_ids_ar = $obj->external_ids;
78             my $id = $obj->id;
79             my $name = $obj->name;
80             my $sex = $obj->sex;
81              
82             =head1 DESCRIPTION
83              
84             The immutable data object for representation of person.
85              
86             =head1 METHODS
87              
88             =head2 C<new>
89              
90             my $obj = Data::Person->new(%params);
91              
92             Constructor.
93              
94             Returns instance of object.
95              
96             =over 8
97              
98             =item * C<email>
99              
100             Person's email for external identification.
101             It's optional.
102             Default value is undef.
103              
104             =item * C<external_ids>
105              
106             Person external ids.
107             It's optional.
108             Value must be a instance of L<Data::ExternalId> object.
109             Default value is [].
110              
111             =item * C<id>
112              
113             Id of person.
114             It's natural number.
115             It's optional.
116             Default value is undef.
117              
118             =item * C<name>
119              
120             Name of person.
121             Length of name is 255.
122             It's optional.
123              
124             =item * C<sex>
125              
126             Sex of person.
127             Possible values are: female, male and unknown.
128             It's optional.
129              
130             =back
131              
132             =head2 C<email>
133              
134             my $email = $obj->email;
135              
136             Get person email.
137              
138             Returns string.
139              
140             =head2 C<external_ids>
141              
142             my $external_ids_ar = $obj->external_ids;
143              
144             Get external ids.
145              
146             Returns reference to array with L<Data::ExternalId> instances.
147              
148             =head2 C<id>
149              
150             my $id = $obj->id;
151              
152             Get person id.
153              
154             Returns number.
155              
156             =head2 C<name>
157              
158             my $name = $obj->name;
159              
160             Get person name.
161              
162             Returns string.
163              
164             =head2 C<sex>
165              
166             my $sex = $obj->sex;
167              
168             Get person sex.
169              
170             Returns string.
171              
172             =head1 ERRORS
173              
174             new():
175             From Mo::utils::Array::check_array_object():
176             Parameter 'external_ids' must be a array.
177             Value: %s
178             Reference: %s
179             Parameter 'external_ids' with array must contain 'Data::ExternalId' objects.
180             Value: %s
181             Reference: %s
182              
183             From Mo::utils::Number::check_positive_natural():
184             Parameter 'id' must a positive natural number.
185             Value: %s
186              
187             Parameter 'name' has length greater than '255'.
188             Value: %s
189             Parameter 'sex' must be one of defined strings.
190             String: %s
191             Possible strings: %s
192              
193             =head1 EXAMPLE
194              
195             =for comment filename=create_and_print_person.pl
196              
197             use strict;
198             use warnings;
199              
200             use Data::ExternalId;
201             use Data::Person;
202             use DateTime;
203             use Unicode::UTF8 qw(decode_utf8 encode_utf8);
204              
205             my $obj = Data::Person->new(
206             'email' => 'skim@cpan.org',
207             'external_ids' => [
208             Data::ExternalId->new(
209             'key' => 'Wikidata',
210             'value' => 'Q27954834',
211             ),
212             ],
213             'id' => 1,
214             'name' => decode_utf8('Michal Josef Špaček'),
215             'sex' => 'male',
216             );
217              
218             # Print out.
219             print 'Id: '.$obj->id."\n";
220             print 'Name: '.encode_utf8($obj->name)."\n";
221             print 'Email: '.$obj->email."\n";
222             print 'Sex: '.$obj->sex."\n";
223             foreach my $external_id (@{$obj->external_ids}) {
224             print 'External id - '.$external_id->key.': '.$external_id->value."\n";
225             }
226              
227             # Output:
228             # Id: 1
229             # Name: Michal Josef Špaček
230             # Email: skim@cpan.org
231             # Sex: male
232             # External id - Wikidata: Q27954834
233              
234             =head1 DEPENDENCIES
235              
236             L<Mo>,
237             L<Mo::utils>,
238             L<Mo::utils::Array>,
239             L<Mo::utils::Email>,
240             L<Mo::utils::Number>,
241             L<Readonly>.
242              
243             =head1 REPOSITORY
244              
245             L<https://github.com/michal-josef-spacek/Data-Person>
246              
247             =head1 AUTHOR
248              
249             Michal Josef Špaček L<mailto:skim@cpan.org>
250              
251             L<http://skim.cz>
252              
253             =head1 LICENSE AND COPYRIGHT
254              
255             © 2021-2025 Michal Josef Špaček
256              
257             BSD 2-Clause License
258              
259             =head1 VERSION
260              
261             0.06
262              
263             =cut