Skip to content

Commit

Permalink
Improving default behavior of with_*_tmp_path methods
Browse files Browse the repository at this point in the history
In returning the file, this helps tidy up some jagged edges in which
concrete classes need to remember to return the correct value.

Now the "implicit" behavior does the more correct thing.
  • Loading branch information
jeremyf committed May 10, 2023
1 parent 4d5c471 commit 9bb74c0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def copy(_from_path, out_file)
# This space deliberately left blank; we need to pass a block for all of the magic to
# happen.
end
out_file
end
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/derivative_rodeo/generators/hocr_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def tesseractify(in_tmp_path, out_file)
out_file.with_new_tmp_path do |out_tmp_path|
run_tesseract(in_tmp_path, out_tmp_path)
end
out_file
end

##
Expand Down
12 changes: 12 additions & 0 deletions lib/derivative_rodeo/storage_adapters/base_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def initialize(file_uri)
# @param auto_write_file [Boolean] Provided as a testing helper method.
#
# @yieldparam tmp_file_path [String]
#
# @return [StorageAdapters::BaseAdapter]
# @see with_tmp_path
def with_new_tmp_path(auto_write_file: true, &block)
with_tmp_path(lambda { |_file_path, tmp_file_path, exist|
Expand All @@ -114,6 +116,9 @@ def with_new_tmp_path(auto_write_file: true, &block)
}, auto_write_file: auto_write_file, &block)
end

##
# @yieldparam tmp_file_path [String]
# @return [StorageAdapters::BaseAdapter]
def with_existing_tmp_path
raise NotImplementedError, "#{self.class}#with_existing_tmp_path"
end
Expand All @@ -128,6 +133,8 @@ def with_existing_tmp_path
# working with the {#with_new_tmp_path}
#
# @yieldparam tmp_file_path [String]
#
# @return [StorageAdapters::BaseAdapter]
def with_tmp_path(preamble_lambda, auto_write_file: false)
raise ArgumentError, 'Expected a block' unless block_given?

Expand All @@ -139,6 +146,11 @@ def with_tmp_path(preamble_lambda, auto_write_file: false)
end
# TODO: Do we need to ensure this?
self.tmp_file_path = nil

# In returning self we again remove the need for those calling #with_new_tmp_path,
# #with_tmp_path, and #with_new_tmp_path to remember to return the current Adapter.
# In other words removing the jagged edges of the code.
self
end

##
Expand Down

0 comments on commit 9bb74c0

Please sign in to comment.