File Coverage

blib/lib/Respite/Common.pm
Criterion Covered Total %
statement 20 25 80.0
branch 3 20 15.0
condition 7 35 20.0
subroutine 5 7 71.4
pod 0 4 0.0
total 35 91 38.4


line stmt bran cond sub pod time code
1             package Respite::Common;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Respite::Common - Common methods used internally
8              
9             =head1 SYNOPSIS
10              
11             package CustomModule;
12             use base 'Respite::Common';
13              
14             sub xkey1 { return shift->_configs->{xxx_key} || "unknown" }
15              
16             sub parse1 { return shift->json->decode(shift) }
17              
18             =cut
19              
20 5     5   38 use strict;
  5         7  
  5         164  
21 5     5   18 use warnings;
  5         6  
  5         3158  
22              
23             sub new {
24 21     21 0 205853 my ($class, $args) = @_;
25 21 100       27 return bless {%{$args || {}}}, $class;
  21         231  
26             }
27              
28             our $js;
29 15   50 15 0 410 sub json { $js ||= eval { require JSON; JSON->new->utf8->allow_unknown->allow_nonref->convert_blessed->canonical } || die "Could not load JSON: $@" }
      66        
30             our $jp;
31 0   0 0 0 0 sub jsop { $jp ||= eval { require JSON; JSON->new->utf8->allow_unknown->allow_nonref->convert_blessed->canonical->pretty } || die "Could not load JSON: $@" }
      0        
32              
33             our $config;
34             sub _configs {
35 143   66 143   404 return $config ||= do {
36 3         3 my $c = undef;
37 3         57 eval {
38 3         24 require config;
39 3         6 eval { $c = config->load };
  3         21  
40 3   0     18 $c ||= defined($config::config) && "HASH" eq ref $config::config && $config::config;
      33        
41 3 0 33     9 $c ||= %config::config ? \%config::config : undef;
42             };
43 3 50 0     12 "HASH" eq ref $c or $c = { failed_load => ($@ || "missing config::config hash") };
44 3         9 $c;
45             };
46             }
47              
48             sub config {
49 0     0 0   my ($self, $key, $def, $name) = @_;
50 0 0 0       $name ||= (my $n = $self->base_class || ref($self) || $self || '') =~ /(\w+)$/ ? lc $1 : '';
      0        
51 0           my $c = $self->_configs($name);
52             return exists($self->{$key}) ? $self->{$key}
53             : exists($c->{"${name}_service_${key}"}) ? $c->{"${name}_service_${key}"}
54             : (ref($c->{"${name}_service"}) && exists $c->{"${name}_service"}->{$key}) ? $c->{"${name}_service"}->{$key}
55             : exists($c->{"${name}_${key}"}) ? $c->{"${name}_${key}"}
56 0 0 0       : (ref($c->{$name}) && exists $c->{$name}->{$key}) ? $c->{$name}->{$key}
    0 0        
    0          
    0          
    0          
    0          
57             : ref($def) eq 'CODE' ? $def->($self) : $def;
58             }
59              
60             1;