-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for nested
no_commands
blocks.
In certain circumstances we may wish to nest `no_commands` blocks. For example, if we want to import any module which includes both methods and `attr_reader` invocations. However, since when exiting the no_commands block the @no_commands instance var is reset to `false` we get unexpected behaviour. This change uses a `NestedContext` object to track the depth of the no_command blocks and ensure that we only start creating commands again once we've left all of them.
- Loading branch information
Showing
5 changed files
with
70 additions
and
9 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,29 @@ | ||
class Thor | ||
class NestedContext | ||
def initialize | ||
@depth = 0 | ||
end | ||
|
||
def enter | ||
push | ||
|
||
yield | ||
ensure | ||
pop | ||
end | ||
|
||
def entered? | ||
@depth > 0 | ||
end | ||
|
||
private | ||
|
||
def push | ||
@depth += 1 | ||
end | ||
|
||
def pop | ||
@depth -= 1 | ||
end | ||
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
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,20 @@ | ||
require "helper" | ||
|
||
describe Thor::NestedContext do | ||
subject(:context) { described_class.new } | ||
|
||
describe "#enter" do | ||
it "is never empty within the entered block" do | ||
context.enter do | ||
context.enter {} | ||
|
||
expect(context).to be_entered | ||
end | ||
end | ||
|
||
it "is empty when outside of all blocks" do | ||
context.enter { context.enter {} } | ||
expect(context).not_to be_entered | ||
end | ||
end | ||
end |