Skip to content

Commit

Permalink
Merge pull request #318 from ifad/chore/use-file-write
Browse files Browse the repository at this point in the history
Use `Pathname#write` where possible
  • Loading branch information
tagliala authored Sep 27, 2024
2 parents 2fb6bfa + 8c08566 commit 8dce844
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
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

0 comments on commit 8dce844

Please sign in to comment.