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

Remove unnecessary classes / modules used by config (#1897)

* Remove` Pry::Config::Default`

* Remove `Pry::Config::Memoization`

* Remove confusing use of `instance_eval`

* Add `Pry::Config.defaults`
  Returns an instance of `Pry::Config`, initialized with Pry defaults.
  Replaces the previous role of `Pry::Config::Default`.
This commit is contained in:
r-obert 2018-12-01 08:51:52 +01:00 committed by GitHub
parent 642588dfe9
commit b5538b8501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 90 additions and 246 deletions

View file

@ -114,8 +114,6 @@ require 'pry/basic_object'
require "pry/prompt"
require 'pry/config/lazy'
require 'pry/config/behavior'
require 'pry/config/memoization'
require 'pry/config/default'
require 'pry/config/convenience'
require 'pry/config'
require 'pry/pry_class'

View file

@ -1,5 +1,5 @@
class Pry::BasicObject < BasicObject
[:Kernel, :Pry].each do |constant|
[:Kernel, :File, :Dir, :LoadError, :ENV, :Pry].each do |constant|
const_set constant, ::Object.const_get(constant)
end
include Kernel

View file

@ -4,8 +4,87 @@ class Pry
class Config < Pry::BasicObject
include Behavior
#
# @return [Pry::Config]
# An object who implements the default configuration for all
# Pry sessions.
#
def self.defaults
defaults = from_hash(
input: Pry.lazy { lazy_readline(defaults) },
output: $stdout.tap { |out| out.sync = true },
commands: Pry::Commands,
prompt_name: Pry::Prompt::DEFAULT_NAME,
prompt: Pry::Prompt[:default],
prompt_safe_contexts: Pry::Prompt::SAFE_CONTEXTS,
print: Pry::DEFAULT_PRINT,
quiet: false,
exception_handler: Pry::DEFAULT_EXCEPTION_HANDLER,
unrescued_exceptions: Pry::DEFAULT_UNRESCUED_EXCEPTIONS,
exception_whitelist: Pry.lazy do
defaults.output.puts '[warning] Pry.config.exception_whitelist is deprecated, ' \
'please use Pry.config.unrescued_exceptions instead.'
Pry::DEFAULT_UNRESCUED_EXCEPTIONS
end,
hooks: Pry::DEFAULT_HOOKS,
pager: true,
system: Pry::DEFAULT_SYSTEM,
color: Pry::Helpers::BaseHelpers.use_ansi_codes?,
default_window_size: 5,
editor: Pry.default_editor_for_platform,
should_load_rc: true,
should_load_local_rc: true,
should_trap_interrupts: Pry::Helpers::Platform.jruby?,
disable_auto_reload: false,
command_prefix: "",
auto_indent: Pry::Helpers::BaseHelpers.use_ansi_codes?,
correct_indent: true,
collision_warning: false,
output_prefix: "=> ",
requires: [],
should_load_requires: true,
should_load_plugins: true,
windows_console_warning: true,
control_d_handler: Pry::DEFAULT_CONTROL_D_HANDLER,
memory_size: 100,
extra_sticky_locals: {},
command_completions: Pry.lazy { defaults.commands.keys },
file_completions: proc { Dir["."] },
ls: Pry::Config.from_hash(Pry::Command::Ls::DEFAULT_OPTIONS),
completer: Pry::InputCompleter,
gist: Pry::Config.from_hash(inspecter: proc(&:pretty_inspect)),
history: Pry::Config.from_hash(should_save: true, should_load: true).tap do |history|
history_file = if File.exist?(File.expand_path('~/.pry_history'))
'~/.pry_history'
elsif ENV.key?('XDG_DATA_HOME') && ENV['XDG_DATA_HOME'] != ''
# See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
ENV['XDG_DATA_HOME'] + '/pry/pry_history'
else
'~/.local/share/pry/pry_history'
end
history.file = File.expand_path(history_file)
end,
exec_string: ""
)
end
def self.shortcuts
Convenience::SHORTCUTS
end
# @api private
def self.lazy_readline(defaults)
require 'readline'
::Readline
rescue LoadError
defaults.output.puts "Sorry, you can't use Pry without Readline or a compatible library. \n" \
"Possible solutions: \n" \
" * Rebuild Ruby with Readline support using `--with-readline` \n" \
" * Use the rb-readline gem, which is a pure-Ruby port of Readline \n" \
" * Use the pry-coolline gem, a pure-ruby alternative to Readline"
raise
end
private_class_method :lazy_readline
end
end

View file

@ -273,7 +273,7 @@ class Pry
#
# @example
# # _pry_.config -> Pry.config -> Pry::Config::Default
# # _pry_.config -> Pry.config -> Pry::Config.defaults
# _pry_.config.last_default
#
# @return [Pry::Config::Behaviour]

View file

@ -1,162 +0,0 @@
class Pry
class Config < Pry::BasicObject
class Default
include Config::Behavior
include Config::Memoization
def_memoized({
input: proc {
lazy_readline
},
output: proc {
$stdout.tap { |out| out.sync = true }
},
commands: proc {
Pry::Commands
},
prompt_name: proc {
Pry::Prompt::DEFAULT_NAME
},
prompt: proc {
Pry::Prompt[:default]
},
prompt_safe_contexts: proc {
Pry::Prompt::SAFE_CONTEXTS
},
print: proc {
Pry::DEFAULT_PRINT
},
quiet: proc {
false
},
exception_handler: proc {
Pry::DEFAULT_EXCEPTION_HANDLER
},
unrescued_exceptions: proc {
Pry::DEFAULT_UNRESCUED_EXCEPTIONS
},
exception_whitelist: proc {
warn 'Pry.config.exception_whitelist is deprecated, please use Pry.config.unrescued_exceptions instead.'
Pry::DEFAULT_UNRESCUED_EXCEPTIONS
},
hooks: proc {
Pry::DEFAULT_HOOKS
},
pager: proc {
true
},
system: proc {
Pry::DEFAULT_SYSTEM
},
color: proc {
Pry::Helpers::BaseHelpers.use_ansi_codes?
},
default_window_size: proc {
5
},
editor: proc {
Pry.default_editor_for_platform
},
should_load_rc: proc {
true
},
should_load_local_rc: proc {
true
},
should_trap_interrupts: proc {
Pry::Helpers::Platform.jruby?
},
disable_auto_reload: proc {
false
},
command_prefix: proc {
""
},
auto_indent: proc {
Pry::Helpers::BaseHelpers.use_ansi_codes?
},
correct_indent: proc {
true
},
collision_warning: proc {
false
},
output_prefix: proc {
"=> "
},
requires: proc {
[]
},
should_load_requires: proc {
true
},
should_load_plugins: proc {
true
},
windows_console_warning: proc {
true
},
control_d_handler: proc {
Pry::DEFAULT_CONTROL_D_HANDLER
},
memory_size: proc {
100
},
extra_sticky_locals: proc {
{}
},
command_completions: proc {
proc { commands.keys }
},
file_completions: proc {
proc { Dir["."] }
},
ls: proc {
Pry::Config.from_hash(Pry::Command::Ls::DEFAULT_OPTIONS)
},
completer: proc {
Pry::InputCompleter
},
gist: proc {
Pry::Config.from_hash({inspecter: proc(&:pretty_inspect)})
},
history: proc {
Pry::Config.from_hash({should_save: true, should_load: true}).tap do |history|
history_file =
if File.exist?(File.expand_path('~/.pry_history'))
'~/.pry_history'
elsif ENV.key?('XDG_DATA_HOME') && ENV['XDG_DATA_HOME'] != ''
# See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
ENV['XDG_DATA_HOME'] + '/pry/pry_history'
else
'~/.local/share/pry/pry_history'
end
history.file = File.expand_path(history_file)
end
},
exec_string: proc {
""
}
})
def initialize
super(nil)
end
private
def lazy_readline
require 'readline'
Readline
rescue LoadError
warn "Sorry, you can't use Pry without Readline or a compatible library."
warn "Possible solutions:"
warn " * Rebuild Ruby with Readline support using `--with-readline`"
warn " * Use the rb-readline gem, which is a pure-Ruby port of Readline"
warn " * Use the pry-coolline gem, a pure-ruby alternative to Readline"
raise
end
end
end
end

View file

@ -1,18 +1,15 @@
class Pry
class Config < Pry::BasicObject
# Wraps a block so it can have a name.
# The primary purpose for instances of this class is to be used as a
# configuration value that is computed upon each access, see {Pry.lazy}
# for more information.
# for more examples.
#
# @example
# proc1 = proc {}
# proc2 = Pry::Config::Lazy.new(&proc {})
#
# proc1.is_a?(Pry::Config::Lazy)
# #=> false
# proc2.is_a?(Pry::Config::Lazy)
# #=> true
# num = 19
# _pry_.config.foo = Pry::Config::Lazy.new(&proc { num += 1 })
# _pry_.config.foo # => 20
# _pry_.config.foo # => 21
# _pry_.config.foo # => 22
#
# @api private
# @since v0.12.0

View file

@ -1,48 +0,0 @@
class Pry
class Config < Pry::BasicObject
module Memoization
MEMOIZED_METHODS = Hash.new { |h,k| h[k] = [] }
module ClassMethods
#
# Defines one or more methods who return a constant value after being
# called once.
#
# @example
# class Foo
# include Pry::Config::Memoization
# def_memoized({
# foo: proc {1+10},
# bar: proc{"aaa"<<"a"}
# })
# end
#
# @param [{String => Proc}] method_table
#
# @return [void]
#
def def_memoized(method_table)
method_table.each do |method_name, method|
define_method(method_name) do
method_table[method_name] = instance_eval(&method) if method_table[method_name].equal? method
method_table[method_name]
end
end
MEMOIZED_METHODS[self] |= method_table.keys
end
end
def self.included(mod)
mod.extend(ClassMethods)
end
#
# @return [Array<Symbol>]
# Returns the names of methods that have been defined by {ClassMethods#def_memoized}.
#
def memoized_methods
MEMOIZED_METHODS[self.class]
end
end
end
end

View file

@ -338,7 +338,7 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
@initial_session = true
@session_finalized = nil
self.config = Pry::Config.new Pry::Config::Default.new
self.config = Pry::Config.new Pry::Config.defaults
self.cli = false
self.current_line = 1
self.line_buffer = [""]

View file

@ -55,7 +55,7 @@ module Pry::Testable
# @return [void]
#
def self.set_testenv_variables
Pry.config = Pry::Config.from_hash(TEST_DEFAULTS, Pry::Config::Default.new)
Pry.config = Pry::Config.from_hash TEST_DEFAULTS, Pry::Config.defaults
Pry.config.hooks = Pry::Hooks.new
end
@ -65,6 +65,6 @@ module Pry::Testable
# @return [void]
#
def self.unset_testenv_variables
Pry.config = Pry::Config.from_hash({}, Pry::Config::Default.new)
Pry.config = Pry::Config.from_hash({}, Pry::Config.defaults)
end
end

View file

@ -1,20 +0,0 @@
RSpec.describe Pry::Config::Memoization do
let(:config) do
Class.new do
include Pry::Config::Memoization
def_memoized({foo: proc { "foo" }, bar: proc { "bar" }})
end.new
end
describe "on call of method" do
it "memoizes the return value" do
expect(config.foo).to be(config.foo)
end
end
describe "#memoized_methods" do
it "tracks a list of memoized methods" do
expect(config.memoized_methods).to eq([:foo, :bar])
end
end
end