diff options
author | r-obert <robert@jazzonmymind.xyz> | 2017-06-23 06:12:11 +0100 |
---|---|---|
committer | r-obert <robert@jazzonmymind.xyz> | 2017-06-23 06:18:19 +0100 |
commit | 505cccf69193f979ec67cd2bebf4ff9f409cf0d6 (patch) | |
tree | a402d4b6a33994a5504e7dd0e53269ea0986f79c | |
parent | fa67de2f98c3ef00295ebc4ec84b3441d17afa72 (diff) | |
download | pry-505cccf69193f979ec67cd2bebf4ff9f409cf0d6.tar.gz |
rename Pry::Config::Lazy to Pry::Config::Memoization (ref #1503)
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | lib/pry/config.rb | 2 | ||||
-rw-r--r-- | lib/pry/config/behavior.rb | 2 | ||||
-rw-r--r-- | lib/pry/config/default.rb | 4 | ||||
-rw-r--r-- | lib/pry/config/lazy.rb | 25 | ||||
-rw-r--r-- | lib/pry/config/memoization.rb | 23 | ||||
-rw-r--r-- | spec/config/lazy_spec.rb | 21 | ||||
-rw-r--r-- | spec/config/memoization_spec.rb | 21 |
8 files changed, 49 insertions, 51 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 48960572..482e771c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,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/) +* Add `Pry::Config::Memoization` to make it easier to implement your own `Pry::Config::Default` class.([#1503](https://github.com/pry/pry/pull/1503/)) * Lazy load the config defaults for `Pry.config.history` and `Pry.config.gist`. ### 0.10.1 diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 9ac184b8..4f702863 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -1,7 +1,7 @@ require_relative 'basic_object' class Pry::Config < Pry::BasicObject require_relative 'config/behavior' - require_relative 'config/lazy' + require_relative 'config/memoization' 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 719dd5e6..ea834642 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.lazy_keys.each do |key| + local_last_default.memoized_methods.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 bc5da406..e4ae3fb6 100644 --- a/lib/pry/config/default.rb +++ b/lib/pry/config/default.rb @@ -1,8 +1,8 @@ class Pry::Config::Default include Pry::Config::Behavior - include Pry::Config::Lazy + include Pry::Config::Memoization - lazy_implement({ + def_memoized({ input: proc { lazy_readline }, diff --git a/lib/pry/config/lazy.rb b/lib/pry/config/lazy.rb deleted file mode 100644 index 9c44e65c..00000000 --- a/lib/pry/config/lazy.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Pry::Config::Lazy - LAZY_KEYS = Hash.new {|h,k| h[k] = [] } - - module ClassMethods - 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) - end - method_name_to_func[method_name] - end - end - LAZY_KEYS[self] |= method_name_to_func.keys - end - end - - def self.included(includer) - includer.extend(ClassMethods) - end - - def lazy_keys - LAZY_KEYS[self.class] - end -end diff --git a/lib/pry/config/memoization.rb b/lib/pry/config/memoization.rb new file mode 100644 index 00000000..fe2e4fbc --- /dev/null +++ b/lib/pry/config/memoization.rb @@ -0,0 +1,23 @@ +module Pry::Config::Memoization + MEMOIZED_METHODS = Hash.new {|h,k| h[k] = [] } + + module ClassMethods + 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 + + def memoized_methods + MEMOIZED_METHODS[self.class] + end +end diff --git a/spec/config/lazy_spec.rb b/spec/config/lazy_spec.rb deleted file mode 100644 index 89ae9a27..00000000 --- a/spec/config/lazy_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'helper' -RSpec.describe Pry::Config::Lazy do - let(:lazyobj) do - Class.new do - include Pry::Config::Lazy - lazy_implement({foo: proc {"foo"}, bar: proc {"bar"}}) - end.new - end - - 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 - expect(lazyobj.lazy_keys).to eq([:foo, :bar]) - end - end -end diff --git a/spec/config/memoization_spec.rb b/spec/config/memoization_spec.rb new file mode 100644 index 00000000..7eabab66 --- /dev/null +++ b/spec/config/memoization_spec.rb @@ -0,0 +1,21 @@ +require 'helper' +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 |