From c05c8f31c1da29121def2159f7238d41a8e18f54 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sat, 21 Sep 2013 21:16:28 +0200 Subject: [PATCH 1/2] add Pry::CommandSet#[]=. Pry::CommandSet#[]= can be used to re-assign commands or add new ones to a command set. The key matches the pattern of a command you'd like to replace and the value is the command you'd the pattern to be matched with. The key is really important. You could say this: Pry.commands.command "hello" do output.puts "Hello! :)" end Pry.commands["help"] = Pry.commands["hello"] I would expect "help" to print "Hello! :)", but the command is bound to the pattern "hello". A new copy of the "hello" command is created and the key is used as the match pattern, so "help" will print "Hello! :)". You can use #[]= to replace commands, maybe your own 'help' implementation: Pry.commands["help"] = MyHelpCommand Commands can also be deleted by assigning a value of nil: Pry.commands["help"] = nil I decided to implement this based on SO post found here: http://stackoverflow.com/questions/17475829/configure-help-output-in-pry --- lib/pry/command_set.rb | 58 +++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/pry/command_set.rb b/lib/pry/command_set.rb index ecefa8b1..19a3fa7f 100644 --- a/lib/pry/command_set.rb +++ b/lib/pry/command_set.rb @@ -152,12 +152,6 @@ class Pry @commands.each(&block) end - # Add a given command object to this set. - # @param [Command] command The subclass of Pry::Command you wish to add. - def add_command(command) - commands[command.match] = command - end - # Removes some commands from the set # @param [Array] searches the matches or listings of the commands to remove def delete(*searches) @@ -317,10 +311,56 @@ class Pry # Find a command that matches the given line # @param [String] val The line that might be a command invocation # @return [Pry::Command, nil] - def find_command(val) - commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last + def [](pattern) + commands.values.select do |command| + command.matches?(pattern) + end.sort_by do |command| + command.match_score(pattern) + end.last + end + alias_method :find_command, :[] + + # + # Re-assign the command found at _pattern_ with _command_. + # + # @param [Regexp, String] pattern + # The command to add or replace(found at _pattern_). + # + # @param [Pry::Command] command + # The command to add. + # + # @return [Pry::Command] + # Returns the new command (matched with "pattern".) + # + # @example + # Pry.commands["help"] = MyHelpCommand + # + def []=(pattern, command) + if command.equal?(nil) + return commands.delete(pattern) + end + unless command < Pry::Command + raise TypeError, "command is not a subclass of Pry::Command" + end + bind_command_to_pattern = pattern != command.match + if bind_command_to_pattern + command_copy = command.dup + command_copy.match = pattern + @commands[pattern] = command_copy + else + @commands[pattern] = command + end + end + + # + # Add a command to set. + # + # @param [Command] command + # a subclass of Pry::Command. + # + def add_command(command) + self[command.match] = command end - alias_method :[], :find_command # Find the command that the user might be trying to refer to. # @param [String] search The user's search. From df4b2aa14e0c0e7ee9d04a5d13c0f034f3982d51 Mon Sep 17 00:00:00 2001 From: Robert Gleeson Date: Sun, 22 Sep 2013 11:21:51 +0200 Subject: [PATCH 2/2] add basic CommandSet#[]= tests. --- spec/command_set_spec.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/command_set_spec.rb b/spec/command_set_spec.rb index 15fd7be8..3dcc2f47 100644 --- a/spec/command_set_spec.rb +++ b/spec/command_set_spec.rb @@ -12,6 +12,26 @@ describe Pry::CommandSet do } end + describe "[]=" do + it "removes a command from the command set" do + @set["help"].should.not == nil + @set["help"] = nil + @set["help"].should == nil + lambda { @set.run_command(TOPLEVEL_BINDING, "help") }.should.raise Pry::NoCommandError + end + + it "replaces a command" do + old_help = @set["help"] + @set["help"] = @set["pry-version"] + @set["help"].should.not == old_help + end + + it "rebinds the command with key" do + @set["help-1"] = @set["help"] + @set["help-1"].match.should == "help-1" + end + end + it 'should call the block used for the command when it is called' do run = false @set.command 'foo' do