Assign default value to `:listing` option

When you create a class command, there is a problem with `:listing`
option, which doesn't carry the correct default value. Consider the
example:

  class MakeMeHappy < Pry::ClassCommand
    match 'woot'
  end

`MakeMeHappy` command matches against 'woot' String, but its `:listing`
option is set to the "nil" String, which is incorrect. We can fix it by
setting `:listing` explicitly:

  command_options :listing => 'woot'

It's a repetitive task, so we can automate it. Holy smoke, this why we
all use computers, after all!

With help of this commit there is no need to set `:listing` manually.
Pry will handle it for you.

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
Kyrylo Silin 2013-01-04 03:44:57 +02:00
parent 1dda70c695
commit ad5bb4c399
2 changed files with 62 additions and 16 deletions

View File

@ -25,7 +25,11 @@ class Pry
attr_writer :match
def match(arg=nil)
@match = arg if arg
if arg
@command_options ||= default_options(arg)
@command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
@match = arg
end
@match
end

View File

@ -733,29 +733,71 @@ describe "Pry::Command" do
Pry.commands.delete 'my-test'
end
it "allows creation of custom sub-classes of Pry::Command" do
it "allows creation of custom subclasses of Pry::Command" do
pry_eval('my---test').should =~ /my-testmy-test/
end
it "never loses its command options" do
options_hash = {
:requires_gem => [],
:keep_retval => false,
:argument_required => false,
:interpolate => true,
:shellwords => false,
:listing => 'my-test',
:use_prefix => true,
:takes_block => false
}
MyTestCommand.options.should == options_hash
end
if !mri18_and_no_real_source_location?
it "shows the source of the process method" do
pry_eval('show-source my-test').should =~ /output.puts command_name/
end
end
describe "command options hash" do
it "is always present" do
options_hash = {
:requires_gem => [],
:keep_retval => false,
:argument_required => false,
:interpolate => true,
:shellwords => false,
:listing => 'my-test',
:use_prefix => true,
:takes_block => false
}
MyTestCommand.options.should == options_hash
end
describe ":listing option" do
it "defaults to :match if not set explicitly" do
class HappyNewYear < Pry::ClassCommand
match 'happy-new-year'
description 'Happy New Year 2013'
end
Pry.commands.add_command HappyNewYear
HappyNewYear.options[:listing].should == 'happy-new-year'
Pry.commands.delete 'happy-new-year'
end
it "can be set explicitly" do
class MerryChristmas < Pry::ClassCommand
match 'merry-christmas'
description 'Merry Christmas!'
command_options :listing => 'happy-holidays'
end
Pry.commands.add_command MerryChristmas
MerryChristmas.options[:listing].should == 'happy-holidays'
Pry.commands.delete 'merry-christmas'
end
it "equals to :match option's inspect, if :match is Regexp" do
class CoolWinter < Pry::ClassCommand
match /.*winter/
description 'Is winter cool or cool?'
end
Pry.commands.add_command CoolWinter
CoolWinter.options[:listing].should == '/.*winter/'
Pry.commands.delete /.*winter/
end
end
end
end
describe "commands can save state" do