File Coverage

blib/lib/Data/HashType.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 2 100.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 0 1 0.0
total 44 45 97.7


line stmt bran cond sub pod time code
1             package Data::HashType;
2              
3 7     7   181117 use strict;
  7         16  
  7         307  
4 7     7   92 use warnings;
  7         34  
  7         360  
5              
6 7     7   7329 use DateTime;
  7         4716824  
  7         330  
7 7     7   3966 use Error::Pure qw(err);
  7         82227  
  7         192  
8 7     7   4307 use Mo qw(build is);
  7         4396  
  7         43  
9 7     7   15396 use Mo::utils 0.28 qw(check_isa check_length check_required);
  7         19501  
  7         179  
10 7     7   4859 use Mo::utils::Number qw(check_positive_natural);
  7         17568  
  7         187  
11              
12             our $VERSION = 0.07;
13              
14             has id => (
15             is => 'ro',
16             );
17              
18             has name => (
19             is => 'ro',
20             );
21              
22             has valid_from => (
23             is => 'ro',
24             );
25              
26             has valid_to => (
27             is => 'ro',
28             );
29              
30             sub BUILD {
31 16     16 0 2016883 my $self = shift;
32              
33             # Check 'id'.
34 16         80 check_positive_natural($self, 'id');
35              
36             # Check 'name'.
37 15         289 check_required($self, 'name');
38 14         130 check_length($self, 'name', 50);
39              
40             # Check 'valid_from'.
41 13         209 check_required($self, 'valid_from');
42 12         94 check_isa($self, 'valid_from', 'DateTime');
43              
44             # Check 'valid_to'.
45 10         277 check_isa($self, 'valid_to', 'DateTime');
46 8 100 100     132 if (defined $self->{'valid_to'}
47             && DateTime->compare($self->{'valid_from'}, $self->{'valid_to'}) != -1) {
48              
49             err "Parameter 'valid_to' must be older than 'valid_from' parameter.",
50             'Value', $self->{'valid_to'},
51 1         103 'Valid from', $self->{'valid_from'},
52             ;
53             }
54              
55 7         124 return;
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding utf8
65              
66             =head1 NAME
67              
68             Data::HashType - Data object for hash type.
69              
70             =head1 SYNOPSIS
71              
72             use Data::HashType;
73              
74             my $obj = Data::HashType->new(%params);
75             my $id = $obj->id;
76             my $name = $obj->name;
77             my $valid_from = $obj->valid_from;
78             my $valid_to = $obj->valid_to;
79              
80             =head1 DESCRIPTION
81              
82             The intention of this module is to store information about the usage of digests.
83             Digests are active only within a certain time range, and we need a mechanism to
84             transition to others.
85              
86             A real-world example is a database table that follows the same format as this data object,
87             with multiple records being valid at different times, while other database tables
88             have relations to this table.
89              
90             =head1 METHODS
91              
92             =head2 C<new>
93              
94             my $obj = Data::HashType->new(%params);
95              
96             Constructor.
97              
98             =over 8
99              
100             =item * C<id>
101              
102             Id of record.
103             Id could be number.
104             It's optional.
105             Default value is undef.
106              
107             =item * C<name>
108              
109             Hash type name.
110             Maximal length of value is 50 characters.
111             It's required.
112              
113             =item * C<valid_from>
114              
115             Date and time of start of use.
116             Must be a L<DateTime> object.
117             It's required.
118              
119             =item * C<valid_to>
120              
121             Date and time of end of use. An undefined value means it is in use.
122             Must be a L<DateTime> object.
123             It's optional.
124              
125             =back
126              
127             Returns instance of object.
128              
129             =head2 C<id>
130              
131             my $id = $obj->id;
132              
133             Get hash type record id.
134              
135             Returns number.
136              
137             =head2 C<name>
138              
139             my $name = $obj->name;
140              
141             Get hash type name.
142              
143             Returns string.
144              
145             =head2 C<valid_from>
146              
147             my $valid_from = $obj->valid_from;
148              
149             Get date and time of start of use.
150              
151             Returns L<DateTime> object.
152              
153             =head2 C<valid_to>
154              
155             my $valid_to = $obj->valid_to;
156              
157             Get date and time of end of use.
158              
159             Returns L<DateTime> object or undef.
160              
161             =head1 ERRORS
162              
163             new():
164             From Mo::utils:
165             Parameter 'name' has length greater than '50'.
166             Value: %s
167             Parameter 'name' is required.
168             Parameter 'valid_from' is required.
169             Parameter 'valid_from' must be a 'DateTime' object.
170             Value: %s
171             Reference: %s
172             Parameter 'valid_to' must be a 'DateTime' object.
173             Value: %s
174             Reference: %s
175             Parameter 'valid_to' must be older than 'valid_from' parameter.
176             Value: %s
177             Valid from: %s
178              
179             From Mo::utils::Number::check_positive_natural():
180             Parameter 'id' must be a positive natural number.
181              
182             =head1 EXAMPLE
183              
184             =for comment filename=create_and_print_hash_type.pl
185              
186             use strict;
187             use warnings;
188              
189             use Data::HashType;
190             use DateTime;
191              
192             my $obj = Data::HashType->new(
193             'id' => 10,
194             'name' => 'SHA-256',
195             'valid_from' => DateTime->new(
196             'year' => 2024,
197             'month' => 1,
198             'day' => 1,
199             ),
200             );
201              
202             # Print out.
203             print 'Name: '.$obj->name."\n";
204             print 'Id: '.$obj->id."\n";
205             print 'Valid from: '.$obj->valid_from->ymd."\n";
206              
207             # Output:
208             # Name: SHA-256
209             # Id: 10
210             # Valid from: 2024-01-01
211              
212             =head1 DEPENDENCIES
213              
214             L<DateTime>,
215             L<Error::Pure>,
216             L<Mo>,
217             L<Mo::utils>,
218             L<Mo::utils::Number>.
219              
220             =head1 REPOSITORY
221              
222             L<https://github.com/michal-josef-spacek/Data-HashType>
223              
224             =head1 AUTHOR
225              
226             Michal Josef Špaček L<mailto:skim@cpan.org>
227              
228             L<http://skim.cz>
229              
230             =head1 LICENSE AND COPYRIGHT
231              
232             © 2023-2025 Michal Josef Špaček
233              
234             BSD 2-Clause License
235              
236             =head1 VERSION
237              
238             0.07
239              
240             =cut