mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Pry::Command::CodeCollector: replace module inclusion with delegation
And implement play and save-file using the new delegator. 'gist' still needs to be re-implemented using CodeCollector. ************ TODO: ************** Couldn't write a test for `play --lines 4..5` with implied target.eval("__FILE__") parameter, need to come up with a decent test.
This commit is contained in:
parent
32eb6ca573
commit
4a059fd3a6
4 changed files with 107 additions and 102 deletions
|
@ -1,17 +1,35 @@
|
|||
class Pry
|
||||
module Command::CodeCollector
|
||||
class Command::CodeCollector
|
||||
include Helpers::CommandHelpers
|
||||
|
||||
def options(opt)
|
||||
attr_accessor :args
|
||||
attr_accessor :opts
|
||||
attr_accessor :_pry_
|
||||
|
||||
def initialize(args, opts, _pry_)
|
||||
@args = args
|
||||
@opts = opts
|
||||
@_pry_ = _pry_
|
||||
end
|
||||
|
||||
# Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
|
||||
def self.inject_options(opt)
|
||||
opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range.", :optional_argument => true, :as => Range, :default => 1..-1
|
||||
opt.on :o, :out, "Select lines from Pry's output result history. Takes an index or range.", :optional_argument => true,
|
||||
:as => Range, :default => -5..-1
|
||||
opt.on :i, :in, "Select lines from Pry's input expression history. Takes an index or range.", :optional_argument => true,
|
||||
:as => Range, :default => -5..-1
|
||||
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
|
||||
opt.on :d=, :doc=, "Select lines from the code object's documentation."
|
||||
opt.on :d, :doc, "Select lines from the code object's documentation."
|
||||
end
|
||||
|
||||
# The content (i.e code/docs) for the selected object.
|
||||
# If the user provided a bare code object, it returns the source.
|
||||
# If the user provided the `-i` or `-o` switches, it returns the
|
||||
# selected input/output lines joined as a string. If the user used
|
||||
# `-d CODE_OBJECT` it returns the docs for that code object.
|
||||
#
|
||||
# @return [String]
|
||||
def content
|
||||
return @content if @content
|
||||
raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?
|
||||
|
@ -22,7 +40,7 @@ class Pry
|
|||
when opts.present?(:i)
|
||||
pry_input_content
|
||||
when opts.present?(:d)
|
||||
code_object_docs
|
||||
code_object.doc
|
||||
else
|
||||
code_object_source_or_file
|
||||
end
|
||||
|
@ -30,21 +48,53 @@ class Pry
|
|||
@content ||= restrict_to_lines(content, line_range)
|
||||
end
|
||||
|
||||
def bad_option_combination?
|
||||
[opts.present?(:in), opts.present?(:out), opts.present?(:d),
|
||||
!args.empty?].count(true) > 1
|
||||
# The code object
|
||||
#
|
||||
# @return [Pry::WrappedModule, Pry::Method, Pry::Command]
|
||||
def code_object
|
||||
Pry::CodeObject.lookup(obj_name, _pry_.current_context, _pry_, :super => opts[:super])
|
||||
end
|
||||
|
||||
# Given a string and a range, return the `range` lines of that
|
||||
# string.
|
||||
#
|
||||
# @param [String] content
|
||||
# @param [Range, Fixnum] range
|
||||
# @return [String] The string restricted to the given range
|
||||
def restrict_to_lines(content, range)
|
||||
Array(content.lines.to_a[range]).join
|
||||
end
|
||||
|
||||
# The selected `_pry_.output_array` as a string, as specified by
|
||||
# the `-o` switch.
|
||||
#
|
||||
# @return [String]
|
||||
def pry_output_content
|
||||
pry_array_content_as_string(_pry_.output_array, opts[:o]) do |v|
|
||||
Pry.config.gist.inspecter.call(v)
|
||||
end
|
||||
end
|
||||
|
||||
# The selected `_pry_.input_array` as a string, as specified by
|
||||
# the `-i` switch.
|
||||
#
|
||||
# @return [String]
|
||||
def pry_input_content
|
||||
pry_array_content_as_string(_pry_.input_array, opts[:i]) { |v| v }
|
||||
end
|
||||
|
||||
# The line range passed to `--lines`
|
||||
def line_range
|
||||
opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def bad_option_combination?
|
||||
[opts.present?(:in), opts.present?(:out),
|
||||
!args.empty?].count(true) > 1
|
||||
end
|
||||
|
||||
def pry_array_content_as_string(array, range, &block)
|
||||
raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0
|
||||
|
||||
|
@ -52,18 +102,6 @@ class Pry
|
|||
array.each_with_object("") { |v, o| o << block.call(v) }
|
||||
end
|
||||
|
||||
def code_object(str=obj_name)
|
||||
Pry::CodeObject.lookup(str, target, _pry_, :super => opts[:super])
|
||||
end
|
||||
|
||||
def code_object_docs
|
||||
if co = code_object(opts[:d])
|
||||
co.doc
|
||||
else
|
||||
could_not_locate(opts[:d])
|
||||
end
|
||||
end
|
||||
|
||||
def code_object_source_or_file
|
||||
(code_object && code_object.source) || file_content
|
||||
end
|
||||
|
@ -76,14 +114,6 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
def line_range
|
||||
opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
|
||||
end
|
||||
|
||||
def restrict_to_lines(content, range)
|
||||
Array(content.lines.to_a[range]).join
|
||||
end
|
||||
|
||||
def could_not_locate(name)
|
||||
raise CommandError, "Cannot locate: #{name}!"
|
||||
end
|
||||
|
@ -96,14 +126,6 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# This can be overriden by subclasses which can define what to return
|
||||
# When no arg is given, i.e `play -l 1..10` the lack of an explicit
|
||||
# code object arg defaults to `_pry_.last_file` (since that's how
|
||||
# `play` implements `no_arg`).
|
||||
def no_arg
|
||||
nil
|
||||
end
|
||||
|
||||
def obj_name
|
||||
@obj_name ||= args.empty? ? no_arg : args.join(" ")
|
||||
end
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
class Pry
|
||||
class Command::Play < Pry::ClassCommand
|
||||
include Command::CodeCollector
|
||||
|
||||
match 'play'
|
||||
group 'Editing'
|
||||
description 'Play back a string variable or a method or a file as input.'
|
||||
|
@ -22,11 +20,14 @@ class Pry
|
|||
BANNER
|
||||
|
||||
def options(opt)
|
||||
super
|
||||
CodeCollector.inject_options(opt)
|
||||
|
||||
opt.on :open, "open", 'When used with the -m switch, it plays the entire method except the last line, leaving the method definition "open". `amend-line` can then be used to modify the method.'
|
||||
end
|
||||
|
||||
def process
|
||||
@cc = CodeCollector.new(args, opts, _pry_)
|
||||
|
||||
perform_play
|
||||
run "show-input" unless Pry::Code.complete_expression?(eval_string)
|
||||
end
|
||||
|
@ -35,8 +36,26 @@ class Pry
|
|||
eval_string << (opts.present?(:open) ? restrict_to_lines(content, (0..-2)) : content)
|
||||
end
|
||||
|
||||
def no_arg
|
||||
_pry_.last_file
|
||||
def content
|
||||
if args.first
|
||||
@cc.content
|
||||
else
|
||||
file_content
|
||||
end
|
||||
end
|
||||
|
||||
# The file to play from when no code object is specified.
|
||||
# e.g `play --lines 4..10`
|
||||
def default_file
|
||||
target.eval("__FILE__") && File.expand_path(target.eval("__FILE__"))
|
||||
end
|
||||
|
||||
def file_content
|
||||
if default_file && File.exists?(default_file)
|
||||
@cc.restrict_to_lines(File.read(default_file), @cc.line_range)
|
||||
else
|
||||
raise CommandError, "File does not exist! File was: #{default_file.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -2,8 +2,6 @@ require 'pry/commands/code_collector'
|
|||
|
||||
class Pry
|
||||
class Command::SaveFile < Pry::ClassCommand
|
||||
include Command::CodeCollector
|
||||
|
||||
match 'save-file'
|
||||
group 'Input and Output'
|
||||
description 'Export to a file using content from the REPL.'
|
||||
|
@ -18,15 +16,17 @@ class Pry
|
|||
USAGE
|
||||
|
||||
def options(opt)
|
||||
super
|
||||
CodeCollector.inject_options(opt)
|
||||
|
||||
opt.on :to=, "Select where content is to be saved."
|
||||
opt.on :a, :append, "Append output to file."
|
||||
end
|
||||
|
||||
def process
|
||||
check_for_errors
|
||||
@cc = CodeCollector.new(args, opts, _pry_)
|
||||
raise CommandError, "Found no code to save." if @cc.content.empty?
|
||||
|
||||
if file_name.empty?
|
||||
if !file_name
|
||||
display_content
|
||||
else
|
||||
save_file
|
||||
|
@ -34,22 +34,18 @@ class Pry
|
|||
end
|
||||
|
||||
def file_name
|
||||
opts[:to] || ""
|
||||
end
|
||||
|
||||
def check_for_errors
|
||||
raise CommandError, "Found no code to save." if content.empty?
|
||||
opts[:to] || nil
|
||||
end
|
||||
|
||||
def save_file
|
||||
File.open(file_name, mode) do |f|
|
||||
f.puts content
|
||||
f.puts @cc.content
|
||||
end
|
||||
output.puts "#{file_name} successfully saved"
|
||||
end
|
||||
|
||||
def display_content
|
||||
output.puts content
|
||||
output.puts @cc.content
|
||||
output.puts "\n\n--\nPlease use `--to FILE` to export to a file."
|
||||
output.puts "No file saved!\n--"
|
||||
end
|
||||
|
|
|
@ -7,60 +7,28 @@ describe "play" do
|
|||
end
|
||||
|
||||
describe "with an argument" do
|
||||
describe "string variable" do
|
||||
it "without --lines switch" do
|
||||
@t.eval 'x = "\"hello\""'
|
||||
@t.process_command 'play x', @eval_str
|
||||
@eval_str.should == '"hello"'
|
||||
end
|
||||
|
||||
it 'using --lines switch to select what to play' do
|
||||
@t.eval 'x = "\"hello\"\n\"goodbye\"\n\"love\""'
|
||||
@t.process_command 'play x --lines 1', @eval_str
|
||||
@eval_str.should == "\"hello\"\n"
|
||||
end
|
||||
end
|
||||
# can't think of a f*cking way to test this!!
|
||||
describe "implied file" do
|
||||
# it 'should play from the file associated with the current binding' do
|
||||
# # require 'fixtures/play_helper'
|
||||
# end
|
||||
|
||||
describe "implied _file_" do
|
||||
before do
|
||||
@tempfile = Tempfile.new(%w|pry .rb|)
|
||||
@tempfile.puts <<-EOS
|
||||
bing = :bing
|
||||
bang = :bang
|
||||
bong = :bong
|
||||
EOS
|
||||
@tempfile.flush
|
||||
|
||||
@t.eval %|_pry_.last_file = "#{ @tempfile.path }"|
|
||||
end
|
||||
# describe "integer" do
|
||||
# it "should process one line from _pry_.last_file" do
|
||||
# @t.process_command 'play --lines 1', @eval_str
|
||||
# @eval_str.should =~ /bing = :bing\n/
|
||||
# end
|
||||
# end
|
||||
|
||||
after do
|
||||
@tempfile.close(true)
|
||||
end
|
||||
|
||||
describe "integer" do
|
||||
it "should process one line from _pry_.last_file" do
|
||||
@t.process_command 'play --lines 1', @eval_str
|
||||
@eval_str.should =~ /bing = :bing\n/
|
||||
end
|
||||
end
|
||||
|
||||
describe "range" do
|
||||
it "should process multiple lines at once from _pry_.last_file" do
|
||||
@t.process_command 'play --lines 1..3', @eval_str
|
||||
[/bing = :bing\n/, /bang = :bang\n/, /bong = :bong\n/].each { |str|
|
||||
@eval_str.should =~ str
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "malformed" do
|
||||
it "should return nothing" do
|
||||
@t.process_command 'play --lines 69', @eval_str
|
||||
@eval_str.should == ''
|
||||
lambda { @t.process_command('play zZz') }.should.raise Pry::CommandError
|
||||
end
|
||||
# describe "range" do
|
||||
# it "should process multiple lines at once from _pry_.last_file" do
|
||||
# @t.process_command 'play --lines 1..3', @eval_str
|
||||
# [/bing = :bing\n/, /bang = :bang\n/, /bong = :bong\n/].each { |str|
|
||||
# @eval_str.should =~ str
|
||||
# }
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue