Add expression flag to play

This is in reference to issue #904.  In said issue it was mentioned that
this should be a `-L` flag because it was ostensibly an extension of the
`-l` flag.  Felt `-e` was more intention revealing.
This commit is contained in:
Jonathan Jackson and Travis Anderson 2013-07-22 19:03:23 -04:00 committed by Hashrocket Workstation
parent 0004cfde97
commit d6176275b6
2 changed files with 41 additions and 1 deletions

View File

@ -14,6 +14,7 @@ class Pry
play -i 20 --lines 1..3 # assumes lines of the input expression at 20
play -o 4 # the output of of an expression at 4
play Pry#repl -l 1..-1 # play the contents of Pry#repl method
play -e 2 # play from specified line until end of valid expression
play hello.rb # play a file
play Rakefile -l 5 # play line 5 of a file
play -d hi # play documentation of hi method
@ -29,6 +30,8 @@ class Pry
' for replaying methods and leaving the method definition' \
' "open". `amend-line` can then be used to' \
' modify the method.'
opt.on :e, :expression=, 'Executes until end of valid expression', :as => Integer
end
def process
@ -39,10 +42,28 @@ class Pry
end
def perform_play
eval_string << (opts.present?(:open) ? restrict_to_lines(content, (0..-2)) : content)
eval_string << content_after_options
run "fix-indent"
end
def content_after_options
if opts.present?(:open)
restrict_to_lines(content, (0..-2))
elsif opts.present?(:expression)
content_at_expression
else
content
end
end
def content_at_expression
code_object.expression_at(opts[:expression])
end
def code_object
Pry::Code.new(content)
end
def should_use_default_file?
!args.first && !opts.present?(:in) && !opts.present?(:out)
end

View File

@ -132,5 +132,24 @@ describe "play" do
d.should == 1
end
end
describe "play -e" do
it 'should run an expression from given line number' do
def @o.test_method
@s = [
1,2,3,
4,5,6
]
end
@t.process_command 'play test_method -e 2'
@t.eval_string.should == unindent(<<-STR, 0)
@s = [
1,2,3,
4,5,6
]
STR
end
end
end
end