diff options
author | r-obert <r-obert@users.noreply.github.com> | 2018-12-01 08:51:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-01 08:51:52 +0100 |
commit | b5538b850118c427e3b99ed351d60518d6cf8fe4 (patch) | |
tree | ef4e5bad4f66337632f062f97b2a69cf30837926 | |
parent | 642588dfe9bf7fa5c431451857a9ab0ed6fd33b6 (diff) | |
download | pry-b5538b850118c427e3b99ed351d60518d6cf8fe4.tar.gz |
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`.
-rw-r--r-- | lib/pry.rb | 2 | ||||
-rw-r--r-- | lib/pry/basic_object.rb | 2 | ||||
-rw-r--r-- | lib/pry/config.rb | 79 | ||||
-rw-r--r-- | lib/pry/config/behavior.rb | 2 | ||||
-rw-r--r-- | lib/pry/config/default.rb | 162 | ||||
-rw-r--r-- | lib/pry/config/lazy.rb | 15 | ||||
-rw-r--r-- | lib/pry/config/memoization.rb | 48 | ||||
-rw-r--r-- | lib/pry/pry_class.rb | 2 | ||||
-rw-r--r-- | lib/pry/testable.rb | 4 | ||||
-rw-r--r-- | spec/config/memoization_spec.rb | 20 |
10 files changed, 90 insertions, 246 deletions
@@ -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' diff --git a/lib/pry/basic_object.rb b/lib/pry/basic_object.rb index fa2dccfb..ab4b03e2 100644 --- a/lib/pry/basic_object.rb +++ b/lib/pry/basic_object.rb @@ -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 diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 1cd12f5b..5b0dd9b8 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -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 diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index f819ae56..9d6c936a 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -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] diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb deleted file mode 100644 index 5a2f70f9..00000000 --- a/lib/pry/config/default.rb +++ /dev/null @@ -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 diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb index a521f918..4718465f 100644 --- a/lib/pry/config/lazy.rb +++ b/lib/pry/config/lazy.rb @@ -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 diff --git a/lib/pry/config/memoization.rb b/lib/pry/config/memoization.rb deleted file mode 100644 index f2e24388..00000000 --- a/lib/pry/config/memoization.rb +++ /dev/null @@ -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 diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index b849717f..72618388 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -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 = [""] diff --git a/lib/pry/testable.rb b/lib/pry/testable.rb index afc4dc25..695eded3 100644 --- a/lib/pry/testable.rb +++ b/lib/pry/testable.rb @@ -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 diff --git a/spec/config/memoization_spec.rb b/spec/config/memoization_spec.rb deleted file mode 100644 index 59a3353e..00000000 --- a/spec/config/memoization_spec.rb +++ /dev/null @@ -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 |