File Coverage

blib/lib/Config/MVP/Reader.pm
Criterion Covered Total %
statement 24 26 92.3
branch 3 6 50.0
condition 4 5 80.0
subroutine 5 7 71.4
pod 3 3 100.0
total 39 47 82.9


line stmt bran cond sub pod time code
1             package Config::MVP::Reader 2.200013;
2             # ABSTRACT: object to read config from storage into an assembler
3              
4 2     2   1105 use Moose;
  2         4  
  2         11  
5              
6 2     2   10933 use Config::MVP::Assembler;
  2         5  
  2         85  
7 2     2   11 use Cwd ();
  2         4  
  2         447  
8              
9             #pod =head1 SYNOPSIS
10             #pod
11             #pod use Config::MVP::Reader::YAML; # this doesn't really exist
12             #pod
13             #pod my $reader = Config::MVP::Reader::YAML->new;
14             #pod
15             #pod my $sequence = $reader->read_config('/etc/foobar.yml');
16             #pod
17             #pod =head1 DESCRIPTION
18             #pod
19             #pod A Config::MVP::Reader exists to read configuration data from storage (like a
20             #pod file) and convert that data into instructions to a L<Config::MVP::Assembler>,
21             #pod which will in turn convert them into a L<Config::MVP::Sequence>, the final
22             #pod product.
23             #pod
24             #pod =attr add_cwd_to_lib
25             #pod
26             #pod If true (which it is by default) then the current working directly will be
27             #pod locally added to C<@INC> during config loading. This helps deal with changes
28             #pod made in Perl v5.26.1.
29             #pod
30             #pod =cut
31              
32             has add_cwd_to_lib => (
33             is => 'ro',
34             isa => 'Bool',
35             default => 1,
36             );
37              
38             #pod =method read_config
39             #pod
40             #pod my $sequence = $reader->read_config($location, \%arg);
41             #pod
42             #pod This method is passed a location, which has no set meaning, but should be the
43             #pod mechanism by which the Reader is told how to locate configuration. It might be
44             #pod a file name, a hashref of parameters, a DBH, or anything else, depending on the
45             #pod needs of the specific Reader subclass.
46             #pod
47             #pod It is also passed a hashref of arguments, of which there is only one valid
48             #pod argument:
49             #pod
50             #pod assembler - the Assembler object into which to read the config
51             #pod
52             #pod If no assembler argument is passed, one will be constructed by calling the
53             #pod Reader's C<build_assembler> method.
54             #pod
55             #pod Subclasses should generally not override C<read_config>, but should instead
56             #pod implement a C<read_into_assembler> method, described below. If a subclass
57             #pod I<does> override C<read_config> it should take care to respect the
58             #pod C<add_cwd_to_lib> attribute, above.
59             #pod
60             #pod =cut
61              
62             sub read_config {
63 4     4 1 1864 my ($self, $location, $arg) = @_;
64 4   100     21 $arg ||= {};
65              
66 4 50       16 $self = $self->new unless blessed $self;
67              
68 4   66     21 my $assembler = $arg->{assembler} || $self->build_assembler;
69              
70             {
71 4         4535 local @INC = @INC;
  4         26  
72 4 50       101 if ($self->add_cwd_to_lib) {
73 4         46 my $cwd = Cwd::getcwd();
74 4 50       10 push @INC, $cwd unless grep {; $_ eq $cwd } @INC;
  40         69  
75             }
76 4         22 $self->read_into_assembler($location, $assembler);
77             }
78              
79 4         91 return $assembler->sequence;
80             }
81              
82             #pod =method read_into_assembler
83             #pod
84             #pod This method should not be called directly. It is called by C<read_config> with
85             #pod the following parameters:
86             #pod
87             #pod my $sequence = $reader->read_into_assembler( $location, $assembler );
88             #pod
89             #pod The method should read the configuration found at C<$location> and use it to
90             #pod instruct the C<$assembler> (a L<Config::MVP::Assembler>) what configuration to
91             #pod perform.
92             #pod
93             #pod The default implementation of this method will throw an exception complaining
94             #pod that it should have been implemented by a subclass.
95             #pod
96             #pod =cut
97              
98             sub read_into_assembler {
99 0     0 1   confess 'required method read_into_assembler unimplemented'
100             }
101              
102             #pod =method build_assembler
103             #pod
104             #pod If no Assembler is provided to C<read_config>'s C<assembler> parameter, this
105             #pod method will be called on the Reader to construct one.
106             #pod
107             #pod It must return a Config::MVP::Assembler object, and by default will return an
108             #pod entirely generic one.
109             #pod
110             #pod =cut
111              
112 0     0 1   sub build_assembler { Config::MVP::Assembler->new; }
113              
114 2     2   13 no Moose;
  2         22  
  2         10  
115             1;
116              
117             __END__
118              
119             =pod
120              
121             =encoding UTF-8
122              
123             =head1 NAME
124              
125             Config::MVP::Reader - object to read config from storage into an assembler
126              
127             =head1 VERSION
128              
129             version 2.200013
130              
131             =head1 SYNOPSIS
132              
133             use Config::MVP::Reader::YAML; # this doesn't really exist
134              
135             my $reader = Config::MVP::Reader::YAML->new;
136              
137             my $sequence = $reader->read_config('/etc/foobar.yml');
138              
139             =head1 DESCRIPTION
140              
141             A Config::MVP::Reader exists to read configuration data from storage (like a
142             file) and convert that data into instructions to a L<Config::MVP::Assembler>,
143             which will in turn convert them into a L<Config::MVP::Sequence>, the final
144             product.
145              
146             =head1 PERL VERSION
147              
148             This module should work on any version of perl still receiving updates from
149             the Perl 5 Porters. This means it should work on any version of perl released
150             in the last two to three years. (That is, if the most recently released
151             version is v5.40, then this module should work on both v5.40 and v5.38.)
152              
153             Although it may work on older versions of perl, no guarantee is made that the
154             minimum required version will not be increased. The version may be increased
155             for any reason, and there is no promise that patches will be accepted to lower
156             the minimum required perl.
157              
158             =head1 ATTRIBUTES
159              
160             =head2 add_cwd_to_lib
161              
162             If true (which it is by default) then the current working directly will be
163             locally added to C<@INC> during config loading. This helps deal with changes
164             made in Perl v5.26.1.
165              
166             =head1 METHODS
167              
168             =head2 read_config
169              
170             my $sequence = $reader->read_config($location, \%arg);
171              
172             This method is passed a location, which has no set meaning, but should be the
173             mechanism by which the Reader is told how to locate configuration. It might be
174             a file name, a hashref of parameters, a DBH, or anything else, depending on the
175             needs of the specific Reader subclass.
176              
177             It is also passed a hashref of arguments, of which there is only one valid
178             argument:
179              
180             assembler - the Assembler object into which to read the config
181              
182             If no assembler argument is passed, one will be constructed by calling the
183             Reader's C<build_assembler> method.
184              
185             Subclasses should generally not override C<read_config>, but should instead
186             implement a C<read_into_assembler> method, described below. If a subclass
187             I<does> override C<read_config> it should take care to respect the
188             C<add_cwd_to_lib> attribute, above.
189              
190             =head2 read_into_assembler
191              
192             This method should not be called directly. It is called by C<read_config> with
193             the following parameters:
194              
195             my $sequence = $reader->read_into_assembler( $location, $assembler );
196              
197             The method should read the configuration found at C<$location> and use it to
198             instruct the C<$assembler> (a L<Config::MVP::Assembler>) what configuration to
199             perform.
200              
201             The default implementation of this method will throw an exception complaining
202             that it should have been implemented by a subclass.
203              
204             =head2 build_assembler
205              
206             If no Assembler is provided to C<read_config>'s C<assembler> parameter, this
207             method will be called on the Reader to construct one.
208              
209             It must return a Config::MVP::Assembler object, and by default will return an
210             entirely generic one.
211              
212             =head1 AUTHOR
213              
214             Ricardo Signes <cpan@semiotic.systems>
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2022 by Ricardo Signes.
219              
220             This is free software; you can redistribute it and/or modify it under
221             the same terms as the Perl 5 programming language system itself.
222              
223             =cut