File Coverage

blib/lib/MooX/ConfigFromFile/Role/HashMergeLoaded.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 6 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 39 39 100.0


line stmt bran cond sub pod time code
1             package MooX::ConfigFromFile::Role::HashMergeLoaded;
2              
3 2     2   2359 use strict;
  2         5  
  2         64  
4 2     2   28 use warnings;
  2         5  
  2         106  
5              
6             our $VERSION = '0.009';
7              
8 2     2   1125 use Hash::Merge;
  2         19818  
  2         113  
9              
10 2     2   20 use Moo::Role;
  2         6  
  2         20  
11              
12             requires "loaded_config";
13             requires "sorted_loaded_config";
14              
15             has "config_merge_behavior" => (is => "lazy");
16              
17 8     8   21 sub _build_config_merge_behavior { 'LEFT_PRECEDENT' }
18              
19             has "config_merger" => (is => "lazy");
20              
21             sub _build_config_merger
22             {
23 9     9   27 my ($class, $params) = @_;
24 9 100       43 defined $params->{config_merge_behavior} or $params->{config_merge_behavior} = $class->_build_config_merge_behavior($params);
25 9         55 Hash::Merge->new($params->{config_merge_behavior});
26             }
27              
28             sub _build_merged_loaded_config
29             {
30 10     10   252 my ($next, $class, $params) = @_;
31              
32 10 100       185 defined $params->{sorted_loaded_config} or $params->{sorted_loaded_config} = $class->_build_sorted_loaded_config($params);
33 10 100       74 defined $params->{config_merger} or $params->{config_merger} = $class->_build_config_merger($params);
34              
35 10         815 my $config_merged = {};
36 10         25 for my $c (map { values %$_ } @{$params->{sorted_loaded_config}})
  23         61  
  10         31  
37             {
38 23         1570 %$config_merged = %{$params->{config_merger}->merge($config_merged, $c)};
  23         90  
39             }
40              
41 10         851 $config_merged;
42             }
43              
44             around _build_loaded_config => \&_build_merged_loaded_config;
45              
46             1;
47              
48             =head1 NAME
49              
50             MooX::ConfigFromFile::Role::HashMergeLoaded - allows better merge strategies for multiple config files
51              
52             =head1 SYNOPSIS
53              
54             package MyApp::Cmd::TPau;
55              
56             use DBI;
57             use Moo;
58             use MooX::Cmd with_configfromfile => 1;
59            
60             with "MooX::ConfigFromFile::Role::HashMergeLoaded";
61              
62             has csv => (is => "ro", required => 1);
63              
64             sub execute
65             {
66             my $self = shift;
67             DBI->connect("DBI::csv:", undef, undef, $self->csv);
68             }
69              
70             __END__
71             $ cat etc/myapp.json
72             {
73             "csv": {
74             "f_ext": ".csv/r",
75             "csv_sep_char": ";",
76             "csv_class": "Text::CSV_XS"
77             }
78             }
79             $cat etc/myapp-tpau.json
80             {
81             "csv": {
82             "f_dir": "data/tpau"
83             }
84             }
85              
86             =head1 DESCRIPTION
87              
88             This is an additional role for MooX::ConfigFromFile to allow better merging
89             of deep structures.
90              
91             =head1 ATTRIBUTES
92              
93             =head2 config_merge_behavior
94              
95             This attribute contains the behavior which will L
96             use to merge particular loaded configurations.
97              
98             =head2 config_merger
99              
100             This attribute contains the instance of L used to merge the
101             I into I.
102              
103             =head2 loaded_config
104              
105             This role modifies the builder for I by merging the items
106             from I in order of appearance. It is assumed that more
107             relevant config files are in front and are filled up with defaults in
108             following ones.
109              
110             =head1 AUTHOR
111              
112             Jens Rehsack, C<< >>
113              
114             =head1 ACKNOWLEDGEMENTS
115              
116             =head1 LICENSE AND COPYRIGHT
117              
118             Copyright 2015-2018 Jens Rehsack.
119              
120             This program is free software; you can redistribute it and/or modify it
121             under the terms of either: the GNU General Public License as published
122             by the Free Software Foundation; or the Artistic License.
123              
124             See L for more information.
125              
126             =cut