From cac9a5f9e429d2461bd2830bc983eced5ef14e1a Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 07:53:05 +0200
Subject: Make .read() merge the hashes and allow an array of paths to load
---
lib/Config.pm6 | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 2fa01bf..372eb3b 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -2,6 +2,8 @@
use v6.c;
+use Hash::Merge;
+
use Config::Exception::MissingParserException;
use Config::Exception::UnknownTypeException;
use Config::Exception::FileNotFoundException;
@@ -109,15 +111,23 @@ class Config is export
}
require ::($!parser);
- $!content = ::($!parser).read($path);
+
+ $!content.merge(::($!parser).read($path));
}
return True;
}
+ multi method read(Array $paths, Str $parser = "")
+ {
+ for $paths -> $path {
+ self.read($path, $parser);
+ }
+ }
+
multi method read(Hash $hash)
{
- $!content = $hash;
+ $!content.merge($hash);
}
method set(Str $key, Any $value)
--
cgit v1.1
From fd33799e2737e1fcce7b9bad44d0031f8dc9a2c9 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 07:53:25 +0200
Subject: Add a .clear() to clear the config
---
lib/Config.pm6 | 7 +++++++
1 file changed, 7 insertions(+)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 372eb3b..d07c640 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -16,6 +16,13 @@ class Config is export
has $!path;
has $!parser;
+ method clear()
+ {
+ $!content = {};
+ $!path = "";
+ $!parser = "";
+ }
+
multi method get(Str $key, Any $default = Nil)
{
my $index = $!content;
--
cgit v1.1
From 5de74ad36feede030f0400c0231bf0eaa3c4ea3e Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:00:47 +0200
Subject: Add some additional comments
---
lib/Config.pm6 | 37 +++++++++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 4 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index d07c640..97d708c 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -12,10 +12,11 @@ use Config::Parser;
class Config is export
{
- has $!content = {};
- has $!path;
- has $!parser;
+ has Hash $!content = {};
+ has Str $!path;
+ has Str $!parser;
+ #| Clear the config.
method clear()
{
$!content = {};
@@ -23,6 +24,8 @@ class Config is export
$!parser = "";
}
+ #| Get a value from the config object. To get a nested
+ #| key, use a . to descent a level.
multi method get(Str $key, Any $default = Nil)
{
my $index = $!content;
@@ -36,6 +39,8 @@ class Config is export
$index;
}
+ #| Get a value from the config object using an array
+ #| to indicate the nested key to get.
multi method get(@keyparts, Any $default = Nil)
{
my $index = $!content;
@@ -49,6 +54,8 @@ class Config is export
$index;
}
+ #| Get the name of the parser module to use for the
+ #| given path.
method get-parser(Str $path, Str $parser = "")
{
if ($parser ne "") {
@@ -64,6 +71,7 @@ class Config is export
"Config::Parser::" ~ $type;
}
+ #| Get the type of parser required for the given path.
method get-parser-type(Str $path)
{
given ($path) {
@@ -83,6 +91,7 @@ class Config is export
return Config::Type::unknown;
}
+ #| Check wether a given key exists.
method has(Str $key) {
my $index = $!content;
@@ -95,11 +104,20 @@ class Config is export
True;
}
+ #| Reload the configuration. Requires the configuration to
+ #| have been loaded from a file.
multi method read()
{
+ if ($!path eq "") {
+ return False;
+ }
+
return self.load($!path);
}
+ #| Load a configuration file from the given path. Optionally
+ #| set a parser module name to use. If not set, Config will
+ #| attempt to deduce the parser to use.
multi method read(Str $path, Str $parser = "")
{
Config::Exception::FileNotFoundException.new(
@@ -119,24 +137,32 @@ class Config is export
require ::($!parser);
- $!content.merge(::($!parser).read($path));
+ self.read(::($!parser).read($path));
}
return True;
}
+ #| Read an array of paths. Will fail on the first file that
+ #| fails to load for whatever reason.
multi method read(Array $paths, Str $parser = "")
{
for $paths -> $path {
self.read($path, $parser);
}
+
+ return True;
}
+ #| Read a plain Hash into the configuration.
multi method read(Hash $hash)
{
$!content.merge($hash);
+
+ return True;
}
+ #| Set a single key to a given value;
method set(Str $key, Any $value)
{
my $index := $!content;
@@ -152,6 +178,9 @@ class Config is export
self;
}
+ #| Write the current configuration to the given path. If
+ #| no parser is given, it tries to use the parser that
+ #| was used when loading the configuration.
method write(Str $path, Str $parser = "")
{
$parser = self.get-parser($path, $parser);
--
cgit v1.1
From 632f695dc8726e3c63dac004a148aac7c95b1191 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:05:14 +0200
Subject: Add a multi method has() that accepts an array param
---
lib/Config.pm6 | 27 ++++++++++++---------------
1 file changed, 12 insertions(+), 15 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 97d708c..2369b6f 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -28,24 +28,16 @@ class Config is export
#| key, use a . to descent a level.
multi method get(Str $key, Any $default = Nil)
{
- my $index = $!content;
-
- for $key.split(".") -> $part {
- return $default unless defined($index{$part});
-
- $index = $index{$part};
- }
-
- $index;
+ self.get($key.split("."), $default);
}
#| Get a value from the config object using an array
#| to indicate the nested key to get.
- multi method get(@keyparts, Any $default = Nil)
+ multi method get(Array $keyparts, Any $default = Nil)
{
my $index = $!content;
- for @keyparts -> $part {
+ for $keyparts -> $part {
return $default unless defined($index{$part});
$index = $index{$part};
@@ -92,16 +84,21 @@ class Config is export
}
#| Check wether a given key exists.
- method has(Str $key) {
+ multi method has(Str $key) {
+ self.has($key.split("."));
+ }
+
+ #| Check wether a given key exists using an array to supply
+ #| the nested key to check.
+ multi method has(Array $keyparts)
+ {
my $index = $!content;
- for $key.split(".") -> $part {
+ for $keyparts -> $part {
return False unless defined($index{$part});
$index = $index{$part};
}
-
- True;
}
#| Reload the configuration. Requires the configuration to
--
cgit v1.1
From 348a75d03fee516bb0dc0854b20336a40dd59b74 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:09:05 +0200
Subject: Add a multi method set() that accepts an array for the key
---
lib/Config.pm6 | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 2369b6f..1d0d7db 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -160,11 +160,16 @@ class Config is export
}
#| Set a single key to a given value;
- method set(Str $key, Any $value)
+ multi method set(Str $key, Any $value)
+ {
+ self.set($key.split("."), $value);
+ }
+
+ multi method set(Array $keyparts, Any $value)
{
my $index := $!content;
- for $key.split(".") -> $part {
+ for $keyparts -> $part {
$index{$part} = {} unless defined($index{$part});
$index := $index{$part};
--
cgit v1.1
From 8cc5724b273133ce645589b7c7e0cacd60276360 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:10:33 +0200
Subject: Initialize path and parser with empty string
---
lib/Config.pm6 | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 1d0d7db..ae78ea1 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -13,8 +13,8 @@ use Config::Parser;
class Config is export
{
has Hash $!content = {};
- has Str $!path;
- has Str $!parser;
+ has Str $!path = "";
+ has Str $!parser = "";
#| Clear the config.
method clear()
--
cgit v1.1
From 070e5a9e406fee7eeeb48481a048e79fb4d531a6 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:18:45 +0200
Subject: Add a get without params to get the entire config hash
---
lib/Config.pm6 | 6 ++++++
1 file changed, 6 insertions(+)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index ae78ea1..b9bc582 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -24,6 +24,12 @@ class Config is export
$!parser = "";
}
+ #| Return the entire config hash.
+ multi method get()
+ {
+ return $!content;
+ }
+
#| Get a value from the config object. To get a nested
#| key, use a . to descent a level.
multi method get(Str $key, Any $default = Nil)
--
cgit v1.1
From 3f4b16f712aa4b5c610319c79b7e1d7d04140503 Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 08:59:30 +0200
Subject: Fix the easy bugs
---
lib/Config.pm6 | 30 ++++++++++++++++--------------
1 file changed, 16 insertions(+), 14 deletions(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index b9bc582..22cd2a3 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -34,16 +34,16 @@ class Config is export
#| key, use a . to descent a level.
multi method get(Str $key, Any $default = Nil)
{
- self.get($key.split("."), $default);
+ self.get($key.split(".").list, $default);
}
- #| Get a value from the config object using an array
+ #| Get a value from the config object using a list
#| to indicate the nested key to get.
- multi method get(Array $keyparts, Any $default = Nil)
+ multi method get(List $keyparts, Any $default = Nil)
{
my $index = $!content;
- for $keyparts -> $part {
+ for $keyparts.list -> $part {
return $default unless defined($index{$part});
$index = $index{$part};
@@ -91,20 +91,22 @@ class Config is export
#| Check wether a given key exists.
multi method has(Str $key) {
- self.has($key.split("."));
+ self.has($key.split(".").list);
}
- #| Check wether a given key exists using an array to supply
+ #| Check wether a given key exists using a list to supply
#| the nested key to check.
- multi method has(Array $keyparts)
+ multi method has(List $keyparts)
{
my $index = $!content;
- for $keyparts -> $part {
+ for $keyparts.list -> $part {
return False unless defined($index{$part});
$index = $index{$part};
}
+
+ defined($index);
}
#| Reload the configuration. Requires the configuration to
@@ -146,11 +148,11 @@ class Config is export
return True;
}
- #| Read an array of paths. Will fail on the first file that
+ #| Read a list of paths. Will fail on the first file that
#| fails to load for whatever reason.
- multi method read(Array $paths, Str $parser = "")
+ multi method read(List $paths, Str $parser = "")
{
- for $paths -> $path {
+ for $paths.list -> $path {
self.read($path, $parser);
}
@@ -168,14 +170,14 @@ class Config is export
#| Set a single key to a given value;
multi method set(Str $key, Any $value)
{
- self.set($key.split("."), $value);
+ self.set($key.split(".").list, $value);
}
- multi method set(Array $keyparts, Any $value)
+ multi method set(List $keyparts, Any $value)
{
my $index := $!content;
- for $keyparts -> $part {
+ for $keyparts.list -> $part {
$index{$part} = {} unless defined($index{$part});
$index := $index{$part};
--
cgit v1.1
From 2fa145223277129586ae290edbd5362d6904507d Mon Sep 17 00:00:00 2001
From: Patrick Spek
Date: Wed, 26 Apr 2017 09:03:35 +0200
Subject: Fix thrown exception
---
lib/Config.pm6 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'lib/Config.pm6')
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 22cd2a3..40bfa27 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -63,7 +63,7 @@ class Config is export
my $type = self.get-parser-type($path);
Config::Exception::UnknownTypeException.new(
- type => $type
+ file => $path
).throw() if $type eq Config::Type::unknown;
"Config::Parser::" ~ $type;
--
cgit v1.1