From d8e41fa8bbbe88b4ef4c412c8026cb94c9d3686f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luis=20Leal=20Cardoso=20Junior?= Date: Sun, 14 Apr 2024 16:51:45 -0300 Subject: [PATCH] Remove OpenStruct usage from Pry::CommandState --- lib/pry/command.rb | 2 +- lib/pry/command_state.rb | 17 +++++++++++------ lib/pry/commands/cd.rb | 2 ++ lib/pry/commands/edit.rb | 2 ++ lib/pry/commands/shell_command.rb | 2 +- lib/pry/commands/shell_mode.rb | 1 + lib/pry/commands/watch_expression.rb | 2 +- lib/pry/control_d_handler.rb | 2 +- spec/command_spec.rb | 11 ++--------- spec/command_state_spec.rb | 26 ++++++++++++++------------ spec/commands/cd_spec.rb | 2 +- spec/commands/shell_command_spec.rb | 4 ++-- 12 files changed, 39 insertions(+), 34 deletions(-) diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 4e213f399..94d08e262 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -200,7 +200,7 @@ def group(name = nil) end def state - Pry::CommandState.default.state_for(match) + Pry::CommandState.default.state_for(self) end end diff --git a/lib/pry/command_state.rb b/lib/pry/command_state.rb index fa98d5155..ef55f3137 100644 --- a/lib/pry/command_state.rb +++ b/lib/pry/command_state.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'ostruct' - class Pry # CommandState is a data structure to hold per-command state. # @@ -20,12 +18,19 @@ def initialize @command_state = {} end - def state_for(command_name) - @command_state[command_name] ||= OpenStruct.new + def state_for(command_class) + @command_state[command_class] ||= command_struct(command_class) + end + + def reset(command_class) + @command_state[command_class] = command_struct(command_class) end - def reset(command_name) - @command_state[command_name] = OpenStruct.new + private + + def command_struct(command_class) + Struct.new(:command, *command_class.command_options[:state]) + .new(command: command_class) end end end diff --git a/lib/pry/commands/cd.rb b/lib/pry/commands/cd.rb index d698e0a09..6d9f74e9b 100644 --- a/lib/pry/commands/cd.rb +++ b/lib/pry/commands/cd.rb @@ -22,6 +22,8 @@ class Cd < Pry::ClassCommand https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope BANNER + command_options state: %i[old_stack] + def process state.old_stack ||= [] diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb index 4c31e089f..3d6474ceb 100644 --- a/lib/pry/commands/edit.rb +++ b/lib/pry/commands/edit.rb @@ -22,6 +22,8 @@ class Edit < Pry::ClassCommand https://github.com/pry/pry/wiki/Editor-integration#wiki-Edit_command BANNER + command_options state: %i[dynamical_ex_file] + def options(opt) opt.on :e, :ex, "Open the file that raised the most recent exception " \ "(_ex_.file)", diff --git a/lib/pry/commands/shell_command.rb b/lib/pry/commands/shell_command.rb index 6069a8827..69b97911d 100644 --- a/lib/pry/commands/shell_command.rb +++ b/lib/pry/commands/shell_command.rb @@ -7,7 +7,7 @@ class ShellCommand < Pry::ClassCommand group 'Input and Output' description "All text following a '.' is forwarded to the shell." command_options listing: '.', use_prefix: false, - takes_block: true + takes_block: true, state: %i[old_pwd] banner <<-'BANNER' Usage: .COMMAND_NAME diff --git a/lib/pry/commands/shell_mode.rb b/lib/pry/commands/shell_mode.rb index a71adb94a..d33abaf6d 100644 --- a/lib/pry/commands/shell_mode.rb +++ b/lib/pry/commands/shell_mode.rb @@ -6,6 +6,7 @@ class ShellMode < Pry::ClassCommand match 'shell-mode' group 'Input and Output' description 'Toggle shell mode. Bring in pwd prompt and file completion.' + command_options state: %i[disabled prev_prompt] banner <<-'BANNER' Toggle shell mode. Bring in pwd prompt and file completion. diff --git a/lib/pry/commands/watch_expression.rb b/lib/pry/commands/watch_expression.rb index 2d977baec..6f7b1e289 100644 --- a/lib/pry/commands/watch_expression.rb +++ b/lib/pry/commands/watch_expression.rb @@ -7,7 +7,7 @@ class WatchExpression < Pry::ClassCommand group 'Context' description 'Watch the value of an expression and print a notification ' \ 'whenever it changes.' - command_options use_prefix: false + command_options use_prefix: false, state: %i[watch_expressions] banner <<-'BANNER' Usage: watch [EXPRESSION] diff --git a/lib/pry/control_d_handler.rb b/lib/pry/control_d_handler.rb index 1d3386d67..7f25abf7f 100644 --- a/lib/pry/control_d_handler.rb +++ b/lib/pry/control_d_handler.rb @@ -19,7 +19,7 @@ def self.default(pry_instance) else # Otherwise, saves current binding stack as old stack and pops last # binding out of binding stack (the old stack still has that binding). - cd_state = Pry::CommandState.default.state_for('cd') + cd_state = Pry::CommandState.default.state_for(Pry::Command::Cd) cd_state.old_stack = pry_instance.binding_stack.dup pry_instance.binding_stack.pop end diff --git a/spec/command_spec.rb b/spec/command_spec.rb index d56bc09ad..47901d8e4 100644 --- a/spec/command_spec.rb +++ b/spec/command_spec.rb @@ -420,7 +420,7 @@ def self.name describe ".state" do it "returns a command state" do - expect(described_class.state).to be_an(OpenStruct) + expect(described_class.state).to be_an(Struct) end end @@ -478,17 +478,10 @@ def process; end end describe "#state" do - let(:target) { binding } - subject { Class.new(described_class).new(pry_instance: Pry.new) } it "returns a state object" do - expect(subject.state).to be_an(OpenStruct) - end - - it "remembers the state" do - subject.state.foo = :bar - expect(subject.state.foo).to eq(:bar) + expect(subject.state).to be_an(Struct) end end diff --git a/spec/command_state_spec.rb b/spec/command_state_spec.rb index 264b772c0..936e8e90e 100644 --- a/spec/command_state_spec.rb +++ b/spec/command_state_spec.rb @@ -17,33 +17,35 @@ 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) + subject.state_for(Pry::Command::Cd).old_stack = 1 + expect(subject.state_for(Pry::Command::Cd).old_stack).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')) + expect(subject.state_for(Pry::Command::Cd)) + .not_to equal(subject.state_for(Pry::Command::Play)) end it "memoizes state for the same command" do - expect(subject.state_for('command')).to equal(subject.state_for('command')) + state_a = subject.state_for(Pry::Command::Cd) + state_b = subject.state_for(Pry::Command::Cd) + expect(state_a).to equal(state_b) 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 + subject.state_for(Pry::Command::Cd).old_stack = 1 + subject.reset(Pry::Command::Cd) + expect(subject.state_for(Pry::Command::Cd).old_stack).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') + subject.state_for(Pry::Command::Cd).old_stack = 1 + subject.state_for(Pry::Command::WatchExpression).watch_expressions = 1 + subject.reset(Pry::Command::Cd) - expect(subject.state_for('other-command').foobar).to eq(1) + expect(subject.state_for(Pry::Command::WatchExpression).watch_expressions).to eq(1) end end end diff --git a/spec/commands/cd_spec.rb b/spec/commands/cd_spec.rb index 9777bddb3..bfdf91284 100644 --- a/spec/commands/cd_spec.rb +++ b/spec/commands/cd_spec.rb @@ -27,7 +27,7 @@ def old_stack end end - after { Pry::CommandState.default.reset('cd') } + after { Pry::CommandState.default.reset(Pry::Command::Cd) } describe 'old stack toggling with `cd -`' do describe 'in fresh pry instance' do diff --git a/spec/commands/shell_command_spec.rb b/spec/commands/shell_command_spec.rb index f66207b4f..6c16bd0ad 100644 --- a/spec/commands/shell_command_spec.rb +++ b/spec/commands/shell_command_spec.rb @@ -7,12 +7,12 @@ @t = pry_tester(@o) do def command_state - Pry::CommandState.default.state_for(Pry::Command::ShellCommand.match) + Pry::CommandState.default.state_for(Pry::Command::ShellCommand) end end end - after { Pry::CommandState.default.reset(Pry::Command::ShellCommand.match) } + after { Pry::CommandState.default.reset(Pry::Command::ShellCommand) } describe ".cd" do before do