From 8c0856677171c8bc97723f1b46ca43525356f8df Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 27 Sep 2024 11:04:26 +0200 Subject: [PATCH] Use Pathname#write where possible Also refactor title to use `Pathname#read` instead of using the global `File.read` --- lib/document.rb | 13 ++++++------- lib/tika_config.rb | 2 +- spec/lib/tika_config_spec.rb | 6 +++--- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/document.rb b/lib/document.rb index 529a53b..81d16fe 100644 --- a/lib/document.rb +++ b/lib/document.rb @@ -90,16 +90,17 @@ def directory # @return the document title. def title - return '' unless File.exist?(directory + 'title') + title_file = directory.join('title') + return '' unless title_file.file? - File.read(directory + 'title').chomp + title_file.read.chomp end # Sets the document title. def title=(new_title) return if new_title.to_s.empty? - File.open(directory + 'title', 'w') { |f| f.puts new_title } + directory.join('title').write new_title end # Returns an array of the document version identifiers. @@ -149,7 +150,7 @@ def add_file(version, filename, body, author = nil) body = StringIO.new(body) unless body.respond_to?(:read) # string -> IO File.open(directory + version + filename, "wb") { |f| IO.copy_stream(body, f) } - File.write(directory + version + AUTHOR_FILE, author) + directory.join(version, AUTHOR_FILE).write author end # Sets the specified version as current. @@ -233,9 +234,7 @@ def to_hash # This metadata is just the {#to_hash}, as JSON, and is intended for access by client # applications. It is not used by Colore for anything. def save_metadata - File.open(directory + 'metadata.json', "w") do |f| - f.puts JSON.pretty_generate(to_hash) - end + directory.join('metadata.json').write JSON.pretty_generate(to_hash) end end end diff --git a/lib/tika_config.rb b/lib/tika_config.rb index f6d2156..4232914 100644 --- a/lib/tika_config.rb +++ b/lib/tika_config.rb @@ -39,7 +39,7 @@ def path_for!(language_alpha3) return file if file.file? FileUtils.mkdir_p(tika_config_path.join('ocr', VERSION)) - File.write(file, format(TEMPLATE, language_alpha3: language_alpha3)) + file.write format(TEMPLATE, language_alpha3: language_alpha3) file end end diff --git a/spec/lib/tika_config_spec.rb b/spec/lib/tika_config_spec.rb index e2bf234..455c743 100644 --- a/spec/lib/tika_config_spec.rb +++ b/spec/lib/tika_config_spec.rb @@ -45,14 +45,14 @@ let(:language) { 'en' } before do - allow(File).to receive(:write) - .with(tika_test_config_path.join('ocr', described_class::VERSION, 'tika.eng.xml'), an_instance_of(String)) + allow(FileUtils).to receive(:mkdir_p) + .with(tika_test_config_path.join('ocr', described_class::VERSION)) .and_call_original end it 'does not overwrite it' do 2.times { described_class.path_for(language) } - expect(File).to have_received(:write).once + expect(FileUtils).to have_received(:mkdir_p).once end end end