| 1 | package Dancer::SearchApp::Defaults; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Data::Diver qw<DiveRef Dive >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use vars qw($VERSION @EXPORT_OK %defaults); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 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 | 3 | @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 13 | get_defaults | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | %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 | 3 | ▶ | sub get_defaults { | ||||||||||||||||||||||||||
|
Subroutine
get_defaults: called CC=13 SCAR=33.8
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 3 | my( %options ) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | my $result = $options{ result } || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$options{'result'} || {}
MC/DC0%0/1
$options{'result'} || {}
$options{'result'}
Truth table50%1/2
A
A= $options{'result'}
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 3 | ▶ | $options{ defaults } ||= \%defaults; # premade defaults | ||||||||||||||||||||||||||
|
Condition50%1/2
$options{"defaults"} ||= \%Dancer::SearchApp::Defaults::defaults
MC/DC0%0/1
$options{"defaults"} ||= \%Dancer::SearchApp::Defaults::defaults
$options{"defaults"}
Truth table50%1/2
A
A= $options{"defaults"}
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | my @names = @{ $options{ names } }; | |||||||||||||||||||||||||||
| 54 | 3 | ▶ | if( ! exists $options{ env }) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $options{ env } = \%ENV; | |||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||
| 57 | 3 | my $env = $options{ env }; | |||||||||||||||||||||||||||
| 58 | 3 | my $config = $options{ config }; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | for my $entry (@{ $options{ names }}) { | |||||||||||||||||||||||||||
| 61 | 6 | my ($result_name, $config_name, $env_name, $hard_default) = @$entry; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 6 | ▶ | if( defined $env_name and exists $env->{ $env_name } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $env_name and exists $env->{$env_name}
MC/DC0%0/2
defined $env_name and exists $env->{$env_name}
defined $env_name
exists $env->{$env_name}
Truth table33%1/3
A and B
A= defined $env_nameB= exists $env->{$env_name}
| |||||||||||||||||||||||||||||
| 64 | #warn "Using $env_name from environment\n"; | ||||||||||||||||||||||||||||
| 65 | 0 | my $result_loc = DiveRef($result, split m!/!, $result_name); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | $$result_loc //= $env->{ $env_name }; | ||||||||||||||||||||||||||
|
Condition0%0/3
$$result_loc //= $env->{$env_name}
MC/DC0%0/2
$$result_loc //= $env->{$env_name}
$$result_loc
$env->{$env_name}
Truth table0%0/3
A //= B
A= $$result_locB= $env->{$env_name}
| |||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 6 | my $val = Dive( $config, split m!/!, $config_name ); | |||||||||||||||||||||||||||
| 70 | 6 | ▶ | if( defined $config_name and defined( $val )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $config_name and defined $val
MC/DC0%0/2
defined $config_name and defined $val
defined $config_name
defined $val
Truth table33%1/3
A and B
A= defined $config_nameB= defined $val
| |||||||||||||||||||||||||||||
| 71 | #warn "Using $config_name from config ('$val')\n"; | ||||||||||||||||||||||||||||
| 72 | 0 | my $result_loc = DiveRef($result, split m!/!, $result_name); | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | $$result_loc //= $val; | ||||||||||||||||||||||||||
|
Condition0%0/3
$$result_loc //= $val
MC/DC0%0/2
$$result_loc //= $val
$$result_loc
$val
Truth table0%0/3
A //= B
A= $$result_locB= $val
| |||||||||||||||||||||||||||||
| 74 | }; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 6 | ▶ | if( ! defined Dive($result, split m!/!, $result_name) and defined $hard_default) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined Dive($result, split(m[/], $result_name, 0)) and defined $hard_default
MC/DC0%0/2
not defined Dive($result, split(m[/], $result_name, 0)) and defined $hard_default
not defined Dive($result, split(m[/], $result_name, 0))
defined $hard_default
Truth table33%1/3
not(A) and B
A= not defined Dive($result, split(m[/], $result_name, 0))B= defined $hard_default
| |||||||||||||||||||||||||||||
| 77 | #warn "No $config_name from config, using hardcoded default\n"; | ||||||||||||||||||||||||||||
| 78 | #print "Using $result_name from hard defaults ($hard_default)\n"; | ||||||||||||||||||||||||||||
| 79 | 6 | my $result_loc = DiveRef($result, split m!/!, $result_name); | |||||||||||||||||||||||||||
| 80 | 6 | $$result_loc = $hard_default; | |||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||
| 82 | }; | ||||||||||||||||||||||||||||
| 83 | 3 | return $result; | |||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | 1; | |||||||||||||||||||||||||||