1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/config/behavior.rb
Robert Gleeson 19c301696b 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.
2014-01-21 00:23:08 +01:00

57 lines
1 KiB
Ruby

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