Skip to content

Commit 34a8363

Browse files
committed
version 0.2: Use different syntax to make sure it only runs when needed. Make it backwards compatible.
1 parent 118565b commit 34a8363

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## [Unreleased]
22

3+
## [0.2.0] - 2023-01-19
4+
Changed the code to only add the monkeypatch if it is needed.
5+
Use alias_method because it is faster after all.
6+
7+
Thanks Austin Ziegler
8+
39
## [0.1.0] - 2023-01-19
410

511
- Initial release

file_exists.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
1111
spec.summary = "File.exists? and Dir.exists? for Ruby 3.2+"
1212
spec.description = "File.exists? and Dir.exists? were deprecated in Ruby 3.2. If you still need these methods. just require this Gem"
1313
spec.homepage = "https://github.com/largo/file_exists"
14-
spec.required_ruby_version = ">= 3.2.0"
14+
spec.required_ruby_version = ">= 2.0.0"
1515

1616

1717
spec.metadata["homepage_uri"] = spec.homepage

lib/file_exists.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# frozen_string_literal: true
22

33
require_relative "file_exists/version"
4-
module FileExists
5-
# Deprecated exists? method monkeypatched with the file_exists gem
6-
def exists?(path)
7-
exist?(path)
4+
5+
# Monkeypatch the File Class with the exists? method
6+
unless Dir.respond_to?(:exists?)
7+
class << Dir
8+
alias_method :exists?, :exist?
89
end
910
end
1011

11-
# Monkeypatch the File Class with the exists? method
12-
File.singleton_class.prepend(FileExists)
13-
# Monkeypatch the File Class with the exists? method
14-
Dir.singleton_class.prepend(FileExists)
12+
# Monkeypatch the Dir Class with the exists? method
13+
unless File.respond_to?(:exists?)
14+
class << File
15+
alias_method :exists?, :exist?
16+
end
17+
end

lib/file_exists/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module FileExists
4-
VERSION = "0.1.0"
4+
VERSION = "0.2.0"
55
end

0 commit comments

Comments
 (0)