From 579707dec9cba3e80e6ec5a9908c96c3695e4337 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sun, 26 Aug 2018 13:51:54 +0200 Subject: Add .clone method --- CHANGELOG.md | 4 ++++ lib/Config.pm6 | 10 ++++++++-- t/09-cloning.t | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 t/09-cloning.t diff --git a/CHANGELOG.md b/CHANGELOG.md index e023e2d..7dd5fe2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [UNRELEASED] +### Added +- `.clone` method now exists to create a clone of the Config object. + ## [2.0.0] - 2018-08-26 ### Changed - `.read` will now return the `Config` object, instead of a `Bool`. diff --git a/lib/Config.pm6 b/lib/Config.pm6 index 78dca0a..0fecadf 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -10,8 +10,8 @@ use Hash::Merge; unit class Config is Associative; has Hash $!content = {}; -has Str $!path = ""; -has Str $!parser = ""; +has Str $.path = ""; +has Str $.parser = ""; #| Clear the config. method clear() @@ -21,6 +21,12 @@ method clear() $!parser = ""; } +method clone ( + --> Config +) { + Config.new(:$!path, :$!parser).read: $!content; +} + #| Return the entire config hash. multi method get() { diff --git a/t/09-cloning.t b/t/09-cloning.t new file mode 100644 index 0000000..b13cd7d --- /dev/null +++ b/t/09-cloning.t @@ -0,0 +1,21 @@ +#! /usr/bin/env perl6 + +use v6.c; + +use Test; + +use Config; + +plan 2; + +my Config $a .= new.read: %( + foo => "bar", + baz => 42, +); + +my Config $b = $a.clone; + +is-deeply $a.get, $b.get, "B contains the same data as A"; +isnt $a, $b, "A and B are not the same object"; + +# vim: ft=perl6 noet -- cgit v1.1