File Coverage

blib/lib/Dancer/SearchApp/Defaults.pm
Criterion Covered Total %
statement 30 35 85.7
branch 4 8 50.0
condition 5 19 26.3
subroutine 5 5 100.0
pod 1 1 100.0
total 45 68 66.1


line stmt bran cond sub pod time code
1             package Dancer::SearchApp::Defaults;
2 2     2   9 use strict;
  2         2  
  2         50  
3 2     2   6 use Exporter 'import';
  2         2  
  2         49  
4 2     2   791 use Data::Diver qw;
  2         1278  
  2         135  
5 2     2   10 use vars qw($VERSION @EXPORT_OK %defaults);
  2         2  
  2         650  
6             $VERSION = '0.05';
7              
8             # This should move to Config::Spec::FromPod
9             # and maybe even Config::Collect
10             # instead of hand rolling yet another cascade
11              
12             @EXPORT_OK = qw(
13             get_defaults
14             );
15              
16             %defaults = (
17             );
18              
19             =head1 FUNCTIONS
20              
21             =head2 C<< get_defaults >>
22              
23             my $config = get_defaults(
24             #defaults => \%Dancer::SearchApp::Defaults::defaults,
25             #env => \%ENV,
26             config => LoadFile( 'config.yml' ),
27            
28             names => [
29             # hash-name, config-name, env-name, (hard-default)
30             [ Server => 'server' => IMAP_SERVER => 'localhost' ],
31             [ Port => 'port' => IMAP_PORT => '993' ],
32             [ User => 'username' => IMAP_USER => '' ],
33             [ Password => 'password' => IMAP_PASSWORD => '' ],
34             [ Debug => 'debug' => IMAP_DEBUG => 0 ],
35             ],
36             );
37              
38             Lame-ass config cascade
39              
40             Read from %ENV, $config, hard defaults, with different names,
41             write to yet more different names
42             Should merge with other config cascade in Config::Collect
43              
44             =cut
45              
46             sub get_defaults {
47 2     2 1 28 my( %options ) = @_;
48            
49 2   50     16 my $result = $options{ result } || {};
50              
51 2   50     12 $options{ defaults } ||= \%defaults; # premade defaults
52            
53 2         3 my @names = @{ $options{ names } };
  2         6  
54 2 50       8 if( ! exists $options{ env }) {
55 0         0 $options{ env } = \%ENV;
56             };
57 2         4 my $env = $options{ env };
58 2         2 my $config = $options{ config };
59            
60 2         2 for my $entry (@{ $options{ names }}) {
  2         8  
61 4         7 my ($result_name, $config_name, $env_name, $hard_default) = @$entry;
62            
63 4 50 33     18 if( defined $env_name and exists $env->{ $env_name } ) {
64             #print "Using $env_name from environment\n";
65 0         0 my $result_loc = DiveRef($result, split m!/!, $result_name);
66 0   0     0 $$result_loc //= $env->{ $env_name };
67             };
68            
69 4         18 my $val = Dive( $config, split m!/!, $config_name );
70 4 50 33     112 if( defined $config_name and defined( $val )) {
71             #print "Using $config_name from config ('$val')\n";
72 0         0 my $result_loc = DiveRef($result, split m!/!, $result_name);
73 0   0     0 $$result_loc //= $val;
74             };
75            
76 4 50 33     11 if( ! defined Dive($result, split m!/!, $result_name) and defined $hard_default) {
77             #print "No $config_name from config, using hardcoded default\n";
78             #print "Using $result_name from hard defaults ($hard_default)\n";
79 4         107 my $result_loc = DiveRef($result, split m!/!, $result_name);
80 4         117 $$result_loc = $hard_default;
81             };
82             };
83 2         6 return $result;
84             };
85              
86              
87             1;