1
0
Fork 0
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:
John Mair 2013-01-09 01:39:02 +01:00
parent 32eb6ca573
commit 4a059fd3a6
4 changed files with 107 additions and 102 deletions

View file

@ -1,17 +1,35 @@
class Pry class Pry
module Command::CodeCollector class Command::CodeCollector
include Helpers::CommandHelpers 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 :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, 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 :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, 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 :as => Range, :default => -5..-1
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count 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 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 def content
return @content if @content return @content if @content
raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination? 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) when opts.present?(:i)
pry_input_content pry_input_content
when opts.present?(:d) when opts.present?(:d)
code_object_docs code_object.doc
else else
code_object_source_or_file code_object_source_or_file
end end
@ -30,21 +48,53 @@ class Pry
@content ||= restrict_to_lines(content, line_range) @content ||= restrict_to_lines(content, line_range)
end end
def bad_option_combination? # The code object
[opts.present?(:in), opts.present?(:out), opts.present?(:d), #
!args.empty?].count(true) > 1 # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
def code_object
Pry::CodeObject.lookup(obj_name, _pry_.current_context, _pry_, :super => opts[:super])
end 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 def pry_output_content
pry_array_content_as_string(_pry_.output_array, opts[:o]) do |v| pry_array_content_as_string(_pry_.output_array, opts[:o]) do |v|
Pry.config.gist.inspecter.call(v) Pry.config.gist.inspecter.call(v)
end end
end end
# The selected `_pry_.input_array` as a string, as specified by
# the `-i` switch.
#
# @return [String]
def pry_input_content def pry_input_content
pry_array_content_as_string(_pry_.input_array, opts[:i]) { |v| v } pry_array_content_as_string(_pry_.input_array, opts[:i]) { |v| v }
end 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) 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 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) } array.each_with_object("") { |v, o| o << block.call(v) }
end 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 def code_object_source_or_file
(code_object && code_object.source) || file_content (code_object && code_object.source) || file_content
end end
@ -76,14 +114,6 @@ class Pry
end end
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) def could_not_locate(name)
raise CommandError, "Cannot locate: #{name}!" raise CommandError, "Cannot locate: #{name}!"
end end
@ -96,14 +126,6 @@ class Pry
end end
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 def obj_name
@obj_name ||= args.empty? ? no_arg : args.join(" ") @obj_name ||= args.empty? ? no_arg : args.join(" ")
end end

View file

@ -1,7 +1,5 @@
class Pry class Pry
class Command::Play < Pry::ClassCommand class Command::Play < Pry::ClassCommand
include Command::CodeCollector
match 'play' match 'play'
group 'Editing' group 'Editing'
description 'Play back a string variable or a method or a file as input.' description 'Play back a string variable or a method or a file as input.'
@ -22,11 +20,14 @@ class Pry
BANNER BANNER
def options(opt) 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.' 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 end
def process def process
@cc = CodeCollector.new(args, opts, _pry_)
perform_play perform_play
run "show-input" unless Pry::Code.complete_expression?(eval_string) run "show-input" unless Pry::Code.complete_expression?(eval_string)
end end
@ -35,8 +36,26 @@ class Pry
eval_string << (opts.present?(:open) ? restrict_to_lines(content, (0..-2)) : content) eval_string << (opts.present?(:open) ? restrict_to_lines(content, (0..-2)) : content)
end end
def no_arg def content
_pry_.last_file 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
end end

View file

@ -2,8 +2,6 @@ require 'pry/commands/code_collector'
class Pry class Pry
class Command::SaveFile < Pry::ClassCommand class Command::SaveFile < Pry::ClassCommand
include Command::CodeCollector
match 'save-file' match 'save-file'
group 'Input and Output' group 'Input and Output'
description 'Export to a file using content from the REPL.' description 'Export to a file using content from the REPL.'
@ -18,15 +16,17 @@ class Pry
USAGE USAGE
def options(opt) def options(opt)
super CodeCollector.inject_options(opt)
opt.on :to=, "Select where content is to be saved." opt.on :to=, "Select where content is to be saved."
opt.on :a, :append, "Append output to file." opt.on :a, :append, "Append output to file."
end end
def process 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 display_content
else else
save_file save_file
@ -34,22 +34,18 @@ class Pry
end end
def file_name def file_name
opts[:to] || "" opts[:to] || nil
end
def check_for_errors
raise CommandError, "Found no code to save." if content.empty?
end end
def save_file def save_file
File.open(file_name, mode) do |f| File.open(file_name, mode) do |f|
f.puts content f.puts @cc.content
end end
output.puts "#{file_name} successfully saved" output.puts "#{file_name} successfully saved"
end end
def display_content 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 "\n\n--\nPlease use `--to FILE` to export to a file."
output.puts "No file saved!\n--" output.puts "No file saved!\n--"
end end

View file

@ -7,60 +7,28 @@ describe "play" do
end end
describe "with an argument" do 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 # can't think of a f*cking way to test this!!
@t.eval 'x = "\"hello\"\n\"goodbye\"\n\"love\""' describe "implied file" do
@t.process_command 'play x --lines 1', @eval_str # it 'should play from the file associated with the current binding' do
@eval_str.should == "\"hello\"\n" # # require 'fixtures/play_helper'
end # end
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 }"| # describe "integer" do
end # 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 # describe "range" do
@tempfile.close(true) # it "should process multiple lines at once from _pry_.last_file" do
end # @t.process_command 'play --lines 1..3', @eval_str
# [/bing = :bing\n/, /bang = :bang\n/, /bong = :bong\n/].each { |str|
describe "integer" do # @eval_str.should =~ str
it "should process one line from _pry_.last_file" do # }
@t.process_command 'play --lines 1', @eval_str # end
@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
end end
end end