Mojo/Hakkefuin/Utils.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 69 CC 8 SCAR 38.1
CC
8
Cov
0%
time_convert
72.0
gen_cookie
2.0
sql_datetime
2.0
CRAP 45.1
1 package Mojo::Hakkefuin::Utils;
2 use Mojo::Base -base;
3  
4 use Mojo::Date;
5 use String::Random;
6  
7 sub gen_cookie {
8 my ($self, $num) = @_;
9 $num //= 3;
10 state $random = String::Random->new;
11 $random->randpattern('CnCCcCCnCn' x $num);
12 }
13  
14 sub sql_datetime {
15 my ($self, $time_plus) = @_;
16 $time_plus //= 0;
17 my $epoch = time + $time_plus;
18 my $to_get_dt = Mojo::Date->new($epoch)->to_datetime;
19 $to_get_dt =~ qr/^([0-9\-]+)\w([0-9\:]+)(.*)/;
20 return $1 . ' ' . $2;
21 }
22  
23 sub time_convert {
24 my ($self, $abbr) = @_;
25  
26 # Reset shortening time
27 $abbr //= '1h';
28 $abbr =~ qr/^([\d.]+)(\w)/;
29  
30 # Set standard of time units
31 my $minute = 60;
32 my $hour = 60 * 60;
33 my $day = 24 * $hour;
34 my $week = 7 * $day;
35 my $month = 30 * $day;
36 my $year = 12 * $month;
37  
38 # Calculate by time units.
39 my $identifier;
40 $identifier = int $1 * 1 if $2 eq 's';
41 $identifier = $1 * $minute if $2 eq 'm';
42 $identifier = $1 * $hour if $2 eq 'h';
43 $identifier = $1 * $day if $2 eq 'd';
44 $identifier = $1 * $week if $2 eq 'w';
45 $identifier = $1 * $month if $2 eq 'M';
46 $identifier = $1 * $year if $2 eq 'y';
47 return $identifier;
48 }
49  
50 1;
51  
52 =encoding utf8
53  
54 =head1 NAME
55  
56 Mojo::Hakkefuin::Utils - Utilities
57  
58 =head1 SYNOPSIS
59  
60 use Mojo::Hakkefuin::Utils;
61
62 my $utils = Mojo::Hakkefuin::Utils->new;
63
64 # to generate cookie
65 my $cookie = $utils->gen_cookie;
66
67 # to generate sql time
68 my $sql_time = $utils->sql_datetime;
69
70 # to generate time by the abbreviation
71 my $time = $utils->time_convert('1d');
72  
73 =head1 DESCRIPTION
74  
75 General utilities which used on Backend and plugin class.
76  
77 =head1 METHODS
78  
79 L<Mojo::Hakkefuin::Utils> inherits all methods from L<Mojo::Base> and implements the
80 following new ones.
81  
82 =head2 gen_cookie
83  
84 my $cookie = $utils->gen_cookie;
85 my $cookie = $utils->gen_cookie(3);
86
87 This method only generate cookie login.
88  
89 =head2 sql_datetime
90  
91 my $sql_time = $utils->sql_datetime;
92 my $sql_time = $utils->sql_datetime(60 * 60);
93
94 This method only generate datetime.
95  
96 =head2 time_convert
97  
98 # To get 1 hour in units of seconds.
99 my $time = $utils->time_convert;
100
101 # time specified by the abbreviation
102 my $time = $utils->time_convert('1d');
103
104 Abbreviation of time :
105  
106 s = seconds.
107 m = minutes
108 h = hours
109 d = days
110 w = weeks
111 M = months
112 y = years
113
114 =head1 SEE ALSO
115  
116 =over 1
117  
118 =item * L<Mojolicious::Plugin::Hakkefuin>
119  
120 =item * L<Mojo::Hakkefuin>
121  
122 =item * L<Mojo::mysql>
123  
124 =item * L<Mojo::Pg>
125  
126 =item * L<Mojo::SQLite>
127  
128 =item * L<Mojolicious::Guides>
129  
130 =item * L<https://mojolicious.org>
131  
132 =back
133  
134 =cut