Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1503 from pry/pry-lazy
make 'Pry::Config::Default' less of a special case by adding Pry::Config::Lazy
- Loading branch information
Showing
7 changed files
with
70 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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) | ||
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 | ||
end | ||
end | ||
end | ||
|
||
def self.included(includer) | ||
includer.extend(ClassMethods) | ||
end | ||
|
||
def lazy_keys | ||
LAZY_KEYS[self.class] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 | ||
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 |