Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Pathname#write where possible #318

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/tika_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/tika_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down