File tree Expand file tree Collapse file tree 5 files changed +60
-0
lines changed
lib/active_support/core_ext Expand file tree Collapse file tree 5 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ * Add ` Pathname#existence ` .
2
+
3
+ ``` ruby
4
+ Pathname .new (" file" ).existence&.read
5
+ ```
6
+
7
+ * Timo Schilling *
8
+
1
9
* Remove deprecate ` ActiveSupport::Multibyte::Unicode.default_normalization_form` .
2
10
3
11
* Rafael Mendon ça Fran ça*
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/pathname/existence"
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ class Pathname
4
+ # Returns the receiver if the named file exists otherwise returns +nil+.
5
+ # <tt>pathname.existence</tt> is equivalent to
6
+ #
7
+ # pathname.existence? ? object : nil
8
+ #
9
+ # For example, something like
10
+ #
11
+ # content = pathname.read if pathname.exist?
12
+ #
13
+ # becomes
14
+ #
15
+ # content = pathname.existence&.read
16
+ #
17
+ # @return [Pathname]
18
+ def existence
19
+ self if exist?
20
+ end
21
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../abstract_unit"
4
+ require "active_support/core_ext/pathname/existence"
5
+
6
+ class PathnameExistenceTest < ActiveSupport ::TestCase
7
+ def test_presence
8
+ existing = Pathname . new ( __FILE__ )
9
+ not_existing = Pathname . new ( "not existing" )
10
+ assert_equal existing , existing . existence
11
+ assert_nil not_existing . existence
12
+ end
13
+ end
Original file line number Diff line number Diff line change @@ -4055,3 +4055,18 @@ end
4055
4055
NOTE: Defined in ` active_support/core_ext/load_error.rb ` .
4056
4056
4057
4057
[ LoadError#is_missing? ] : https://api.rubyonrails.org/classes/LoadError.html#method-i-is_missing-3F
4058
+
4059
+ Extensions to Pathname
4060
+ -------------------------
4061
+
4062
+ ### ` existence `
4063
+
4064
+ The [ ` existence ` ] [ Pathname#existence ] method returns the receiver if the named file exists otherwise returns +nil+. It is useful for idioms like this:
4065
+
4066
+ ``` ruby
4067
+ content = Pathname .new (" file" ).existence&.read
4068
+ ```
4069
+
4070
+ NOTE: Defined in ` active_support/core_ext/pathname/existence.rb ` .
4071
+
4072
+ [ Pathname#existence ] : https://api.rubyonrails.org/classes/Pathname.html#method-i-existence
You can’t perform that action at this time.
0 commit comments