From e892316068e0eebfc970d723eb2cbbd18e42a85c Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Tue, 20 Mar 2018 14:40:00 +0100 Subject: Fix :delete adverb --- lib/Config.pm6 | 21 +++++++++++++++++++++ t/08-unsetting.t.t | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 t/08-unsetting.t.t diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 91fafc2..a4bf3af 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -214,6 +214,27 @@ class Config is Associative is export self; } + multi method unset(Str $key) + { + self.unset($key.split(".").Array); + } + + multi method unset(@parts) + { + my %index := $!content; + my $target = @parts.pop; + + for @parts.list -> $part { + %index{$part} = {} unless defined(%index{$part}); + + %index := %index{$part}; + } + + %index{$target}:delete if %index{$target}:exists; + + 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. diff --git a/t/08-unsetting.t.t b/t/08-unsetting.t.t new file mode 100644 index 0000000..8837989 --- /dev/null +++ b/t/08-unsetting.t.t @@ -0,0 +1,30 @@ +#! /usr/bin/env perl6 + +use v6.c; +use Test; +use lib "lib"; + +plan 8; + +use Config; + +my $config = Config.new; + +$config.read: %( + a => "b", + c => %( + d => "e", + ), +); + +dd $config.get(); + +ok $config:exists, "'a' exists"; +ok $config:delete, "'a' gets deleted"; +nok $config:exists, "'a' no longer exists"; +ok $config:exists, "'c' remains untouched"; + +ok $config.has("c.d"), "'c.d' exists"; +ok $config.unset("c.d"), "'c.d' gets deleted"; +nok $config.has("c.d"), "'c.d' no longer exists"; +ok $config.has("c"), "'c' still exists"; -- cgit v1.1