From 2e00481ab7dfa35deaa980c3100a974349dc345f Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Mon, 28 Dec 2015 11:26:11 +0000 Subject: [PATCH 1/8] make 'Pry::Config::Default' less of a special case by adding Pry::Config::Lazy --- lib/pry/config.rb | 1 + lib/pry/config/behavior.rb | 2 +- lib/pry/config/default.rb | 15 +++------------ lib/pry/config/lazy.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 lib/pry/config/lazy.rb diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 11b0cd34..9ac184b8 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,6 +1,7 @@ require_relative 'basic_object' class Pry::Config < Pry::BasicObject require_relative 'config/behavior' + require_relative 'config/lazy' require_relative 'config/default' require_relative 'config/convenience' include Pry::Config::Behavior diff --git a/lib/pry/config/behavior.rb b/lib/pry/config/behavior.rb index da17c725..04c55d94 100644 --- a/lib/pry/config/behavior.rb +++ b/lib/pry/config/behavior.rb @@ -142,7 +142,7 @@ module Pry::Config::Behavior def eager_load! local_last_default = last_default - local_last_default.default_keys.each do |key| + local_last_default.lazy_keys.each do |key| self[key] = local_last_default.public_send(key) end end diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index bf50bb46..0eaf42ec 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,5 +1,6 @@ class Pry::Config::Default include Pry::Config::Behavior + include Pry::Config::Lazy default = { input: proc { @@ -121,19 +122,9 @@ class Pry::Config::Default configure_gist configure_history end + lazy_implement(default) - default.each do |key, value| - define_method(key) do - if default[key].equal?(value) - default[key] = instance_eval(&value) - end - default[key] - end - end - - define_method(:default_keys) { default.keys } - -private + private # TODO: # all of this configure_* stuff is a relic of old code. # we should try move this code to being command-local. diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb new file mode 100644 index 00000000..c32f8076 --- /dev/null +++ b/lib/pry/config/lazy.rb @@ -0,0 +1,26 @@ +module Pry::Config::Lazy + LAZY_KEYS = {} + LAZY_KEYS.default_proc = lambda {|h,k| h[k] = [] } + + module ExtendModule + def lazy_implement(method_name_to_func) + method_name_to_func.each do |method_name, func| + define_method(method_name) do + if method_name_to_func[method_name].equal?(func) + method_name_to_func[method_name] = instance_eval(&func) + LAZY_KEYS[self.class] |= method_name_to_func.keys + end + method_name_to_func[method_name] + end + end + end + end + + def self.included(includer) + includer.extend(ExtendModule) + end + + def lazy_keys + LAZY_KEYS[self.class] + end +end From 098ea17b490768ba37b84fc16da293be2cbc0487 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Mon, 28 Dec 2015 11:41:19 +0000 Subject: [PATCH 2/8] update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be785d37..4513b057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,7 @@ * Implemented support for CDPATH for ShellCommand ([#1433](https://github.com/pry/pry/issues/1433), [#1434](https://github.com/pry/pry/issues/1434)) * `Pry::CLI.parse_options` does not start Pry anymore ([#1393](https://github.com/pry/pry/pull/1393)) * The gem uses CPU-less platforms for Windows now ([#1410](https://github.com/pry/pry/pull/1410)) +* Add `Pry::Config::Lazy` to make it easier to reimplement `Pry::Config::Default` without knowing its implementation [#1503](https://github.com/pry/pry/pull/1503/) ### 0.10.1 From b221a8f90c81f48784857ff9e9da2a25e54a95f5 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Mon, 28 Dec 2015 11:45:30 +0000 Subject: [PATCH 3/8] remove 'default' local --- lib/pry/config/default.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pry/config/default.rb b/lib/pry/config/default.rb index 0eaf42ec..8c7a1dfa 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -2,7 +2,7 @@ class Pry::Config::Default include Pry::Config::Behavior include Pry::Config::Lazy - default = { + lazy_implement({ input: proc { lazy_readline }, @@ -115,14 +115,13 @@ class Pry::Config::Default exec_string: proc { "" } - } + }) def initialize super(nil) configure_gist configure_history end - lazy_implement(default) private # TODO: From bf057cc348e5c6c4b3975909f2b20700637f09a4 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Mon, 28 Dec 2015 11:54:22 +0000 Subject: [PATCH 4/8] fix return value of lazy method --- lib/pry/config/lazy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb index c32f8076..e63f2862 100644 --- a/lib/pry/config/lazy.rb +++ b/lib/pry/config/lazy.rb @@ -7,8 +7,8 @@ module Pry::Config::Lazy method_name_to_func.each do |method_name, func| define_method(method_name) do if method_name_to_func[method_name].equal?(func) - method_name_to_func[method_name] = instance_eval(&func) LAZY_KEYS[self.class] |= method_name_to_func.keys + method_name_to_func[method_name] = instance_eval(&func) end method_name_to_func[method_name] end From ba34c6a4cbdf9c7d9add05507017887b72a33217 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Tue, 29 Dec 2015 02:33:40 +0000 Subject: [PATCH 5/8] use the more common approach to assigning key default of [] --- lib/pry/config/lazy.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb index e63f2862..fc3eb101 100644 --- a/lib/pry/config/lazy.rb +++ b/lib/pry/config/lazy.rb @@ -1,6 +1,5 @@ module Pry::Config::Lazy - LAZY_KEYS = {} - LAZY_KEYS.default_proc = lambda {|h,k| h[k] = [] } + LAZY_KEYS = Hash.new {|h,k| h[k] = [] } module ExtendModule def lazy_implement(method_name_to_func) From 983daf6fd5c592442a66b687dabf8098bc3ad049 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Tue, 29 Dec 2015 06:50:49 +0000 Subject: [PATCH 6/8] rename module --- lib/pry/config/lazy.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb index fc3eb101..59c705fc 100644 --- a/lib/pry/config/lazy.rb +++ b/lib/pry/config/lazy.rb @@ -1,7 +1,7 @@ module Pry::Config::Lazy LAZY_KEYS = Hash.new {|h,k| h[k] = [] } - module ExtendModule + module ClassMethods def lazy_implement(method_name_to_func) method_name_to_func.each do |method_name, func| define_method(method_name) do @@ -16,7 +16,7 @@ module Pry::Config::Lazy end def self.included(includer) - includer.extend(ExtendModule) + includer.extend(ClassMethods) end def lazy_keys From 398a62bf79d1d9e614438e8153fdd0bdfd05eaa3 Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Thu, 31 Dec 2015 03:59:53 +0000 Subject: [PATCH 7/8] add lazy_spec.rb --- spec/config/lazy_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 spec/config/lazy_spec.rb diff --git a/spec/config/lazy_spec.rb b/spec/config/lazy_spec.rb new file mode 100644 index 00000000..7f512333 --- /dev/null +++ b/spec/config/lazy_spec.rb @@ -0,0 +1,13 @@ +require 'helper' +RSpec.describe Pry::Config::Lazy do + let(:lazyobject) do + Class.new do + include Pry::Config::Lazy + lazy_implement({foo: proc {"bar"}}) + end.new + end + + it 'memorizes value after first call' do + expect(lazyobject.foo).to equal(lazyobject.foo) + end +end From 6531140f068f639f17b385d9671e36b836643adc Mon Sep 17 00:00:00 2001 From: strcmp <28xdb6+fvutuvy4f@grr.la> Date: Thu, 31 Dec 2015 15:25:33 -0800 Subject: [PATCH 8/8] add tests --- spec/config/behavior_spec.rb | 16 ++++++++++++++++ spec/config/lazy_spec.rb | 17 +++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 spec/config/behavior_spec.rb diff --git a/spec/config/behavior_spec.rb b/spec/config/behavior_spec.rb new file mode 100644 index 00000000..5f62991f --- /dev/null +++ b/spec/config/behavior_spec.rb @@ -0,0 +1,16 @@ +require 'helper' +RSpec.describe Pry::Config::Behavior do + let(:behavior) do + Class.new do + include Pry::Config::Behavior + end + end + + describe "#last_default" do + it "returns the last default in a list of defaults" do + last = behavior.from_hash({}, nil) + middle = behavior.from_hash({}, last) + expect(behavior.from_hash({}, middle).last_default).to be(last) + end + end +end diff --git a/spec/config/lazy_spec.rb b/spec/config/lazy_spec.rb index 7f512333..69884f20 100644 --- a/spec/config/lazy_spec.rb +++ b/spec/config/lazy_spec.rb @@ -1,13 +1,22 @@ require 'helper' RSpec.describe Pry::Config::Lazy do - let(:lazyobject) do + let(:lazyobj) do Class.new do include Pry::Config::Lazy - lazy_implement({foo: proc {"bar"}}) + lazy_implement({foo: proc {"foo"}, bar: proc {"bar"}}) end.new end - it 'memorizes value after first call' do - expect(lazyobject.foo).to equal(lazyobject.foo) + describe "on call of a lazy method" do + it "memoizes the return value" do + expect(lazyobj.foo).to be(lazyobj.foo) + end + end + + describe "#lazy_keys" do + it "tracks a list of lazy keys" do + lazyobj.foo # at least one lazy method has to be called before #lazy_keys could return a non-empty array. + expect(lazyobj.lazy_keys).to eq([:foo, :bar]) + end end end