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



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pry/config/behavior.rb', line 192

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



44
45
46
# File 'lib/pry/config/behavior.rb', line 44

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.



125
126
127
# File 'lib/pry/config/behavior.rb', line 125

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:



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

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:



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

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

#__clip_inspect(obj) ⇒ Object (private)



213
214
215
# File 'lib/pry/config/behavior.rb', line 213

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

#__dup(value) ⇒ Object (private)



229
230
231
232
233
234
235
# File 'lib/pry/config/behavior.rb', line 229

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

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



237
238
239
240
241
242
243
# File 'lib/pry/config/behavior.rb', line 237

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) ⇒ Object (private)



245
246
247
# File 'lib/pry/config/behavior.rb', line 245

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

#__try_convert_to_hash(obj) ⇒ Object (private)



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/pry/config/behavior.rb', line 217

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

#clear

This method returns an undefined value.

Clear the lookup table of self.



147
148
149
150
# File 'lib/pry/config/behavior.rb', line 147

def clear
  @lookup.clear
  true
end

#defaultPry::Config::Behavior

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

Returns:



58
59
60
# File 'lib/pry/config/behavior.rb', line 58

def default
  @default
end

#eager_load!Object



160
161
162
163
164
165
166
# File 'lib/pry/config/behavior.rb', line 160

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)

This method returns an undefined value.

Removes a key from self.

Parameters:

  • key (String)

    a key (as a String)



102
103
104
105
# File 'lib/pry/config/behavior.rb', line 102

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

#initialize(default = Pry.config) ⇒ Object



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

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

#inspectObject



183
184
185
186
# File 'lib/pry/config/behavior.rb', line 183

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.



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

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.



156
157
158
# File 'lib/pry/config/behavior.rb', line 156

def keys
  @lookup.keys
end

#last_defaultObject



168
169
170
171
172
# File 'lib/pry/config/behavior.rb', line 168

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

#merge!(other)

This method returns an undefined value.

Parameters:

  • other (Hash, #to_h, #to_hash)

    a hash to merge into self.

Raises:

  • (TypeError)


113
114
115
116
117
118
119
# File 'lib/pry/config/behavior.rb', line 113

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



188
189
190
# File 'lib/pry/config/behavior.rb', line 188

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

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

Returns:

  • (Boolean)


207
208
209
210
# File 'lib/pry/config/behavior.rb', line 207

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_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.



178
179
180
# File 'lib/pry/config/behavior.rb', line 178

def to_hash
  @lookup.dup
end