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

added Pry.config.extra_sticky_locals config option

* Pry.config.extra_sticky_locals[:random] = proc { rand(10) }
* alternatively, Pry.start binding, :extra_sticky_locals => { :random => proc { rand(10) } }
* updated tests
This commit is contained in:
John Mair 2012-03-14 15:33:51 +13:00
parent cf9f41f5d0
commit e5bef51789
5 changed files with 95 additions and 5 deletions

View file

@ -171,6 +171,12 @@ class Pry
# Pry.config.gist.inspecter = proc &:inspect
# @return [OpenStruct]
attr_accessor :gist
# @return [Hash] Additional sticky locals (to the standard ones) to use in Pry sessions.
# @example Inject `random_number` sticky local into Pry session
# Pry.config.extra_sticky_locals = { :random_number => proc {
# rand(10) } }
attr_accessor :extra_sticky_locals
end
end

View file

@ -343,7 +343,7 @@ class Pry
self.content << File.read(File.expand_path(file))
end
opt.on :l, :lines, "Only play a subset of lines.", :optional => true, :as => Range, :default => 1..-1
opt.on :i, :in, "Play entries from Pry's input expression history. Takes an index or range.", :optional => true,
opt.on :i, :in, "Play entries from Pry's input expression history. Takes an index or range. Note this can only replay pure Ruby code, not Pry commands.", :optional => true,
:as => Range, :default => -5..-1 do |range|
input_expressions = _pry_.input_array[range] || []
Array(input_expressions).each { |v| self.content << v }

View file

@ -50,7 +50,7 @@ class Pry
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
:hooks, :color, :pager, :editor, :memory_size, :input_stack
:hooks, :color, :pager, :editor, :memory_size, :input_stack, :extra_sticky_locals
end
# Load the rc files given in the `Pry::RC_FILES` array.
@ -261,6 +261,8 @@ class Pry
config.memory_size = 100
config.extra_sticky_locals = {}
config.ls ||= OpenStruct.new({
:heading_color => :default,

View file

@ -24,6 +24,8 @@ class Pry
attr_accessor :backtrace
attr_accessor :extra_sticky_locals
# Special treatment for hooks as we want to alert people of the
# changed API
attr_reader :hooks
@ -65,7 +67,7 @@ class Pry
attributes = [
:input, :output, :commands, :print,
:exception_handler, :hooks, :custom_completions,
:prompt, :memory_size, :input_stack
:prompt, :memory_size, :input_stack, :extra_sticky_locals
]
attributes.each do |attribute|
@ -155,7 +157,7 @@ class Pry
:_file_ => proc { last_file },
:_dir_ => proc { last_dir },
:_ => proc { last_result }
}
}.merge(extra_sticky_locals)
end
# Initialize the repl session.

View file

@ -32,7 +32,87 @@ describe "Sticky locals (_file_ and friends)" do
Pry.commands.delete "file-and-dir-test"
end
describe "User defined sticky locals, Pry#add_sticky_local()" do
describe "User defined sticky locals" do
describe "setting as Pry.config option" do
it 'should define a new sticky local for the session (normal value)' do
Pry.config.extra_sticky_locals[:test_local] = :john
o = Object.new
redirect_pry_io(InputTester.new("@value = test_local",
"exit-all")) do
Pry.start(o)
end
o.instance_variable_get(:@value).should == :john
Pry.config.extra_sticky_locals = {}
end
it 'should define a new sticky local for the session (proc)' do
Pry.config.extra_sticky_locals[:test_local] = proc { :john }
o = Object.new
redirect_pry_io(InputTester.new("@value = test_local",
"exit-all")) do
Pry.start(o)
end
o.instance_variable_get(:@value).should == :john
Pry.config.extra_sticky_locals = {}
end
end
describe "passing in as hash option when creating pry instance" do
it 'should define a new sticky local for the session (normal value)' do
o = Object.new
redirect_pry_io(InputTester.new("@value = test_local",
"exit-all")) do
Pry.start(o, :extra_sticky_locals => { :test_local => :john } )
end
o.instance_variable_get(:@value).should == :john
end
it 'should define multiple sticky locals' do
o = Object.new
redirect_pry_io(InputTester.new("@value1 = test_local1",
"@value2 = test_local2",
"exit-all")) do
Pry.start(o, :extra_sticky_locals => { :test_local1 => :john ,
:test_local2 => :carl} )
end
o.instance_variable_get(:@value1).should == :john
o.instance_variable_get(:@value2).should == :carl
end
it 'should define a new sticky local for the session (as Proc)' do
o = Object.new
redirect_pry_io(InputTester.new("@value = test_local",
"exit-all")) do
Pry.start(o, :extra_sticky_locals => { :test_local => proc { :john }} )
end
o.instance_variable_get(:@value).should == :john
end
end
describe "hash option value should override config value" do
it 'should define a new sticky local for the session (normal value)' do
Pry.config.extra_sticky_locals[:test_local] = :john
o = Object.new
redirect_pry_io(InputTester.new("@value = test_local",
"exit-all")) do
Pry.start(o, :extra_sticky_locals => { :test_local => :carl })
end
o.instance_variable_get(:@value).should == :carl
Pry.config.extra_sticky_locals = {}
end
end
it 'should create a new sticky local' do
o = Object.new
pi = Pry.new