Skip to content

Commit

Permalink
make 'Pry::Config::Default' less of a special case by adding Pry::Con…
Browse files Browse the repository at this point in the history
…fig::Lazy
  • Loading branch information
strcmp committed Dec 28, 2015
1 parent 55f170d commit 2e00481
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/pry/config.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pry/config/behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def keys

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
Expand Down
15 changes: 3 additions & 12 deletions lib/pry/config/default.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Pry::Config::Default
include Pry::Config::Behavior
include Pry::Config::Lazy

default = {
input: proc {
Expand Down Expand Up @@ -121,19 +122,9 @@ def initialize
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.
Expand Down
26 changes: 26 additions & 0 deletions lib/pry/config/lazy.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2e00481

Please sign in to comment.