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

140 lines
2.8 KiB
Ruby
Raw Normal View History

module Pry::Config::Behavior
ASSIGNMENT = "=".freeze
NODUP = [TrueClass, FalseClass, NilClass, Symbol, Numeric, Module, Proc].freeze
INSPECT_REGEXP = /#{Regexp.escape "default=#<"}/
2014-03-17 07:27:57 -04:00
module Builder
def from_hash(hash, default = nil)
new(default).tap do |config|
config.merge!(hash)
end
end
end
def self.included(klass)
unless defined?(RESERVED_KEYS)
const_set :RESERVED_KEYS, instance_methods(false).map(&:to_s).freeze
end
2014-03-17 07:27:57 -04:00
klass.extend(Builder)
end
2014-01-27 01:16:10 -05:00
def initialize(default = Pry.config)
@default = default
2014-03-14 21:57:07 -04:00
@lookup = {}
end
2014-03-14 00:40:09 -04:00
#
# @return [Pry::Config::Behavior]
# returns the default used if a matching value for a key isn't found in self
2014-03-14 00:40:09 -04:00
#
def default
@default
end
def [](key)
2014-03-14 21:57:07 -04:00
@lookup[key.to_s]
end
def []=(key, value)
key = key.to_s
if RESERVED_KEYS.include?(key)
raise ArgumentError, "few things are reserved by pry, but using '#{key}' as a configuration key is."
end
2014-03-14 21:57:07 -04:00
@lookup[key] = 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 key?(key)
self[key]
elsif @default.respond_to?(name)
value = @default.public_send(name, *args, &block)
# FIXME: refactor Pry::Hook so that it stores config on the config object,
# so that we can use the normal strategy.
self[key] = value.dup if key == 'hooks'
value
else
nil
end
end
def merge!(other)
2014-03-23 04:57:59 -04:00
other = try_convert_to_hash(other)
raise TypeError, "unable to convert argument into a Hash" unless other
other.each do |key, value|
self[key] = value
end
end
2014-03-23 04:57:59 -04:00
def ==(other)
@lookup == try_convert_to_hash(other)
2014-03-23 04:57:59 -04:00
end
alias_method :eql?, :==
def respond_to_missing?(key, include_private=false)
key?(key) or @default.respond_to?(key) or super(key, include_private)
2014-01-27 01:02:55 -05:00
end
def key?(key)
key = key.to_s
2014-03-14 21:57:07 -04:00
@lookup.key?(key)
end
2014-03-14 22:05:19 -04:00
def clear
2014-03-14 21:57:07 -04:00
@lookup.clear
true
end
2014-03-14 22:05:19 -04:00
alias_method :refresh, :clear
def forget(key)
2014-03-14 21:57:07 -04:00
@lookup.delete(key.to_s)
end
2014-02-02 17:23:23 -05:00
def keys
2014-03-14 21:57:07 -04:00
@lookup.keys
2014-02-02 17:23:23 -05:00
end
def to_hash
2014-03-14 21:57:07 -04:00
@lookup.dup
end
alias_method :to_h, :to_hash
def inspect
key_str = keys.map { |key| "'#{key}'" }.join(",")
"#<#{_clip_inspect(self)} local_keys=[#{key_str}] default=#{@default.inspect}>"
end
def pretty_print(q)
q.text inspect[1..-1].gsub(INSPECT_REGEXP, "default=<")
end
private
def _clip_inspect(obj)
"#{obj.class}:0x%x" % obj.object_id << 1
end
def _dup(value)
if NODUP.any? { |klass| klass === value }
value
else
value.dup
end
end
2014-03-23 04:57:59 -04:00
def try_convert_to_hash(obj)
if Hash === obj
obj
elsif obj.respond_to?(:to_h)
obj.to_h
elsif obj.respond_to?(:to_hash)
obj.to_hash
else
nil
end
end
end