Mojolicious/Plugin/NotYAMLConfig.pm untested

filter: Statementstmt 0.0% 0 / 23 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 41 CC 5 SCAR 21.6
CC
5
Cov
0%
parse
12.0
register
12.0
BEGIN
2.0
CRAP 8.7
1 package Mojolicious::Plugin::NotYAMLConfig;
2 use Mojo::Base 'Mojolicious::Plugin::JSONConfig';
3  
4 use CPAN::Meta::YAML;
5 use Mojo::Util qw(decode encode);
6  
7 sub parse {
8 my ($self, $content, $file, $conf, $app) = @_;
9 my $config = eval { $self->{yaml}->(encode('UTF-8', $self->render($content, $file, $conf, $app))) };
10 die qq{Can't load configuration from file "$file": $@} if $@;
11 die qq{Configuration file "$file" did not return a YAML mapping} unless ref $config eq 'HASH';
12 return $config;
13 }
14  
15 sub register {
16 my ($self, $app, $conf) = @_;
17  
18 $conf->{ext} //= 'yml';
19 $self->{yaml} = sub { CPAN::Meta::YAML::Load(decode 'UTF-8', shift) };
20 if (my $mod = $conf->{module}) {
21 die qq{YAML module $mod has no Load function} unless $self->{yaml} = $mod->can('Load');
22 }
23  
24 return $self->SUPER::register($app, $conf);
25 }
26  
27 1;
28  
29 =encoding utf8
30  
31 =head1 NAME
32  
33 Mojolicious::Plugin::NotYAMLConfig - Not quite YAML configuration plugin
34  
35 =head1 SYNOPSIS
36  
37 # myapp.yml (it's just YAML with embedded Perl)
38 ---
39 foo: bar
40 baz:
41 - ♥
42 music_dir: <%= app->home->child('music') %>
43  
44 # Mojolicious
45 my $config = $app->plugin('NotYAMLConfig');
46 say $config->{foo};
47  
48 # Mojolicious::Lite
49 my $config = plugin 'NotYAMLConfig';
50 say $config->{foo};
51  
52 # foo.html.ep
53 %= config->{foo}
54  
55 # The configuration is available application-wide
56 my $config = app->config;
57 say $config->{foo};
58  
59 # Everything can be customized with options
60 my $config = plugin NotYAMLConfig => {file => '/etc/myapp.conf'};
61  
62 =head1 DESCRIPTION
63  
64 L<Mojolicious::Plugin::NotYAMLConfig> is a YAML configuration plugin that preprocesses its input with L<Mojo::Template>.
65 By default it uses L<CPAN::Meta::YAML> for parsing, which is not the best YAML module available, but good enough for
66 most config files. If you need something more correct you can use a different module like L<YAML::XS> with the
67 L</"module"> option.
68  
69 The application object can be accessed via C<$app> or the C<app> function. A default configuration filename in the
70 application home directory will be generated from the value of L<Mojolicious/"moniker"> (C<$moniker.yml>). You can
71 extend the normal configuration file C<$moniker.yml> with C<mode> specific ones like C<$moniker.$mode.yml>, which will
72 be detected automatically.
73  
74 These configuration values are currently reserved:
75  
76 =over 2
77  
78 =item C<config_override>
79  
80 If this configuration value has been set in L<Mojolicious/"config"> when this plugin is loaded, it will not do anything
81 besides loading deployment specific plugins.
82  
83 =item C<plugins>
84  
85 plugins:
86 - SetUserGroup:
87 user: sri
88 group: staff
89  
90 One or more deployment specific plugins that should be loaded right after this plugin has been loaded.
91  
92 =back
93  
94 The code of this plugin is a good example for learning to build new plugins, you're welcome to fork it.
95  
96 See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available by default.
97  
98 =head1 OPTIONS
99  
100 L<Mojolicious::Plugin::NotYAMLConfig> inherits all options from L<Mojolicious::Plugin::JSONConfig> and supports the
101 following new ones.
102  
103 =head2 module
104  
105 # Mojolicious::Lite
106 plugin NotYAMLConfig => {module => 'YAML::PP'};
107  
108 Alternative YAML module to use for parsing.
109  
110 =head1 METHODS
111  
112 L<Mojolicious::Plugin::NotYAMLConfig> inherits all methods from L<Mojolicious::Plugin::JSONConfig> and implements the
113 following new ones.
114  
115 =head2 parse
116  
117 $plugin->parse($content, $file, $conf, $app);
118  
119 Process content with L<Mojolicious::Plugin::JSONConfig/"render"> and parse it with L<CPAN::Meta::YAML>.
120  
121 sub parse ($self, $content, $file, $conf, $app) {
122 ...
123 $content = $self->render($content, $file, $conf, $app);
124 ...
125 return $hash;
126 }
127  
128 =head2 register
129  
130 my $config = $plugin->register(Mojolicious->new);
131 my $config = $plugin->register(Mojolicious->new, {file => '/etc/foo.conf'});
132  
133 Register plugin in L<Mojolicious> application and merge configuration.
134  
135 =head1 SEE ALSO
136  
137 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
138  
139 =cut