1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Added rename() method to Pry#CommandSet

This enables whingers like RubyPanther to change commands such as 'ls'
to something more bland so they no longer suffer 'mental pollution'
This commit is contained in:
John Mair 2011-11-24 18:27:30 +13:00
parent c800c0f04f
commit 18c8ccdecc
2 changed files with 67 additions and 1 deletions

View file

@ -142,7 +142,6 @@ class Pry
commands[name] = Command.new(name, description, options, block)
end
# Execute a block of code before a command is invoked. The block also
# gets access to parameters that will be passed to the command and
# is evaluated in the same context.
@ -217,6 +216,9 @@ class Pry
end
end
# @param [String, Regexp] name_or_listing The name or listing name
# of the command to retrieve.
# @return [Command] The command object matched.
def find_command_by_name_or_listing(name_or_listing)
if commands[name_or_listing]
cmd = commands[name_or_listing]
@ -240,6 +242,30 @@ class Pry
commands[new_name].description = desc
end
# Rename a command. You must use the actual name of the command,
# not the listing name.
# @param [String, Regexp] new_name The new name for the command.
# @param [String, Regexp] old_name The command's current name.
# @param [Hash] options The optional configuration parameters,
# accepts the same as the `command` method, but also allows the
# command description to be passed this way too.
# @example Renaming the `ls` command and changing its description.
# Pry.config.commands.rename "dir", "ls", :description => "DOS friendly ls"
def rename_command(new_name, old_name, options={})
raise ArgumentError, "A command called '#{old_name}' was not found!" if !commands[old_name]
options = {
:listing => new_name,
:description => commands[old_name].description
}.merge!(options)
commands[new_name] = commands[old_name]
commands[new_name].name = new_name
commands[new_name].description = options.delete(:description)
commands[new_name].options.merge!(options)
commands.delete(old_name)
end
# Runs a command.
# @param [Object] context Object which will be used as self during the
# command.

View file

@ -271,6 +271,46 @@ describe Pry::CommandSet do
order.should == order.sort
end
describe "renaming a command" do
it 'should be able to rename and run a command' do
run = false
@set.command('foo') { run = true }
@set.rename_command('bar', 'foo')
@set.run_command(@ctx, 'bar')
run.should == true
end
it 'should not accept listing name when renaming a command' do
@set.command('foo', "", :listing => 'love') { }
lambda { @set.rename_command('bar', 'love') }.should.raise ArgumentError
end
it 'should be able to rename and run a command' do
run = false
@set.command('foo') { run = true }
@set.rename_command('bar', 'foo')
@set.run_command(@ctx, 'bar')
run.should == true
end
it 'should make old command name inaccessible' do
@set.command('foo') { }
@set.rename_command('bar', 'foo')
lambda { @set.run_command(@ctx, 'foo') }.should.raise Pry::NoCommandError
end
it 'should be able to pass in options when renaming command' do
desc = "hello"
listing = "bing"
@set.command('foo') { }
@set.rename_command('bar', 'foo', :description => desc, :listing => listing, :keep_retval => true)
@set.commands['bar'].description.should == desc
@set.commands['bar'].options[:listing].should == listing
@set.commands['bar'].options[:keep_retval].should == true
end
end
describe "command decorators - before_command and after_command" do
describe "before_command" do
it 'should be called before the original command' do