File: Reader.pm

package info (click to toggle)
libconfig-mvp-perl 2.200013-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 312 kB
  • sloc: perl: 829; makefile: 2
file content (223 lines) | stat: -rw-r--r-- 6,789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package Config::MVP::Reader 2.200013;
# ABSTRACT: object to read config from storage into an assembler

use Moose;

use Config::MVP::Assembler;
use Cwd ();

#pod =head1 SYNOPSIS
#pod
#pod   use Config::MVP::Reader::YAML; # this doesn't really exist
#pod
#pod   my $reader   = Config::MVP::Reader::YAML->new;
#pod
#pod   my $sequence = $reader->read_config('/etc/foobar.yml');
#pod
#pod =head1 DESCRIPTION
#pod
#pod A Config::MVP::Reader exists to read configuration data from storage (like a
#pod file) and convert that data into instructions to a L<Config::MVP::Assembler>,
#pod which will in turn convert them into a L<Config::MVP::Sequence>, the final
#pod product.
#pod
#pod =attr add_cwd_to_lib
#pod
#pod If true (which it is by default) then the current working directly will be
#pod locally added to C<@INC> during config loading.  This helps deal with changes
#pod made in Perl v5.26.1.
#pod
#pod =cut

has add_cwd_to_lib => (
  is  => 'ro',
  isa => 'Bool',
  default => 1,
);

#pod =method read_config
#pod
#pod   my $sequence = $reader->read_config($location, \%arg);
#pod
#pod This method is passed a location, which has no set meaning, but should be the
#pod mechanism by which the Reader is told how to locate configuration.  It might be
#pod a file name, a hashref of parameters, a DBH, or anything else, depending on the
#pod needs of the specific Reader subclass.
#pod
#pod It is also passed a hashref of arguments, of which there is only one valid
#pod argument:
#pod
#pod  assembler - the Assembler object into which to read the config
#pod
#pod If no assembler argument is passed, one will be constructed by calling the
#pod Reader's C<build_assembler> method.
#pod
#pod Subclasses should generally not override C<read_config>, but should instead
#pod implement a C<read_into_assembler> method, described below.  If a subclass
#pod I<does> override C<read_config> it should take care to respect the
#pod C<add_cwd_to_lib> attribute, above.
#pod
#pod =cut

sub read_config {
  my ($self, $location, $arg) = @_;
  $arg ||= {};

  $self = $self->new unless blessed $self;

  my $assembler = $arg->{assembler} || $self->build_assembler;

  {
    local @INC = @INC;
    if ($self->add_cwd_to_lib) {
      my $cwd = Cwd::getcwd();
      push @INC, $cwd unless grep {; $_ eq $cwd } @INC;
    }
    $self->read_into_assembler($location, $assembler);
  }

  return $assembler->sequence;
}

#pod =method read_into_assembler
#pod
#pod This method should not be called directly.  It is called by C<read_config> with
#pod the following parameters:
#pod
#pod   my $sequence = $reader->read_into_assembler( $location, $assembler );
#pod
#pod The method should read the configuration found at C<$location> and use it to
#pod instruct the C<$assembler> (a L<Config::MVP::Assembler>) what configuration to
#pod perform.
#pod
#pod The default implementation of this method will throw an exception complaining
#pod that it should have been implemented by a subclass.
#pod
#pod =cut

sub read_into_assembler {
  confess 'required method read_into_assembler unimplemented'
}

#pod =method build_assembler
#pod
#pod If no Assembler is provided to C<read_config>'s C<assembler> parameter, this
#pod method will be called on the Reader to construct one.
#pod
#pod It must return a Config::MVP::Assembler object, and by default will return an
#pod entirely generic one.
#pod
#pod =cut

sub build_assembler { Config::MVP::Assembler->new; }

no Moose;
1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Config::MVP::Reader - object to read config from storage into an assembler

=head1 VERSION

version 2.200013

=head1 SYNOPSIS

  use Config::MVP::Reader::YAML; # this doesn't really exist

  my $reader   = Config::MVP::Reader::YAML->new;

  my $sequence = $reader->read_config('/etc/foobar.yml');

=head1 DESCRIPTION

A Config::MVP::Reader exists to read configuration data from storage (like a
file) and convert that data into instructions to a L<Config::MVP::Assembler>,
which will in turn convert them into a L<Config::MVP::Sequence>, the final
product.

=head1 PERL VERSION

This module should work on any version of perl still receiving updates from
the Perl 5 Porters.  This means it should work on any version of perl released
in the last two to three years.  (That is, if the most recently released
version is v5.40, then this module should work on both v5.40 and v5.38.)

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to lower
the minimum required perl.

=head1 ATTRIBUTES

=head2 add_cwd_to_lib

If true (which it is by default) then the current working directly will be
locally added to C<@INC> during config loading.  This helps deal with changes
made in Perl v5.26.1.

=head1 METHODS

=head2 read_config

  my $sequence = $reader->read_config($location, \%arg);

This method is passed a location, which has no set meaning, but should be the
mechanism by which the Reader is told how to locate configuration.  It might be
a file name, a hashref of parameters, a DBH, or anything else, depending on the
needs of the specific Reader subclass.

It is also passed a hashref of arguments, of which there is only one valid
argument:

 assembler - the Assembler object into which to read the config

If no assembler argument is passed, one will be constructed by calling the
Reader's C<build_assembler> method.

Subclasses should generally not override C<read_config>, but should instead
implement a C<read_into_assembler> method, described below.  If a subclass
I<does> override C<read_config> it should take care to respect the
C<add_cwd_to_lib> attribute, above.

=head2 read_into_assembler

This method should not be called directly.  It is called by C<read_config> with
the following parameters:

  my $sequence = $reader->read_into_assembler( $location, $assembler );

The method should read the configuration found at C<$location> and use it to
instruct the C<$assembler> (a L<Config::MVP::Assembler>) what configuration to
perform.

The default implementation of this method will throw an exception complaining
that it should have been implemented by a subclass.

=head2 build_assembler

If no Assembler is provided to C<read_config>'s C<assembler> parameter, this
method will be called on the Reader to construct one.

It must return a Config::MVP::Assembler object, and by default will return an
entirely generic one.

=head1 AUTHOR

Ricardo Signes <cpan@semiotic.systems>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut