Module: Pry::Config::Behavior

Included in:
Pry::Config, Default
Defined in:
lib/pry/config/behavior.rb

Defined Under Namespace

Modules: Builder

Constant Summary

ASSIGNMENT =
"=".freeze
NODUP =
[TrueClass, FalseClass, NilClass, Symbol, Numeric, Module, Proc].freeze
INSPECT_REGEXP =
/#{Regexp.escape "default=#<"}/

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pry/config/behavior.rb', line 52

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

Class Method Details

.included(klass) ⇒ Object



14
15
16
17
18
19
# File 'lib/pry/config/behavior.rb', line 14

def self.included(klass)
  unless defined?(RESERVED_KEYS)
    const_set :RESERVED_KEYS, instance_methods(false).map(&:to_s).freeze
  end
  klass.extend(Builder)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



75
76
77
# File 'lib/pry/config/behavior.rb', line 75

def ==(other)
  @lookup == __try_convert_to_hash(other)
end

#[](key) ⇒ Object



34
35
36
37
# File 'lib/pry/config/behavior.rb', line 34

def [](key)
  key = key.to_s
  @lookup[key] or (@default and @default[key])
end

#[]=(key, value) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/pry/config/behavior.rb', line 39

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
  __push(key,value)
end

#__clip_inspect(obj) ⇒ Object (private)



115
116
117
# File 'lib/pry/config/behavior.rb', line 115

def __clip_inspect(obj)
  "#{obj.class}:0x%x" % obj.object_id << 1
end

#__dup(value) ⇒ Object (private)



131
132
133
134
135
136
137
# File 'lib/pry/config/behavior.rb', line 131

def __dup(value)
  if NODUP.any? { |klass| klass === value }
    value
  else
    value.dup
  end
end

#__push(key, value) ⇒ Object (private)



139
140
141
142
143
# File 'lib/pry/config/behavior.rb', line 139

def __push(key,value)
  define_singleton_method(key) { self[key] }
  define_singleton_method("#{key}=") { |value| @lookup[key] = value }
  @lookup[key] = value
end

#__remove(key) ⇒ Object (private)



145
146
147
# File 'lib/pry/config/behavior.rb', line 145

def __remove(key)
  @lookup.delete(key)
end

#__try_convert_to_hash(obj) ⇒ Object (private)



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pry/config/behavior.rb', line 119

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

#clearObject Also known as: refresh



89
90
91
92
# File 'lib/pry/config/behavior.rb', line 89

def clear
  @lookup.clear
  true
end

#defaultPry::Config::Behavior



30
31
32
# File 'lib/pry/config/behavior.rb', line 30

def default
  @default
end

#forget(key) ⇒ Object



47
48
49
50
# File 'lib/pry/config/behavior.rb', line 47

def forget(key)
  key = key.to_s
  __remove(key)
end

#initialize(default = Pry.config) ⇒ Object



21
22
23
24
# File 'lib/pry/config/behavior.rb', line 21

def initialize(default = Pry.config)
  @default = default
  @lookup = {}
end

#inspectObject



105
106
107
108
# File 'lib/pry/config/behavior.rb', line 105

def inspect
  key_str = keys.map { |key| "'#{key}'" }.join(",")
  "#<#{__clip_inspect(self)} local_keys=[#{key_str}] default=#{@default.inspect}>"
end

#key?(key) ⇒ Boolean



84
85
86
87
# File 'lib/pry/config/behavior.rb', line 84

def key?(key)
  key = key.to_s
  @lookup.key?(key)
end

#keysObject



95
96
97
# File 'lib/pry/config/behavior.rb', line 95

def keys
  @lookup.keys
end

#merge!(other) ⇒ Object

Raises:

  • (TypeError)


67
68
69
70
71
72
73
# File 'lib/pry/config/behavior.rb', line 67

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

#pretty_print(q) ⇒ Object



110
111
112
# File 'lib/pry/config/behavior.rb', line 110

def pretty_print(q)
  q.text inspect[1..-1].gsub(INSPECT_REGEXP, "default=<")
end

#respond_to_missing?(key, include_private = false) ⇒ Boolean



80
81
82
# File 'lib/pry/config/behavior.rb', line 80

def respond_to_missing?(key, include_private=false)
  key?(key) or @default.respond_to?(key) or super(key, include_private)
end

#to_hashObject Also known as: to_h



99
100
101
# File 'lib/pry/config/behavior.rb', line 99

def to_hash
  @lookup.dup
end