File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed
lib/active_support/core_ext/object Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change
1
+ * Yield instance to ` Object#with ` block
2
+
3
+ ``` ruby
4
+ client.with(timeout: 5_000 ) do |c |
5
+ c.get(" /commits" )
6
+ end
7
+ ```
8
+
9
+ * Sean Doyle *
10
+
1
11
* Use logical core count instead of physical core count to determine the
2
12
default number of workers when parallelizing tests.
3
13
Original file line number Diff line number Diff line change @@ -4,11 +4,13 @@ class Object
4
4
# Set and restore public attributes around a block.
5
5
#
6
6
# client.timeout # => 5
7
- # client.with(timeout: 1) do
8
- # client .timeout # => 1
7
+ # client.with(timeout: 1) do |c|
8
+ # c .timeout # => 1
9
9
# end
10
10
# client.timeout # => 5
11
11
#
12
+ # The receiver is yielded to the provided block.
13
+ #
12
14
# This method is a shorthand for the common begin/ensure pattern:
13
15
#
14
16
# old_value = object.attribute
@@ -28,7 +30,7 @@ def with(**attributes)
28
30
old_values [ key ] = public_send ( key )
29
31
public_send ( "#{ key } =" , value )
30
32
end
31
- yield
33
+ yield self
32
34
ensure
33
35
old_values . each do |key , old_value |
34
36
public_send ( "#{ key } =" , old_value )
Original file line number Diff line number Diff line change @@ -88,6 +88,10 @@ def initialize
88
88
assert_equal :mixed , @object . mixed_attr
89
89
end
90
90
91
+ test "yields the instance to the block" do
92
+ assert_equal "1" , @object . with ( public_attr : "1" , &:public_attr )
93
+ end
94
+
91
95
test "basic immediates don't respond to #with" do
92
96
assert_not_respond_to nil , :with
93
97
assert_not_respond_to true , :with
You can’t perform that action at this time.
0 commit comments