forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00fb4e6
commit 1a14bcb
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require "active_support/core_ext/pathname/existence" |
21 changes: 21 additions & 0 deletions
21
activesupport/lib/active_support/core_ext/pathname/existence.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class Pathname | ||
# Returns the receiver if the named file exists otherwise returns +nil+. | ||
# <tt>pathname.existence</tt> is equivalent to | ||
# | ||
# pathname.existence? ? object : nil | ||
# | ||
# For example, something like | ||
# | ||
# content = pathname.read if pathname.exist? | ||
# | ||
# becomes | ||
# | ||
# content = pathname.existence&.read | ||
# | ||
# @return [Pathname] | ||
def existence | ||
self if exist? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../abstract_unit" | ||
require "active_support/core_ext/pathname/existence" | ||
|
||
class PathnameExistenceTest < ActiveSupport::TestCase | ||
def test_presence | ||
existing = Pathname.new(__FILE__) | ||
not_existing = Pathname.new("not existing") | ||
assert_equal existing, existing.existence | ||
assert_nil not_existing.existence | ||
end | ||
end |