mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
add Pry::Config::Behavior.
a Module Pry::Config and Pry::Config::Default can use to share common behavior. before now ::Default was a subclass of Pry::Config but are class methods(such as Pry::Config.from_hash()) that do not make sense on Pry::Config::Default.
This commit is contained in:
parent
7c63654206
commit
19c301696b
3 changed files with 62 additions and 56 deletions
|
@ -1,8 +1,9 @@
|
|||
class Pry::Config
|
||||
require 'ostruct'
|
||||
require 'pry/config/behavior'
|
||||
require 'pry/config/default'
|
||||
require 'pry/config/convenience'
|
||||
ASSIGNMENT = "=".freeze
|
||||
include Pry::Config::Behavior
|
||||
|
||||
def self.shortcuts
|
||||
Convenience::SHORTCUTS
|
||||
|
@ -14,56 +15,6 @@ class Pry::Config
|
|||
end
|
||||
end
|
||||
|
||||
def initialize(default = Pry.config)
|
||||
@default = default
|
||||
@lookup = {}
|
||||
end
|
||||
|
||||
def [](key)
|
||||
@lookup[key.to_s]
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
@lookup[key.to_s] = value
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
key = name.to_s
|
||||
if key[-1] == ASSIGNMENT
|
||||
short_key = key[0..-2]
|
||||
self[short_key] = args[0]
|
||||
elsif @lookup.has_key?(key)
|
||||
self[key]
|
||||
elsif @default.respond_to?(name)
|
||||
@default.public_send(name, *args, &block)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def merge!(other)
|
||||
raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash)
|
||||
other = other.to_hash
|
||||
keys, values = other.keys.map(&:to_s), other.values
|
||||
@lookup.merge! Hash[keys.zip(values)]
|
||||
end
|
||||
|
||||
def respond_to?(name, boolean=false)
|
||||
@lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean)
|
||||
end
|
||||
|
||||
def refresh
|
||||
@lookup = {}
|
||||
end
|
||||
|
||||
def to_hash
|
||||
@lookup
|
||||
end
|
||||
|
||||
def to_h
|
||||
@lookup
|
||||
end
|
||||
|
||||
#
|
||||
# FIXME
|
||||
# @param [Pry::Hooks] hooks
|
||||
|
@ -77,8 +28,4 @@ class Pry::Config
|
|||
self["hooks"] = hooks
|
||||
end
|
||||
end
|
||||
|
||||
def quiet?
|
||||
quiet
|
||||
end
|
||||
end
|
||||
|
|
57
lib/pry/config/behavior.rb
Normal file
57
lib/pry/config/behavior.rb
Normal file
|
@ -0,0 +1,57 @@
|
|||
module Pry::Config::Behavior
|
||||
ASSIGNMENT = "=".freeze
|
||||
|
||||
def initialize(default = Pry.config)
|
||||
@default = default
|
||||
@lookup = {}
|
||||
end
|
||||
|
||||
def [](key)
|
||||
@lookup[key.to_s]
|
||||
end
|
||||
|
||||
def []=(key, value)
|
||||
@lookup[key.to_s] = value
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
key = name.to_s
|
||||
if key[-1] == ASSIGNMENT
|
||||
short_key = key[0..-2]
|
||||
self[short_key] = args[0]
|
||||
elsif @lookup.has_key?(key)
|
||||
self[key]
|
||||
elsif @default.respond_to?(name)
|
||||
@default.public_send(name, *args, &block)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def merge!(other)
|
||||
raise TypeError, "cannot coerce argument to Hash" unless other.respond_to?(:to_hash)
|
||||
other = other.to_hash
|
||||
keys, values = other.keys.map(&:to_s), other.values
|
||||
@lookup.merge! Hash[keys.zip(values)]
|
||||
end
|
||||
|
||||
def respond_to?(name, boolean=false)
|
||||
@lookup.has_key?(name.to_s) or @default.respond_to?(name) or super(name, boolean)
|
||||
end
|
||||
|
||||
def refresh
|
||||
@lookup = {}
|
||||
end
|
||||
|
||||
def to_hash
|
||||
@lookup
|
||||
end
|
||||
|
||||
def to_h
|
||||
@lookup
|
||||
end
|
||||
|
||||
def quiet?
|
||||
quiet
|
||||
end
|
||||
end
|
|
@ -1,4 +1,6 @@
|
|||
class Pry::Config::Default < Pry::Config
|
||||
class Pry::Config::Default
|
||||
include Pry::Config::Behavior
|
||||
|
||||
default = {
|
||||
:input => proc { Readline },
|
||||
:output => proc { $stdout },
|
||||
|
|
Loading…
Reference in a new issue