aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2019-07-12 11:42:23 +0200
committerPatrick Spek <Tyil@users.noreply.github.com>2019-07-14 08:29:24 +0200
commit640dc5f8da3e42aa4d82aba450bcacd82046caf6 (patch)
tree71faaedfa0772e1609eb12b7c5bf53d412c95a35
parent67f036c37e062e2c864e35b1fe06b51e74eb523d (diff)
Attempt to allow IO::Path for path based arguments
-rw-r--r--lib/Config.pm645
-rw-r--r--lib/Config/Parser/NULL.pm614
2 files changed, 47 insertions, 12 deletions
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 0fecadf..3db882e 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -64,7 +64,7 @@ multi method get(List $keyparts, Any $default = Nil)
#| Get the name of the parser module to use for the
#| given path.
-method get-parser(Str $path, Str $parser = "" --> Str)
+multi method get-parser(Str $path, Str $parser = "" --> Str)
{
return $parser if $parser ne "";
return $!parser if $!parser ne "";
@@ -74,8 +74,13 @@ method get-parser(Str $path, Str $parser = "" --> Str)
"Config::Parser::" ~ $type;
}
+multi method get-parser(IO::Path $path, Str $parser = "" --> Str)
+{
+ samewith($path.absolute, $parser)
+}
+
#| Get the type of parser required for the given path.
-method get-parser-type(Str $path --> Str)
+multi method get-parser-type(Str $path --> Str)
{
given ($path) {
when .ends-with(".yml") { return "yaml"; };
@@ -94,6 +99,11 @@ method get-parser-type(Str $path --> Str)
return "";
}
+multi method get-parser-type(IO::Path $path --> Str)
+{
+ samewith($path.absolute)
+}
+
#| Check wether a given key exists.
multi method has(Str $key) {
self.has($key.split(".").list);
@@ -145,9 +155,21 @@ multi method read (
Bool :$skip-not-found = False,
--> Config
) {
- Config::Exception::FileNotFoundException.new(
- path => $path
- ).throw() unless ($path.IO.f || $skip-not-found);
+ samewith($path.IO, $parser, :$skip-not-found)
+}
+
+#| 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 (
+ IO::Path $path,
+ Str $parser = "",
+ Bool :$skip-not-found = False,
+ --> Config
+) {
+ if (!$path.f && !$skip-not-found) {
+ Config::Exception::FileNotFoundException.new(path => $path.absolute).throw();
+ }
$!parser = self.get-parser($path, $parser);
@@ -177,12 +199,10 @@ multi method read (
--> Config
) {
for $paths.list -> $path {
- next if $skip-not-found && !$path.IO.f;
-
- self.read($path, $parser);
+ samewith($path, $parser, :$skip-not-found);
}
- return self;
+ self;
}
#| Read a plain Hash into the configuration.
@@ -240,7 +260,12 @@ multi method unset(@parts)
#| 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 = "")
+multi method write(IO::Path $path, Str $parser = "")
+{
+ samewith($path.absolute, $parser)
+}
+
+multi method write(Str $path, Str $parser = "")
{
my $chosen-parser = self.get-parser($path, $parser);
diff --git a/lib/Config/Parser/NULL.pm6 b/lib/Config/Parser/NULL.pm6
index 5113043..2352cfc 100644
--- a/lib/Config/Parser/NULL.pm6
+++ b/lib/Config/Parser/NULL.pm6
@@ -12,11 +12,16 @@ class Config::Parser::NULL is Config::Parser
my %mock-config = ();
#| Return the mock config, skipping the file entirely.
- method read(Str $path --> Hash)
+ multi method read(Str $path --> Hash)
{
%mock-config;
}
+ multi method read(IO::Path $path --> Hash)
+ {
+ %mock-config;
+ }
+
#| Set the mock config to return on read.
method set-config(Hash $config)
{
@@ -24,7 +29,12 @@ class Config::Parser::NULL is Config::Parser
}
#| Return True, as if writing succeeded.
- method write(Str $path, Hash $config --> Bool)
+ multi method write(Str $path, Hash $config --> Bool)
+ {
+ True;
+ }
+
+ multi method write(IO::Path $path, Hash $config --> Bool)
{
True;
}