File Coverage

blib/lib/Data/Random/HashType.pm
Criterion Covered Total %
statement 48 48 100.0
branch 12 12 100.0
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 70 70 100.0


line stmt bran cond sub pod time code
1             package Data::Random::HashType;
2              
3 4     4   83760 use strict;
  4         25  
  4         114  
4 4     4   25 use warnings;
  4         11  
  4         122  
5              
6 4     4   2462 use Class::Utils qw(set_params);
  4         55570  
  4         79  
7 4     4   2252 use Data::HashType;
  4         27903  
  4         126  
8 4     4   27 use Error::Pure qw(err);
  4         8  
  4         153  
9 4     4   37 use Readonly;
  4         6  
  4         1916  
10              
11             Readonly::Array our @OBSOLETE_HASH_TYPES => qw(MD4 MD5 SHA1);
12             Readonly::Array our @DEFAULT_HASH_TYPES => qw(SHA-256 SHA-384 SHA-512);
13             Readonly::Array our @ALL_HASH_TYPES => (@OBSOLETE_HASH_TYPES, @DEFAULT_HASH_TYPES);
14              
15             our $VERSION = 0.01;
16              
17             sub new {
18 9     9 1 10174 my ($class, @params) = @_;
19              
20             # Create object.
21 9         25 my $self = bless {}, $class;
22              
23             # Add id or not.
24 9         27 $self->{'mode_id'} = 0;
25              
26             # Number of hash types.
27 9         18 $self->{'num_generated'} = 1;
28              
29             # Hash types.
30 9         20 $self->{'possible_hash_types'} = \@DEFAULT_HASH_TYPES;
31              
32             # Process parameters.
33 9         32 set_params($self, @params);
34              
35             # TODO Check mode_id.
36             # TODO Check num_generated.
37 9 100       154 if (! defined $self->{'num_generated'}) {
38 1         4 err "Parameter 'num_generated' is required.";
39             }
40 8 100       29 if (ref $self->{'possible_hash_types'} ne 'ARRAY') {
41 1         4 err "Parameter 'possible_hash_types' must be a reference to array.";
42             }
43 7 100       11 if (! @{$self->{'possible_hash_types'}}) {
  7         23  
44 1         16 err "Parameter 'possible_hash_types' must contain at least one hash type name.";
45             }
46              
47 6         31 return $self;
48             }
49              
50             sub random {
51 5     5 1 27 my $self = shift;
52              
53 5         9 my @ret;
54 5 100       8 if ($self->{'num_generated'} < @{$self->{'possible_hash_types'}}) {
  5         27  
55              
56 2         6 my @list = @{$self->{'possible_hash_types'}};
  2         5  
57 2         9 foreach my $id (1 .. $self->{'num_generated'}) {
58 2         40 my $rand = int(rand(scalar @list - 1));
59 2         9 my $hash_type = splice @list, $rand, 1;
60             push @ret, Data::HashType->new(
61 2 100       12 $self->{'mode_id'} ? ('id' => $id) : (),
62             'active' => 1,
63             'name' => $hash_type,
64             );
65             }
66             } else {
67 3         4 my $i = 1;
68 3         5 foreach my $hash_type (@{$self->{'possible_hash_types'}}) {
  3         9  
69             push @ret, Data::HashType->new(
70 4 100       18 $self->{'mode_id'} ? ('id' => $i) : (),
71             'active' => 1,
72             'name' => $hash_type,
73             );
74 4         375 $i++;
75             }
76             }
77              
78 5         221 return @ret;
79             }
80              
81             1;
82              
83             __END__
84              
85             =pod
86              
87             =encoding utf8
88              
89             =head1 NAME
90              
91             Data::Random::HashType - Random hash type objects.
92              
93             =head1 SYNOPSIS
94              
95             use Data::Random::HashType;
96              
97             my $obj = Data::Random::HashType->new(%params);
98             my @hash_types = $obj->random;
99              
100             =head1 METHODS
101              
102             =head2 C<new>
103              
104             my $obj = Data::Random::HashType->new(%params);
105              
106             Constructor.
107              
108             =over 8
109              
110             =item * C<mode_id>
111              
112             Boolean value if we are generating id in hash type object.
113              
114             Default value is 0.
115              
116             =item * C<num_generated>
117              
118             Number of generated hash types.
119              
120             Default value is 1.
121              
122             =item * C<possible_hash_types>
123              
124             Possible hash type names for result.
125              
126             Default value is list (SHA-256 SHA-384 SHA-512).
127              
128             =back
129              
130             Returns instance of object.
131              
132             =head2 C<random>
133              
134             my @hash_types = $obj->random;
135              
136             Get random hash type object.
137              
138             Returns instance of L<Data::HashType>.
139              
140             =head1 ERRORS
141              
142             new():
143             Parameter 'num_generated' is required.
144             Parameter 'possible_hash_types' must be a reference to array.
145             Parameter 'possible_hash_types' must contain at least one hash type name.
146              
147             =head1 EXAMPLE
148              
149             =for comment filename=random_hash_type.pl
150              
151             use strict;
152             use warnings;
153              
154             use Data::Printer;
155             use Data::Random::HashType;
156              
157             my $obj = Data::Random::HashType->new(
158             'mode_id' => 1,
159             'num_generated' => 2,
160             );
161              
162             my @hash_types = $obj->random;
163              
164             # Dump hash types to out.
165             p @hash_types;
166              
167             # Output:
168             # [
169             # [0] Data::HashType {
170             # parents: Mo::Object
171             # public methods (5):
172             # BUILD
173             # Mo::utils:
174             # check_bool, check_length, check_number, check_required
175             # private methods (0)
176             # internals: {
177             # active 1,
178             # id 1,
179             # name "SHA-256"
180             # }
181             # },
182             # [1] Data::HashType {
183             # parents: Mo::Object
184             # public methods (5):
185             # BUILD
186             # Mo::utils:
187             # check_bool, check_length, check_number, check_required
188             # private methods (0)
189             # internals: {
190             # active 1,
191             # id 2,
192             # name "SHA-384"
193             # }
194             # }
195             # ]
196              
197             =head1 DEPENDENCIES
198              
199             L<Class::Utils>,
200             L<Data::HashType>,
201             L<Error::Pure>,
202             L<Readonly>.
203              
204             =head1 REPOSITORY
205              
206             L<https://github.com/michal-josef-spacek/Data-Random-HashType>
207              
208             =head1 AUTHOR
209              
210             Michal Josef Špaček L<mailto:skim@cpan.org>
211              
212             L<http://skim.cz>
213              
214             =head1 LICENSE AND COPYRIGHT
215              
216             © 2023 Michal Josef Špaček
217              
218             BSD 2-Clause License
219              
220             =head1 VERSION
221              
222             0.01
223              
224             =cut