File Coverage

blib/lib/Rapi/Blog/Util.pm
Criterion Covered Total %
statement 12 41 29.2
branch 0 24 0.0
condition 0 15 0.0
subroutine 4 13 30.7
pod 0 7 0.0
total 16 100 16.0


line stmt bran cond sub pod time code
1             package Rapi::Blog::Util;
2              
3 1     1   7 use strict;
  1         2  
  1         29  
4 1     1   5 use warnings;
  1         1  
  1         39  
5              
6 1     1   6 use RapidApp::Util ':all';
  1         3  
  1         431  
7              
8 1     1   2229 use DateTime;
  1         478014  
  1         576  
9              
10             sub _dt_base_opts {(
11 0     0     time_zone => 'local'
12             )}
13              
14 0     0 0   sub now_ts { &dt_to_ts( &now_dt ) }
15 0     0 0   sub now_dt { DateTime->now( &_dt_base_opts ) }
16              
17             sub dt_to_ts {
18 0 0 0 0 0   shift if ($_[0] && $_[0] eq __PACKAGE__);
19 0           my $dt = shift;
20 0           join(' ',$dt->ymd('-'),$dt->hms(':'));
21             }
22              
23             # This is overkill and probably silly; I wrote it to be able to rule out possible time-zone
24             # inflate/deflate conversion issues. As a sanity check, I can always compare apples to
25             # apples with the DateTime/db-date-string conversion funcs in this package
26             sub ts_to_dt {
27 0 0 0 0 0   shift if ($_[0] && $_[0] eq __PACKAGE__);
28 0           my $ts = shift;
29 0 0         length($ts) == 19 or die "Bad timestamp '$ts' - should be exactly 19 characters long (YYYY-MM-DD hh:mm:ss)";
30            
31 0           my ($date,$time) = split(/\s/,$ts,2);
32 0 0         length($date) == 10 or die "Bad date part '$date' - should be exactly 10 characters long (YYYY-MM-DD)";
33 0 0         length($time) == 8 or die "Bad time part '$time' - should be exactly 8 characters long (hh:mm:ss)";
34            
35 0           my @d = split(/\-/,$date);
36 0           my @t = split(/\:/,$time);
37 0 0         scalar(@d) == 3 or die "Bad date part '$date' - didn't split ('-') into exactly 3 items";
38 0 0         scalar(@t) == 3 or die "Bad time part '$time' - didn't split (':') into exactly 3 items";
39            
40 0           my %o = ( &_dt_base_opts );
41 0           ($o{year},$o{month},$o{day},$o{hour},$o{minute},$o{second}) = (@d,@t);
42            
43 0           DateTime->new(%o)
44             }
45              
46             sub get_uid {
47 0 0   0 0   if(my $c = RapidApp->active_request_context) {
48 0 0 0       return $c->user->linkedRow->id if ($c->can('user') && $c->user && $c->user->linkedRow);
      0        
49             }
50 0           return 0;
51             }
52              
53             sub get_User {
54 0 0   0 0   if(my $c = RapidApp->active_request_context) {
55 0 0 0       return $c->user->linkedRow if ($c->can('user') && $c->user);
56             }
57 0           return undef;
58             }
59              
60              
61             sub get_scaffold_cfg {
62 0 0   0 0   if(my $c = RapidApp->active_request_context) {
63 0     0     return try{$c->template_controller->Access->scaffold_cfg};
  0            
64             }
65 0           return undef;
66             }
67              
68             1;