File Coverage

blib/lib/Data/Random/Person.pm
Criterion Covered Total %
statement 59 63 93.6
branch 5 8 62.5
condition n/a
subroutine 13 15 86.6
pod 2 2 100.0
total 79 88 89.7


line stmt bran cond sub pod time code
1             package Data::Random::Person;
2              
3 4     4   136970 use strict;
  4         10  
  4         134  
4 4     4   20 use warnings;
  4         6  
  4         277  
5              
6 4     4   2088 use Class::Utils qw(set_params);
  4         57507  
  4         87  
7 4     4   2631 use Data::Person;
  4         637640  
  4         349  
8 4     4   37 use Error::Pure qw(err);
  4         7  
  4         298  
9 4     4   24 use List::Util 1.33 qw(none);
  4         127  
  4         343  
10 4     4   21 use Mo::utils 0.06 qw(check_bool);
  4         90  
  4         201  
11 4     4   2383 use Mock::Person::CZ qw(name);
  4         8946  
  4         115  
12 4     4   2776 use Text::Unidecode;
  4         6170  
  4         2697  
13              
14             our $VERSION = 0.02;
15              
16             sub new {
17 4     4 1 846688 my ($class, @params) = @_;
18              
19             # Create object.
20 4         13 my $self = bless {}, $class;
21              
22             # Domain.
23 4         21 $self->{'domain'} = 'example.com';
24              
25             # Id.
26 4         13 $self->{'id'} = 1;
27             $self->{'cb_id'} = sub {
28 0     0   0 return $self->{'id'}++;
29 4         42 };
30              
31             # Name callback.
32             $self->{'cb_name'} = sub {
33 1     1   7 return name();
34 4         31 };
35              
36             # Add id or not.
37 4         11 $self->{'mode_id'} = 0;
38              
39             # Number of users.
40 4         13 $self->{'num_people'} = 10;
41              
42             # Process parameters.
43 4         26 set_params($self, @params);
44              
45 4         70 check_bool($self, 'mode_id');
46              
47             # Check domain.
48 3 100       102 if ($self->{'domain'} !~ m/^[a-zA-Z0-9\-\.]+$/ms) {
49 1         8 err "Parameter 'domain' is not valid.";
50             }
51              
52 2         8 return $self;
53             }
54              
55             sub random {
56 1     1 1 7 my $self = shift;
57              
58 1         21 my @data;
59 1         6 foreach my $i (1 .. $self->{'num_people'}) {
60 1         2 my $ok = 1;
61 1         4 while ($ok) {
62 1         6 my $people = $self->{'cb_name'}->($self);
63 1         137 my $email = $self->_name_to_email($people);
64 1 50   0   14 if (none { $_->email eq $email } @data) {
  0         0  
65 1         2 my $id;
66 1 50       5 if ($self->{'mode_id'}) {
67 0         0 $id = $self->{'cb_id'}->($self);
68             }
69 1 50       15 push @data, Data::Person->new(
70             'email' => $email,
71             defined $id ? ('id' => $id) : (),
72             'name' => $people,
73             );
74 1         5358 $ok = 0;
75             } else {
76 0         0 print "Fail\n";
77             }
78             }
79             }
80              
81 1         6 return @data;
82             }
83              
84             sub _name_to_email {
85 1     1   3 my ($self, $name) = @_;
86              
87 1         26 my $email = unidecode(lc($name));
88 1         2213 $email =~ s/\s+/\./g;
89 1         6 $email .= '@'.$self->{'domain'};
90              
91 1         3 return $email;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding utf8
101              
102             =head1 NAME
103              
104             Data::Random::Person - Random person objects.
105              
106             =head1 SYNOPSIS
107              
108             use Data::Random::Person;
109              
110             my $obj = Data::Random::Person->new(%params);
111             my @people = $obj->random;
112              
113             =head1 METHODS
114              
115             =head2 C<new>
116              
117             my $obj = Data::Random::Person->new(%params);
118              
119             Constructor.
120              
121             =over 8
122              
123             =item * C<cb_id>
124              
125             Callback to adding of id.
126              
127             Default value is subroutine which returns C<$self->{'id'}++>.
128              
129             =item * C<cb_name>
130              
131             Callback to create person name.
132              
133             Default value is subroutine which returns C<Mock::Person::CZ::name()>.
134              
135             =item * C<domain>
136              
137             Domain for email.
138              
139             Default value is 'example.com'.
140              
141             =item * C<id>
142              
143             Minimal id for adding. Only if C<mode_id> is set to 1.
144              
145             Default value is 1.
146              
147             =item * C<mode_id>
148              
149             Boolean value if we are generating id in hash type object.
150              
151             Default value is 0.
152              
153             =item * C<num_people>
154              
155             Number of generated person records.
156              
157             Default value is 10.
158              
159             =back
160              
161             Returns instance of object.
162              
163             =head2 C<random>
164              
165             my @people = $obj->random;
166              
167             Get random person records.
168              
169             Returns instance of L<Data::Person>.
170              
171             =head1 ERRORS
172              
173             new():
174             From Mo::utils::check_bool():
175             Parameter 'mode_id' must be a bool (0/1).
176             Value: %s
177             Parameter 'domain' is not valid.
178              
179             =head1 EXAMPLE
180              
181             =for comment filename=random_person.pl
182              
183             use strict;
184             use warnings;
185              
186             use Data::Printer;
187             use Data::Random::Person;
188              
189             my $obj = Data::Random::Person->new(
190             'mode_id' => 1,
191             'num_people' => 2,
192             );
193              
194             my @people = $obj->random;
195              
196             # Dump person records to out.
197             p @people;
198              
199             # Output like:
200             # [
201             # [0] Data::Person {
202             # parents: Mo::Object
203             # public methods (6):
204             # BUILD
205             # Mo::utils:
206             # check_length, check_number_id, check_strings
207             # Mo::utils::Email:
208             # check_email
209             # Readonly:
210             # Readonly
211             # private methods (0)
212             # internals: {
213             # email "jiri.sykora@example.com",
214             # id 1,
215             # name "Jiří Sýkora"
216             # }
217             # },
218             # [1] Data::Person {
219             # parents: Mo::Object
220             # public methods (6):
221             # BUILD
222             # Mo::utils:
223             # check_length, check_number_id, check_strings
224             # Mo::utils::Email:
225             # check_email
226             # Readonly:
227             # Readonly
228             # private methods (0)
229             # internals: {
230             # email "bedrich.pavel.stepanek@example.com",
231             # id 2,
232             # name "Bedřich Pavel Štěpánek"
233             # }
234             # }
235             # ]
236              
237             =head1 DEPENDENCIES
238              
239             L<Class::Utils>,
240             L<Data::Person>,
241             L<Error::Pure>,
242             L<List::Util>,
243             L<Mo::utils>,
244             L<Mock::Person::CZ>,
245             L<Text::Unidecode>.
246              
247             =head1 REPOSITORY
248              
249             L<https://github.com/michal-josef-spacek/Data-Random-Person>
250              
251             =head1 AUTHOR
252              
253             Michal Josef Špaček L<mailto:skim@cpan.org>
254              
255             L<http://skim.cz>
256              
257             =head1 LICENSE AND COPYRIGHT
258              
259             © 2024 Michal Josef Špaček
260              
261             BSD 2-Clause License
262              
263             =head1 VERSION
264              
265             0.02
266              
267             =cut