aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2019-08-19 17:51:19 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2019-08-19 18:19:33 +0300
commit11e6beb1f44fb74dc11e79c3bda25aebfd80ae12 (patch)
treebef1bfcecd9ad587c582a79a8dac78ca5f7569b7
parentd417ed4a1875321b28c47ea5f6324479d1592309 (diff)
downloadconfig-parser-11e6beb1f44fb74dc11e79c3bda25aebfd80ae12.tar.gz
config-parser-11e6beb1f44fb74dc11e79c3bda25aebfd80ae12.tar.bz2
Improve constructor
This commit introduces the "mangle" method, which can be implemented by the caller in order to run any additional tree modifications after a successful parse and commit (see the new testcase for an example). * lib/Config/Parser.pm (new): Call the init method if lexicon hash has been loaded. (init,inited,commit): New methods. * t/ConfigSpec3.pm: New file. * t/TestConfig.pm: Don't attempt to load empty files. * t/conf08.t: New file.
-rw-r--r--lib/Config/Parser.pm23
-rw-r--r--t/ConfigSpec3.pm21
-rw-r--r--t/TestConfig.pm5
-rw-r--r--t/conf08.t18
4 files changed, 64 insertions, 3 deletions
diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm
index 843b70c..fed0100 100644
--- a/lib/Config/Parser.pm
+++ b/lib/Config/Parser.pm
@@ -12,6 +12,7 @@ our $VERSION = "1.00";
sub new {
my $class = shift;
local %_ = @_;
+ my $loaded = 0;
my @parseargs;
if (my $filename = delete $_{filename}) {
@@ -31,6 +32,7 @@ sub new {
# print "LOADING FROM $c\n";
if (my $s = loadsynt($c)) {
$_{lexicon} = { %{$_{lexicon}}, %$s };
+ $loaded++;
}
last if $c eq $class;
}
@@ -39,7 +41,7 @@ sub new {
}
my $self = $class->SUPER::new(%_);
-
+ $self->init if $loaded;
if (@parseargs) {
$self->parse(@parseargs);
$self->commit or croak "configuration failed";
@@ -48,6 +50,22 @@ sub new {
return $self;
}
+sub init {
+ my $self = shift;
+ $self->{_inited} = 1;
+}
+
+sub inited { shift->{_inited} };
+
+sub commit {
+ my $self = shift;
+ my $res = $self->SUPER::commit;
+ if ($res && $self->inited && $self->can('mangle')) {
+ $self->mangle;
+ }
+ return $res;
+}
+
sub findsynt {
my $class = shift;
my $file = $class;
@@ -67,7 +85,8 @@ sub findsynt {
sub loadsynt {
my ($class) = @_;
if (my ($file, $line, $data) = findsynt($class)) {
- open(my $fh, '<', \$data);
+ open(my $fh, '<', \$data)
+ or croak "can't open filehandle for data string";
my $d = $class->new(filename => $file,
fh => $fh,
line => $line,
diff --git a/t/ConfigSpec3.pm b/t/ConfigSpec3.pm
new file mode 100644
index 0000000..51401c3
--- /dev/null
+++ b/t/ConfigSpec3.pm
@@ -0,0 +1,21 @@
+package ConfigSpec3;
+use parent 'TestConfig';
+
+sub mangle {
+ my $self = shift;
+ my $rootdir = $self->get(qw(core root));
+ foreach my $kw ($self->names_of('dir')) {
+ my $subdir = $self->get('dir', $kw);
+ $self->set('dir', $kw, $rootdir . '/' . $subdir);
+ }
+ return $res;
+}
+
+1;
+__DATA__
+[core]
+ root = STRING :mandatory
+[dir]
+ temp = STRING
+ store = STRING
+ diag = STRING
diff --git a/t/TestConfig.pm b/t/TestConfig.pm
index bd4f3f5..08f5bae 100644
--- a/t/TestConfig.pm
+++ b/t/TestConfig.pm
@@ -47,9 +47,12 @@ sub new {
# FIXME: Filter out fh and line keywords?
my $self = $class->SUPER::new(%_);
$self->{_expected_errors} = $exp if $exp;
+ if (-s $file->filename) {
$self->parse($file->filename);
$self->{_status} = $self->commit;
-
+ } else {
+ $self->{_status} = 1;
+ }
if ($exp && @{$self->{_expected_errors}}) {
$self->{_status} = 0;
$self->error("not all expected errors reported");
diff --git a/t/conf08.t b/t/conf08.t
new file mode 100644
index 0000000..c82f282
--- /dev/null
+++ b/t/conf08.t
@@ -0,0 +1,18 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec3;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec3;
+ok($c->canonical,q{core.root="/var" dir.diag="/var/log" dir.store="/var/spool" dir.temp="/var/tmp"});
+
+__DATA__
+[core]
+ root = /var
+[dir]
+ temp = tmp
+ store = spool
+ diag = log

Return to:

Send suggestions and report system problems to the System administrator.