Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2020 from pry/command-global-state
pry_instance: factor out command state to be global
- Loading branch information
Showing
10 changed files
with
100 additions
and
17 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
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 @@ | ||
require 'ostruct' | ||
|
||
class Pry | ||
# CommandState is a data structure to hold per-command state. | ||
# | ||
# Pry commands can store arbitrary state here. This state persists between | ||
# subsequent command invocations. All state saved here is unique to the | ||
# command. | ||
# | ||
# @since ?.?.? | ||
# @api private | ||
class CommandState | ||
def self.default | ||
@default ||= new | ||
end | ||
|
||
def initialize | ||
@command_state = {} | ||
end | ||
|
||
def state_for(command_name) | ||
@command_state[command_name] ||= OpenStruct.new | ||
end | ||
|
||
def reset(command_name) | ||
@command_state[command_name] = OpenStruct.new | ||
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
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,47 @@ | ||
RSpec.describe Pry::CommandState do | ||
describe ".default" do | ||
it "returns the default command state" do | ||
expect(described_class.default).to be_a(described_class) | ||
end | ||
|
||
context "when called multiple times" do | ||
it "returns the same command state" do | ||
first_state = described_class.default | ||
second_state = described_class.default | ||
expect(first_state).to eql(second_state) | ||
end | ||
end | ||
end | ||
|
||
describe "#state_for" do | ||
it "returns a state for the matching command" do | ||
subject.state_for('command').foobar = 1 | ||
expect(subject.state_for('command').foobar).to eq(1) | ||
end | ||
|
||
it "returns new state for new command" do | ||
expect(subject.state_for('command')) | ||
.not_to equal(subject.state_for('other-command')) | ||
end | ||
|
||
it "memoizes state for the same command" do | ||
expect(subject.state_for('command')).to equal(subject.state_for('command')) | ||
end | ||
end | ||
|
||
describe "#reset" do | ||
it "resets the command state for the given command" do | ||
subject.state_for('command').foobar = 1 | ||
subject.reset('command') | ||
expect(subject.state_for('command').foobar).to be_nil | ||
end | ||
|
||
it "doesn't reset command state for other commands" do | ||
subject.state_for('command').foobar = 1 | ||
subject.state_for('other-command').foobar = 1 | ||
subject.reset('command') | ||
|
||
expect(subject.state_for('other-command').foobar).to eq(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