File Coverage

blib/lib/Data/Random/HashType.pm
Criterion Covered Total %
statement 65 65 100.0
branch 10 10 100.0
condition n/a
subroutine 12 12 100.0
pod 2 2 100.0
total 89 89 100.0


line stmt bran cond sub pod time code
1             package Data::Random::HashType;
2              
3 4     4   177968 use strict;
  4         8  
  4         171  
4 4     4   22 use warnings;
  4         6  
  4         273  
5              
6 4     4   2424 use Class::Utils qw(set_params);
  4         62768  
  4         152  
7 4     4   2824 use Data::HashType 0.05;
  4         2912746  
  4         183  
8 4     4   50 use DateTime;
  4         10  
  4         130  
9 4     4   20 use Error::Pure qw(err);
  4         20  
  4         309  
10 4     4   34 use Mo::utils 0.25 qw(check_bool check_isa check_number_min check_required);
  4         77  
  4         259  
11 4     4   2516 use Random::Day::InThePast;
  4         355109  
  4         180  
12 4     4   46 use Readonly;
  4         13  
  4         3408  
13              
14             Readonly::Array our @OBSOLETE_HASH_TYPES => qw(MD4 MD5 SHA1);
15             Readonly::Array our @DEFAULT_HASH_TYPES => qw(SHA-256 SHA-384 SHA-512);
16             Readonly::Array our @ALL_HASH_TYPES => (@OBSOLETE_HASH_TYPES, @DEFAULT_HASH_TYPES);
17              
18             our $VERSION = 0.06;
19              
20             sub new {
21 15     15 1 928149 my ($class, @params) = @_;
22              
23             # Create object.
24 15         51 my $self = bless {}, $class;
25              
26             # Start date time.
27 15         579 $self->{'dt_start'} = DateTime->new(
28             'day' => 1,
29             'month' => 1,
30             'year' => ((localtime)[5] + 1900 - 1),
31             );
32              
33             # Id.
34 15         6843 $self->{'id'} = 1;
35             $self->{'cb_id'} = sub {
36 4     4   27 return $self->{'id'}++;
37 15         118 };
38              
39             # Add id or not.
40 15         38 $self->{'mode_id'} = 0;
41              
42             # Number of hash types.
43 15         38 $self->{'num_generated'} = 1;
44              
45             # Hash types.
46 15         50 $self->{'possible_hash_types'} = \@DEFAULT_HASH_TYPES;
47              
48             # Process parameters.
49 15         87 set_params($self, @params);
50              
51 15         388 check_required($self, 'dt_start');
52 14         172 check_isa($self, 'dt_start', 'DateTime');
53 13         378 check_bool($self, 'mode_id');
54 12         382 check_number_min($self, 'num_generated', 1);
55 11         448 check_required($self, 'num_generated');
56 10 100       98 if (ref $self->{'possible_hash_types'} ne 'ARRAY') {
57 1         9 err "Parameter 'possible_hash_types' must be a reference to array.";
58             }
59 9 100       20 if (! @{$self->{'possible_hash_types'}}) {
  9         82  
60 1         6 err "Parameter 'possible_hash_types' must contain at least one hash type name.";
61             }
62              
63             $self->{'_random_valid_from'} = Random::Day::InThePast->new(
64 8         79 'dt_from' => $self->{'dt_start'},
65             );
66              
67 8         12677 return $self;
68             }
69              
70             sub random {
71 7     7 1 42 my $self = shift;
72              
73 7         16 my @ret;
74 7 100       17 if ($self->{'num_generated'} < @{$self->{'possible_hash_types'}}) {
  7         25  
75              
76 2         3 my @list = @{$self->{'possible_hash_types'}};
  2         6  
77 2         5 foreach my $id (1 .. $self->{'num_generated'}) {
78 2         7 my $rand_index = int(rand(scalar @list));
79 2         5 my $hash_type = splice @list, $rand_index, 1;
80             push @ret, Data::HashType->new(
81             $self->{'mode_id'} ? (
82             'id' => $self->{'cb_id'}->($self),
83             ) : (),
84             'name' => $hash_type,
85 2 100       10 'valid_from' => $self->{'_random_valid_from'}->get->clone,
86             );
87             }
88             } else {
89 5         11 my $i = 1;
90 5         9 foreach my $hash_type (@{$self->{'possible_hash_types'}}) {
  5         18  
91             push @ret, Data::HashType->new(
92             $self->{'mode_id'} ? (
93             'id' => $self->{'cb_id'}->($self),
94             ) : (),
95             'name' => $hash_type,
96 8 100       53 'valid_from' => $self->{'_random_valid_from'}->get->clone,
97             );
98 8         90731 $i++;
99             }
100             }
101              
102 7         18773 return @ret;
103             }
104              
105             1;
106              
107             __END__
108              
109             =pod
110              
111             =encoding utf8
112              
113             =head1 NAME
114              
115             Data::Random::HashType - Random hash type objects.
116              
117             =head1 SYNOPSIS
118              
119             use Data::Random::HashType;
120              
121             my $obj = Data::Random::HashType->new(%params);
122             my @hash_types = $obj->random;
123              
124             =head1 METHODS
125              
126             =head2 C<new>
127              
128             my $obj = Data::Random::HashType->new(%params);
129              
130             Constructor.
131              
132             =over 8
133              
134             =item * C<cb_id>
135              
136             Callback to adding of id.
137              
138             Default value is subroutine which returns C<$self->{'id'}++>.
139              
140             =item * C<dt_start>
141              
142             L<DateTime> object with start date for random valid_from date. Range is dt_start
143             and actual date.
144              
145             Default value is January 1. year ago.
146              
147             =item * C<id>
148              
149             Minimal id for adding. Only if C<mode_id> is set to 1.
150              
151             Default value is 1.
152              
153             =item * C<mode_id>
154              
155             Boolean value if we are generating id in hash type object.
156              
157             Default value is 0.
158              
159             =item * C<num_generated>
160              
161             Number of generated hash types.
162              
163             Default value is 1.
164              
165             =item * C<possible_hash_types>
166              
167             Possible hash type names for result.
168              
169             Default value is list (SHA-256 SHA-384 SHA-512).
170              
171             =back
172              
173             Returns instance of object.
174              
175             =head2 C<random>
176              
177             my @hash_types = $obj->random;
178              
179             Get random hash type object.
180              
181             Returns instance of L<Data::HashType>.
182              
183             =head1 ERRORS
184              
185             new():
186             From Mo::utils:
187             Parameter 'dt_start' is required.
188             Parameter 'dt_start' must be a 'DateTime' object.
189             Value: %s
190             Reference: %s
191             Parameter 'mode_id' must be a bool (0/1).
192             Value: %s
193             Parameter 'num_generated' must be greater than %s.
194             Value: %s
195             Parameter 'num_generated' is required.
196             Parameter 'possible_hash_types' must be a reference to array.
197             Parameter 'possible_hash_types' must contain at least one hash type name.
198              
199             =head1 EXAMPLE
200              
201             =for comment filename=random_hash_type.pl
202              
203             use strict;
204             use warnings;
205              
206             use Data::Printer;
207             use Data::Random::HashType;
208              
209             my $obj = Data::Random::HashType->new(
210             'mode_id' => 1,
211             'num_generated' => 2,
212             );
213              
214             my @hash_types = $obj->random;
215              
216             # Dump hash types to out.
217             p @hash_types;
218              
219             # Output like:
220             # [
221             # [0] Data::HashType {
222             # parents: Mo::Object
223             # public methods (6):
224             # BUILD
225             # Error::Pure:
226             # err
227             # Mo::utils:
228             # check_isa, check_length, check_number, check_required
229             # private methods (0)
230             # internals: {
231             # id 1,
232             # name "SHA-384",
233             # valid_from 2023-03-17T00:00:00 (DateTime)
234             # }
235             # },
236             # [1] Data::HashType {
237             # parents: Mo::Object
238             # public methods (6):
239             # BUILD
240             # Error::Pure:
241             # err
242             # Mo::utils:
243             # check_isa, check_length, check_number, check_required
244             # private methods (0)
245             # internals: {
246             # id 2,
247             # name "SHA-256",
248             # valid_from 2023-01-27T00:00:00 (DateTime)
249             # }
250             # }
251             # ]
252              
253             =head1 DEPENDENCIES
254              
255             L<Class::Utils>,
256             L<Data::HashType>,
257             L<DateTime>,
258             L<Error::Pure>,
259             L<Mo::utils>,
260             L<Random::Day>,
261             L<Readonly>.
262              
263             =head1 REPOSITORY
264              
265             L<https://github.com/michal-josef-spacek/Data-Random-HashType>
266              
267             =head1 AUTHOR
268              
269             Michal Josef Špaček L<mailto:skim@cpan.org>
270              
271             L<http://skim.cz>
272              
273             =head1 LICENSE AND COPYRIGHT
274              
275             © 2023-2025 Michal Josef Špaček
276              
277             BSD 2-Clause License
278              
279             =head1 VERSION
280              
281             0.06
282              
283             =cut