Skip to content

Commit

Permalink
Use File.write where possible
Browse files Browse the repository at this point in the history
Also refactor title to use `Pathname#read` instead of using the global
`File.read`
  • Loading branch information
tagliala committed Sep 27, 2024
1 parent 2fb6bfa commit b83347a
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 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 }
File.write(directory.join('title'), new_title)
end

# Returns an array of the document version identifiers.
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
File.write(directory.join('metadata.json'), JSON.pretty_generate(to_hash))
end
end
end

0 comments on commit b83347a

Please sign in to comment.