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=#<"}/
ReservedKeyError =
Class.new(RuntimeError)

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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/pry/config/behavior.rb', line 175

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



27
28
29
# File 'lib/pry/config/behavior.rb', line 27

def self.included(klass)
  klass.extend(Builder)
end

Instance Method Details

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

Parameters:

  • other (Hash, #to_h, #to_hash)

    a hash to compare against the lookup table of self.



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

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

#[](key) ⇒ Object, BasicObject

Returns an object from self or one of its defaults.

Parameters:

  • key (String)

    a key (as a String)

Returns:



52
53
54
55
# File 'lib/pry/config/behavior.rb', line 52

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

#[]=(key, value) ⇒ Object

Add a key and value pair to self.

Parameters:

Raises:



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

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

#clearvoid

This method returns an undefined value.

Clear the lookup table of self.



130
131
132
133
# File 'lib/pry/config/behavior.rb', line 130

def clear
  @lookup.clear
  true
end

#defaultPry::Config::Behavior

Returns the default used incase a key isn’t found in self.

Returns:



41
42
43
# File 'lib/pry/config/behavior.rb', line 41

def default
  @default
end

#eager_load!Object



143
144
145
146
147
148
149
# File 'lib/pry/config/behavior.rb', line 143

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

#forget(key) ⇒ void

This method returns an undefined value.

Removes a key from self.

Parameters:

  • key (String)

    a key (as a String)



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

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

#initialize(default = Pry.config) ⇒ Object



31
32
33
34
35
# File 'lib/pry/config/behavior.rb', line 31

def initialize(default = Pry.config)
  @default = default
  @lookup = {}
  @reserved_keys = methods.map(&:to_s).freeze
end

#inspectObject



166
167
168
169
# File 'lib/pry/config/behavior.rb', line 166

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

#key?(key) ⇒ Boolean

Returns true when “key” is a member of self.

Parameters:

  • key (String)

    a key (as a String)

Returns:

  • (Boolean)

    returns true when “key” is a member of self.



120
121
122
123
# File 'lib/pry/config/behavior.rb', line 120

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

#keysArray<String>

Returns an array of keys in self.

Returns:

  • (Array<String>)

    returns an array of keys in self.



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

def keys
  @lookup.keys
end

#last_defaultObject



151
152
153
154
155
# File 'lib/pry/config/behavior.rb', line 151

def last_default
  last = @default
  last = last.default while last and last.default
  last
end

#merge!(other) ⇒ void

This method returns an undefined value.

Parameters:

  • other (Hash, #to_h, #to_hash)

    a hash to merge into self.

Raises:

  • (TypeError)


96
97
98
99
100
101
102
# File 'lib/pry/config/behavior.rb', line 96

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



171
172
173
# File 'lib/pry/config/behavior.rb', line 171

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

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

Returns:

  • (Boolean)


190
191
192
# File 'lib/pry/config/behavior.rb', line 190

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

#to_hashHash Also known as: to_h

Returns a duplicate copy of the lookup table used by self.

Returns:

  • (Hash)

    returns a duplicate copy of the lookup table used by self.



161
162
163
# File 'lib/pry/config/behavior.rb', line 161

def to_hash
  @lookup.dup
end