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

Use Pathname#write where possible #318

merged 1 commit into from
Sep 27, 2024

Conversation

tagliala
Copy link
Member

@tagliala tagliala commented Sep 27, 2024

Also refactor title to use Pathname#read instead of using the global
File.read

@tagliala tagliala force-pushed the chore/use-file-write branch 2 times, most recently from b83347a to 3f543cd Compare September 27, 2024 09:00
@tagliala tagliala changed the title Use File.write where possible Use Pathname#write where possible Sep 27, 2024
@tagliala
Copy link
Member Author

Copilot chat

The functionality of the refactored code is largely the same, but it has been modified to use Pathname objects and their methods for file operations instead of string concatenation and the File class methods directly. Here is a detailed comparison and analysis for potential breaking changes:

lib/document.rb

  1. Title Method:

    • Original:
      return '' unless File.exist?(directory + 'title')
      File.read(directory + 'title').chomp
    • Refactored:
      title_file = directory.join('title')
      return '' unless title_file.file?
      title_file.read.chomp
    • Functionality:
      • Both versions check for the existence of the file and read its contents.
    • Potential Breaking Changes: None. The refactored code should work the same as the original.
  2. Title Setter Method:

    • Original:
      File.open(directory + 'title', 'w') { |f| f.puts new_title }
    • Refactored:
      directory.join('title').write new_title
    • Functionality:
      • Both versions write the new title to the file.
    • Potential Breaking Changes: None. Both methods achieve the same result.
  3. Add File Method:

    • Original:
      File.write(directory + version + AUTHOR_FILE, author)
    • Refactored:
      directory.join(version, AUTHOR_FILE).write author
    • Functionality:
      • Both versions write the author information to the specified file.
    • Potential Breaking Changes: None. The functionality remains the same.
  4. Save Metadata Method:

    • Original:
      File.open(directory + 'metadata.json', "w") do |f|
        f.puts JSON.pretty_generate(to_hash)
      end
    • Refactored:
      directory.join('metadata.json').write JSON.pretty_generate(to_hash)
    • Functionality:
      • Both versions write the JSON metadata to the file.
    • Potential Breaking Changes: None. The refactored code should behave the same as the original.

lib/tika_config.rb

  1. Path For Method:
    • Original:
      File.write(file, format(TEMPLATE, language_alpha3: language_alpha3))
    • Refactored:
      file.write format(TEMPLATE, language_alpha3: language_alpha3)
    • Functionality:
      • Both versions write the formatted string to the file.
    • Potential Breaking Changes: None. The functionality remains unchanged.

Summary

The refactored code uses Pathname objects and their methods, which simplifies file operations and improves readability. There should be no breaking changes as the overall functionality and logic remain the same. Ensure that Pathname is required if not already done:

require 'pathname'

If Pathname is consistently used across the codebase, these changes should integrate smoothly without any issues.

Also refactor title to use `Pathname#read` instead of using the global
`File.read`
@tagliala
Copy link
Member Author

This pull request focuses on refactoring file operations to utilize the Pathname library for cleaner and more readable code. The most significant changes involve replacing File operations with Pathname methods across various methods in the lib/document.rb and lib/tika_config.rb files.

Refactoring file operations:

  • lib/document.rb: Updated the title method to use Pathname for file checks and reads.
  • lib/document.rb: Modified the title= method to use Pathname for writing the title.
  • lib/document.rb: Updated the add_file method to use Pathname for writing author information.
  • lib/document.rb: Refactored the save_metadata method to use Pathname for writing metadata.
  • lib/tika_config.rb: Changed the path_for! method to use Pathname for writing configuration files.

Test adjustments:

@tagliala tagliala merged commit 8dce844 into master Sep 27, 2024
3 checks passed
@tagliala tagliala deleted the chore/use-file-write branch September 27, 2024 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant