MultiTenant/Config.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 40.0% 4 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 79.7% 55 / 69 CC 9 SCAR 18.6
CC
9
Cov
78%
new
12.3
config_for
2.0
BEGIN
1.0
CRAP 6.5
↑ Summary MultiTenant.pm »
1 package Dezi::MultiTenant::Config;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Dezi::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 configs
8 3 );
9 3 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.003';
12  
13 =head1 NAME
14  
15 Dezi::MultiTenant::Config - multi-tenant Dezi configuration
16  
17 =head1 SYNOPSIS
18  
19 use Dezi::MultiTenant::Config;
20  
21 my $dmc = Dezi::MultiTenant::Config->new(
22 '/foo' => {
23 engine_config => {
24 type => 'Lucy'
25 },
26 },
27 '/bar' => {
28 engine_config => {
29 type => 'Xapian',
30 },
31 },
32 });
33  
34 for my $path ($dmc->paths()) {
35 printf("%s mounted with config: %s\n",
36 $path, Data::Dump::dump( $dmc->config_for( $path )
37 );
38 }
39  
40 =head1 DESCRIPTION
41  
42 Dezi::MultiTenant::Config is a hash-based configuration
43 with keys representing Plack mount points and values of
44 Dezi::Config objects.
45  
46 =head1 METHODS
47  
48 =head2 new( I<config> )
49  
50 I<config> should be a hashref with keys representing
51 Plack mount points. Values are passed directly
52 to Dezi::Config->new().
53  
54 =cut
55  
56 1 ▶sub new {
Subroutine new: called CC=8 SCAR=25.1
Pod documented
57 1 my $class = shift;
58 1 ▶ my $config = shift or croak "config hashref required";
Branch: 1 branch
BranchTrueFalseTotal
my $config = shift() or croak("config hashref required") 1 0 1
59 1 ▶ if ( !ref $config or ref $config ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $config or ref $config ne "HASH") 0 1 1
Condition33%1/3
not ref $config or ref $config ne "HASH"
l!l&&r!l&&!r
001
MC/DC0%0/2
not ref $config or ref $config ne "HASH"
not ref $config ref $config ne "HASH"
Truth table33%1/3
not(A) or B
A= not ref $configB= ref $config ne "HASH"
ABresult
1X1
011
000
60 0 croak "config should be a hashref";
61 }
62  
63 #dump $config;
64  
65 # some "meta" keys apply to all servers so propogate them
66 # unless explicitly set already
67 my %meta_config
68 0 = map { $_ => $config->{$_} } grep { !m,^(/|http), } keys %$config;
69  
70 #dump \%meta_config;
71  
72 1 my %configs;
73 1 for my $key ( keys %$config ) {
74  
75 2 ▶ next if exists $meta_config{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta_config{$key} 0 2 2
76  
77 # merge in meta
78 2 my $single_config = $config->{$key};
79 2 for my $k ( keys %meta_config ) {
80 0 ▶ next if exists $single_config->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $single_config->{$k} 0 0 0
81 0 $single_config->{$k} = $meta_config{$k};
82 }
83  
84 #dump $single_config;
85  
86 2 $configs{$key} = Dezi::Config->new($single_config);
87 }
88  
89 #dump \%configs;
90  
91 1 return bless { configs => \%configs }, $class;
92 }
93  
94 =head2 paths
95  
96 Returns array of keys set in new().
97  
98 =cut
99  
100 1 ▶sub paths {
Subroutine paths: called CC=1 SCAR=0.0
Pod documented
101 1 my $self = shift;
102 1 return keys %{ $self->configs };
103 }
104  
105 =head2 config_for( I<path> )
106  
107 Returns Dezi::Config object for I<path>.
108  
109 =cut
110  
111 2 ▶sub config_for {
Subroutine config_for: called CC=2 SCAR=7.1
Pod documented
112 2 my $self = shift;
113 2 ▶ my $path = shift or croak "path required";
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or croak("path required") 2 0 2
114 2 return $self->{configs}->{$path};
115 }
116  
117 3 1;
118  
119 __END__
↑ Summary MultiTenant.pm »