module Pry::Config::Behavior

Constants

ASSIGNMENT
INSPECT_REGEXP
NODUP
ReservedKeyError

Public Class Methods

included(klass) click to toggle source
# File lib/pry/config/behavior.rb, line 46
def self.included(klass)
  klass.extend(Builder)
end
new(default = Pry.config) click to toggle source
# File lib/pry/config/behavior.rb, line 50
def initialize(default = Pry.config)
  @default = default
  @lookup = {}
  @reserved_keys = methods.map(&:to_s).freeze
end

Public Instance Methods

==(other) click to toggle source

@param [Hash, to_h, to_hash] other

a hash to compare against the lookup table of self.
# File lib/pry/config/behavior.rb, line 129
def ==(other)
  @lookup == __try_convert_to_hash(other)
end
Also aliased as: eql?
[](key) click to toggle source

@param [String] key

a key (as a String)

@return [Object, BasicObject]

returns an object from self or one of its defaults.
# File lib/pry/config/behavior.rb, line 71
def [](key)
  key = key.to_s
  key?(key) ? @lookup[key] : (@default and @default[key])
end
[]=(key, value) click to toggle source

Add a key and value pair to self.

@param [String] key

a key (as a String).

@param [Object,BasicObject] value

a value.

@raise [Pry::Config::ReservedKeyError]

when 'key' is a reserved key name.
# File lib/pry/config/behavior.rb, line 88
def []=(key, value)
  key = key.to_s
  if @reserved_keys.include?(key)
    raise ReservedKeyError, "It is not possible to use '#{key}' as a key name, please choose a different key name."
  end

  __push(key,value)
end
clear() click to toggle source

Clear the lookup table of self.

@return [void]

# File lib/pry/config/behavior.rb, line 151
def clear
  @lookup.clear
  true
end
default() click to toggle source

@return [Pry::Config::Behavior]

returns the default used incase a key isn't found in self.
# File lib/pry/config/behavior.rb, line 60
def default
  @default
end
eager_load!() click to toggle source
# File lib/pry/config/behavior.rb, line 164
def eager_load!
  default = @default
  while default
    default.memoized_methods.each { |method| self[key] = default.public_send(key) } if default.respond_to?(:memoized_methods)
    default = @default.default
  end
end
eql?(other)
Alias for: ==
forget(key) click to toggle source

Removes a key from self.

@param [String] key

a key (as a String)

@return [void]

# File lib/pry/config/behavior.rb, line 105
def forget(key)
  key = key.to_s
  __remove(key)
end
inspect() click to toggle source
# File lib/pry/config/behavior.rb, line 187
def inspect
  key_str = keys.map { |key| "'#{key}'" }.join(",")
  "#<#{__clip_inspect(self)} keys=[#{key_str}] default=#{@default.inspect}>"
end
key?(key) click to toggle source

@param [String] key

a key (as a String)

@return [Boolean]

returns true when "key" is a member of self.
# File lib/pry/config/behavior.rb, line 141
def key?(key)
  key = key.to_s
  @lookup.key?(key)
end
keys() click to toggle source

@return [Array<String>]

returns an array of keys in self.
# File lib/pry/config/behavior.rb, line 160
def keys
  @lookup.keys
end
last_default() click to toggle source
# File lib/pry/config/behavior.rb, line 172
def last_default
  last = @default
  last = last.default while last and last.default
  last
end
merge!(other) click to toggle source

@param [Hash, to_h, to_hash] other

a hash to merge into self.

@return [void]

# File lib/pry/config/behavior.rb, line 116
def merge!(other)
  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
method_missing(name, *args, &block) click to toggle source
# File lib/pry/config/behavior.rb, line 196
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)
    self[key] = __dup(value)
  else
    nil
  end
end
pretty_print(q) click to toggle source
# File lib/pry/config/behavior.rb, line 192
def pretty_print(q)
  q.text inspect[1..-1].gsub(INSPECT_REGEXP, "default=<")
end
respond_to_missing?(key, include_all = false) click to toggle source
Calls superclass method
# File lib/pry/config/behavior.rb, line 211
def respond_to_missing?(key, include_all = false)
  key = key.to_s.chomp(ASSIGNMENT)
  key?(key) or @default.respond_to?(key) or super(key, include_all)
end
to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [Hash]

returns a duplicate copy of the lookup table used by self.
# File lib/pry/config/behavior.rb, line 182
def to_hash
  @lookup.dup
end
Also aliased as: to_h

Private Instance Methods

__clip_inspect(obj) click to toggle source
# File lib/pry/config/behavior.rb, line 218
def __clip_inspect(obj)
  "#{obj.class}:0x%x" % obj.object_id
end
__dup(value) click to toggle source
# File lib/pry/config/behavior.rb, line 234
def __dup(value)
  if NODUP.any? { |klass| klass === value }
    value
  else
    value.dup
  end
end
__push(key,value) click to toggle source
# File lib/pry/config/behavior.rb, line 242
def __push(key,value)
  unless singleton_class.method_defined? key
    define_singleton_method(key) { self[key] }
    define_singleton_method("#{key}=") { |val| @lookup[key] = val }
  end
  @lookup[key] = value
end
__remove(key) click to toggle source
# File lib/pry/config/behavior.rb, line 250
def __remove(key)
  @lookup.delete(key)
end
__try_convert_to_hash(obj) click to toggle source
# File lib/pry/config/behavior.rb, line 222
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