Module: Pry::Config::Behavior

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

Defined Under Namespace

Modules: Builder

Constant Summary collapse

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


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pry/config/behavior.rb', line 46

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 = value.dup if key == 'hooks'
    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?


72
73
74
# File 'lib/pry/config/behavior.rb', line 72

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

#[](key) ⇒ Object


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

def [](key)
  @lookup[key.to_s]
end

#[]=(key, value) ⇒ Object


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

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
  @lookup[key] = value
end

#clearObject Also known as: refresh


86
87
88
89
# File 'lib/pry/config/behavior.rb', line 86

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


92
93
94
# File 'lib/pry/config/behavior.rb', line 92

def forget(key)
  @lookup.delete(key.to_s)
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


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

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

#key?(key) ⇒ Boolean


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

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

#keysObject


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

def keys
  @lookup.keys
end

#merge!(other) ⇒ Object

Raises:

  • (TypeError)

64
65
66
67
68
69
70
# File 'lib/pry/config/behavior.rb', line 64

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


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

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

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


77
78
79
# File 'lib/pry/config/behavior.rb', line 77

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


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

def to_hash
  @lookup.dup
end