File Coverage

blib/lib/Pcore/Util/Config.pm
Criterion Covered Total %
statement 20 31 64.5
branch 3 8 37.5
condition 2 12 16.6
subroutine 4 5 80.0
pod 0 2 0.0
total 29 58 50.0


line stmt bran cond sub pod time code
1             package Pcore::Util::Config;
2              
3 5     5   33 use Pcore -const;
  5         10  
  5         30  
4 5     5   30 use Pcore::Util::Text qw[encode_utf8];
  5         12  
  5         30  
5 5     5   1889 use Pcore::Util::Data qw[:TYPE encode_data decode_data];
  5         19  
  5         44  
6              
7             const our $EXT_TYPE_MAP => {
8             perl => $DATA_TYPE_PERL,
9             json => $DATA_TYPE_JSON,
10             cbor => $DATA_TYPE_CBOR,
11             yaml => $DATA_TYPE_YAML,
12             yml => $DATA_TYPE_YAML,
13             xml => $DATA_TYPE_XML,
14             ini => $DATA_TYPE_INI,
15             };
16              
17 8     8 0 16 sub load ( $cfg, @ ) {
  8         20  
  8         14  
18 8         46 my %args = (
19             type => undef,
20             splice @_, 1,
21             );
22              
23 8         24 my $type = delete $args{type};
24              
25 8 50       29 if ( !ref $cfg ) {
26 8 50       189 die qq[Config file "$cfg" wasn't found.] if !-f $cfg;
27              
28 8 50 33     125 $type = $EXT_TYPE_MAP->{$1} if !$type && $cfg =~ /[.]([^.]+)\z/sm;
29              
30 8         269 $cfg = P->file->read_bin($cfg);
31             }
32             else {
33 0         0 encode_utf8 $cfg->$*;
34             }
35              
36 8   33     32 $type //= $DATA_TYPE_PERL;
37              
38 8         47 return decode_data( $type, $cfg, %args );
39             }
40              
41 0     0 0   sub store ( $path, $cfg, @ ) {
  0            
  0            
  0            
42 0           my %args = (
43             type => undef,
44             splice @_, 2,
45             );
46              
47 0           my $type = delete $args{type};
48              
49 0 0 0       $type = $EXT_TYPE_MAP->{$1} if !$type && $path =~ /[.]([^.]+)\z/sm;
50              
51 0   0       $type //= $DATA_TYPE_PERL;
52              
53 0           P->file->write_bin( $path, encode_data( $type, $cfg, %args ) );
54              
55 0           return;
56             }
57              
58             1;
59             ## -----SOURCE FILTER LOG BEGIN-----
60             ##
61             ## PerlCritic profile "pcore-script" policy violations:
62             ## +------+----------------------+----------------------------------------------------------------------------------------------------------------+
63             ## | Sev. | Lines | Policy |
64             ## |======+======================+================================================================================================================|
65             ## | 3 | 49 | RegularExpressions::ProhibitCaptureWithoutTest - Capture variable used outside conditional |
66             ## +------+----------------------+----------------------------------------------------------------------------------------------------------------+
67             ##
68             ## -----SOURCE FILTER LOG END-----
69             __END__
70             =pod
71              
72             =encoding utf8
73              
74             =head1 NAME
75              
76             Pcore::Util::Config
77              
78             =head1 SYNOPSIS
79              
80             =head1 DESCRIPTION
81              
82             =cut