From 2747959f406b16b54b0621f22986de9213b7694b Mon Sep 17 00:00:00 2001 From: shinokaro Date: Wed, 29 May 2024 01:04:30 +0900 Subject: [PATCH] Inherit from Pathname --- bin/ocran | 74 +++---------------------------------------- lib/ocran/pathname.rb | 5 +++ 2 files changed, 9 insertions(+), 70 deletions(-) create mode 100644 lib/ocran/pathname.rb diff --git a/bin/ocran b/bin/ocran index 8e0923e..50cc034 100644 --- a/bin/ocran +++ b/bin/ocran @@ -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 @@ -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 @@ -131,19 +111,7 @@ module Ocran # pathname.append_to_filename("_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. @@ -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. diff --git a/lib/ocran/pathname.rb b/lib/ocran/pathname.rb new file mode 100644 index 0000000..ee977c2 --- /dev/null +++ b/lib/ocran/pathname.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + + +module Ocran +end