Skip to content

Commit

Permalink
Refactor path normalization logic in RefinePathname module
Browse files Browse the repository at this point in the history
- Extract path separator normalization logic from to_posix method to a private constant NORMALIZE_FILE_SEPARATOR.
- Apply NORMALIZE_FILE_SEPARATOR in the to_posix method and eql? method for consistent path normalization.
- Ensure NORMALIZE_FILE_SEPARATOR is private to the RefinePathname module to encapsulate the implementation details.
  • Loading branch information
shinokaro committed Jun 2, 2024
1 parent 9ae37d6 commit 8ec62f5
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ module Ocran
# The Pathname class in Ruby is modified to handle mixed path separators and
# to be case-insensitive.
module RefinePathname
NORMALIZE_FILE_SEPARATOR = if File::ALT_SEPARATOR
proc { |s| s.tr(File::ALT_SEPARATOR, File::SEPARATOR) }
else
proc { |s| s }
end
private_constant :NORMALIZE_FILE_SEPARATOR

# Compares two paths for equality based on the case sensitivity of the
# Ruby execution environment's file system.
# If the file system is case-insensitive, it performs a case-insensitive
Expand All @@ -21,11 +28,7 @@ module Ocran
private :pathequal

def to_posix
if File::ALT_SEPARATOR
to_s.tr(File::ALT_SEPARATOR, File::SEPARATOR)
else
to_s
end
NORMALIZE_FILE_SEPARATOR[to_s]
end

# Checks if two Pathname objects are equal, considering the file system's
Expand All @@ -35,8 +38,8 @@ module Ocran
def eql?(other)
return false unless other.is_a?(Pathname)

a = to_posix
b = other.to_posix
a = NORMALIZE_FILE_SEPARATOR[to_s]
b = NORMALIZE_FILE_SEPARATOR[other.to_s]
pathequal(a, b)
end

Expand Down

0 comments on commit 8ec62f5

Please sign in to comment.