Skip to content

Commit

Permalink
Inherit from Pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
shinokaro committed May 29, 2024
1 parent 2d70aa7 commit 2747959
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 70 deletions.
74 changes: 4 additions & 70 deletions bin/ocran
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,11 @@
# encoding: UTF-8

module Ocran
require "pathname"
# Path handling class. Ruby's Pathname class is not used because it
# is case sensitive and doesn't handle paths with mixed path
# separators.
class Pathname
def Pathname.pwd
Pathname.new(Dir.pwd)
end

def Pathname.glob(pattern, flags = 0)
if block_given?
Dir.glob(pattern, flags) { |s| yield Pathname.new(s) }
else
ary = Dir.glob(pattern, flags)
ary.map! { |s| Pathname.new(s) }
ary
end
end

SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR.to_s}#{Regexp.quote File::SEPARATOR}]/
ABSOLUTE_PAT = /\A([A-Z]:)?#{SEPARATOR_PAT}/i

def initialize(path)
@path = path
end

class Pathname < ::Pathname
# 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 Down Expand Up @@ -114,7 +94,7 @@ module Ocran
if other.absolute?
other
else
Pathname.new(File.join(@path, other.to_s))
Pathname.new(plus(@path, other.to_s))
end
end

Expand All @@ -131,19 +111,7 @@ module Ocran
# pathname.append_to_filename("_bar") # => #<Pathname:path.to/foo_bar>
#
def append_to_filename(suffix)
sub(/(.*?#{SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3")
end

def sub(*a, &b)
Pathname.new(@path.sub(*a, &b))
end

def sub_ext(new_ext)
sub(/(\.[^.]*?)?$/, new_ext)
end

def extname
File.extname(@path)
sub(/(.*?#{Pathname::SEPARATOR_PAT})?(\.?[^.]+)?(\..*)?\z/, "\\1\\2#{suffix}\\3")
end

# Checks if the file's extension matches the expected extension.
Expand All @@ -152,40 +120,6 @@ module Ocran
def extname?(expected_ext)
extname.casecmp(expected_ext) == 0
end

def find(ignore_error: true)
require "find"
if block_given?
Find.find(@path, ignore_error: ignore_error) { |path| yield Pathname.new(path) }
else
Find.find(@path, ignore_error: ignore_error).map{ |path| Pathname.new(path) }.to_enum
end
end

def exist?; File.exist?(@path); end
def file?; File.file?(@path); end
def directory?; File.directory?(@path); end
def absolute?; @path =~ ABSOLUTE_PAT; end
def dirname; Pathname.new(File.dirname(@path)); end
def basename; Pathname.new(File.basename(@path)); end

def expand_path(dir = ".")
Pathname.new(File.expand_path(@path, dir))
end

def size; File.size(@path); end

def binread(*args)
File.binread(@path, *args)
end

def parent
Pathname.new(File.basename(File.dirname(@path)))
end

def to_path; @path; end

def to_s; @path; end
end

# Type conversion for the Pathname class. Works with Pathname and String only.
Expand Down
5 changes: 5 additions & 0 deletions lib/ocran/pathname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true


module Ocran
end

0 comments on commit 2747959

Please sign in to comment.