File Coverage

blib/lib/Data/Login/Role.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::Login::Role;
2              
3 8     8   135500 use strict;
  8         20  
  8         316  
4 8     8   84 use warnings;
  8         15  
  8         424  
5              
6 8     8   10335 use DateTime;
  8         4425768  
  8         456  
7 8     8   4443 use Error::Pure qw(err);
  8         93207  
  8         243  
8 8     8   4202 use Mo qw(build default is);
  8         4360  
  8         48  
9 8     8   22083 use Mo::utils 0.28 qw(check_isa check_length check_required);
  8         20202  
  8         254  
10 8     8   4619 use Mo::utils::Number qw(check_positive_natural);
  8         16518  
  8         359  
11              
12             our $VERSION = 0.05;
13              
14             has id => (
15             is => 'ro',
16             );
17              
18             has role => (
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 14     14 0 1873829 my $self = shift;
32              
33             # Check id.
34 14         104 check_positive_natural($self, 'id');
35              
36             # Check role.
37 13         277 check_length($self, 'role', '100');
38 12         310 check_required($self, 'role');
39              
40             # Check valid_from.
41 11         96 check_isa($self, 'valid_from', 'DateTime');
42 11         276 check_required($self, 'valid_from');
43              
44             # Check valid_to.
45 10         90 check_isa($self, 'valid_to', 'DateTime');
46 10 100 100     158 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         114 'Valid from', $self->{'valid_from'},
52             ;
53             }
54              
55 9         96 return;
56             }
57              
58             1;
59              
60             __END__
61              
62             =pod
63              
64             =encoding utf8
65              
66             =head1 NAME
67              
68             Data::Login::Role - Data object for login role.
69              
70             =head1 SYNOPSIS
71              
72             use Data::Login::Role;
73              
74             my $obj = Data::Login::Role->new(%params);
75             my $id = $obj->id;
76             my $role = $obj->role;
77             my $valid_from = $obj->valid_from;
78             my $valid_to = $obj->valid_to;
79              
80             =head1 METHODS
81              
82             =head2 C<new>
83              
84             my $obj = Data::Login::Role->new(%params);
85              
86             Constructor.
87              
88             =over 8
89              
90             =item * C<id>
91              
92             Id of record.
93             Id could be number.
94             It's optional.
95             Default value is undef.
96              
97             =item * C<role>
98              
99             Role name.
100             Maximal length of value is 100 characters.
101             It's required.
102              
103             =item * C<valid_from>
104              
105             I<It will be required in near future. Optional is because backward
106             compatibility.>
107              
108             Date and time of start of use.
109             Must be a L<DateTime> object.
110             It's required.
111              
112             =item * C<valid_to>
113              
114             Date and time of end of use. An undefined value means it is in use.
115             Must be a L<DateTime> object.
116             It's optional.
117              
118             =back
119              
120             Returns instance of object.
121              
122             =head2 C<id>
123              
124             my $id = $obj->id;
125              
126             Get login role record id.
127              
128             Returns number.
129              
130             =head2 C<role>
131              
132             my $role = $obj->role;
133              
134             Get role name.
135              
136             Returns string.
137              
138             =head2 C<valid_from>
139              
140             my $valid_from = $obj->valid_from;
141              
142             Get date and time of start of use.
143              
144             Returns L<DateTime> object.
145              
146             =head2 C<valid_to>
147              
148             my $valid_to = $obj->valid_to;
149              
150             Get date and time of end of use.
151              
152             Returns L<DateTime> object or undef.
153              
154             =head1 ERRORS
155              
156             new():
157             From Mo::utils:
158             Parameter 'role' has length greater than '100'.
159             Value: %s
160             Parameter 'role' is required.
161             Parameter 'valid_from' is required.
162             Parameter 'valid_from' must be a 'DateTime' object.
163             Value: %s
164             Reference: %s
165             Parameter 'valid_to' must be a 'DateTime' object.
166             Value: %s
167             Reference: %s
168             Parameter 'valid_to' must be older than 'valid_from' parameter.
169             Value: %s
170             Valid from: %s
171              
172             From Mo::utils::Number::check_positive_natural():
173             Parameter 'id' must be a positive natural number.
174             Value: %s
175              
176             =head1 EXAMPLE
177              
178             =for comment filename=create_and_print_login_role.pl
179              
180             use strict;
181             use warnings;
182              
183             use Data::Login::Role;
184              
185             my $obj = Data::Login::Role->new(
186             'id' => 2,
187             'role' => 'admin',
188             'valid_from' => DateTime->new(
189             'day' => 1,
190             'month' => 1,
191             'year' => 2024,
192             ),
193             'valid_from' => DateTime->new(
194             'day' => 31,
195             'month' => 12,
196             'year' => 2024,
197             ),
198             );
199              
200             # Print out.
201             print 'Id: '.$obj->id."\n";
202             print 'Role: '.$obj->role."\n";
203             print 'Valid from: '.$obj->valid_from->ymd."\n";
204             print 'Valid to: '.$obj->valid_from->ymd."\n";
205              
206             # Output:
207             # Id: 2
208             # Role: admin
209             # Valid from: 2024-01-01
210             # Valid to: 2024-12-31
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-Login>
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.05
239              
240             =cut