From 9ccca6acec9473d9204b7607b01c66f76796cc8e Mon Sep 17 00:00:00 2001 From: Jeffrey Lau Date: Thu, 21 Nov 2024 05:45:40 +0800 Subject: [PATCH] fix: Fix errors when modifying register attributes when unsaved --- lib/paneron/register/raw/register.rb | 12 +++++++---- spec/raw_register_spec.rb | 32 +++++++++++++++++++--------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/paneron/register/raw/register.rb b/lib/paneron/register/raw/register.rb index b47fd00..bfacdbf 100644 --- a/lib/paneron/register/raw/register.rb +++ b/lib/paneron/register/raw/register.rb @@ -402,10 +402,14 @@ def data_set_path(data_set_name) end def metadata - @metadata ||= YAML.safe_load_file( - register_yaml_path, - permitted_classes: [Time, Date, DateTime], - ) + @metadata ||= begin + YAML.safe_load_file( + register_yaml_path, + permitted_classes: [Time, Date, DateTime], + ) + rescue Errno::ENOENT + self.class.metadata_template + end end def metadata=(metadata) diff --git a/spec/raw_register_spec.rb b/spec/raw_register_spec.rb index 8fe7862..0bd77de 100644 --- a/spec/raw_register_spec.rb +++ b/spec/raw_register_spec.rb @@ -471,18 +471,30 @@ end describe "#title=" do - it "updates the metadata title" do - expect do - raw_register.title = "new title" - end.to change { raw_register.metadata["title"] }.from("register").to("new title") + shared_examples_for "changes title" do |old_title| + it "updates the metadata title" do + expect do + raw_register.title = "new title" + end.to change { raw_register.metadata["title"] }.from(old_title).to("new title") - expect do - raw_register.title = "new title" - end.to_not(change { raw_register.metadata["title"] }) + expect do + raw_register.title = "new title" + end.to_not(change { raw_register.metadata["title"] }) - expect do - raw_register.title = "new title 2" - end.to change { raw_register.title }.from("new title").to("new title 2") + expect do + raw_register.title = "new title 2" + end.to change { raw_register.title }.from("new title").to("new title 2") + end + end + + it_behaves_like "changes title", "register" + + describe "for an unsaved register" do + let(:raw_register) do + described_class.new("./another/new/register") + end + + it_behaves_like "changes title", "" end end end