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
- #==(other) ⇒ Object (also: #eql?)
-
#[](key) ⇒ Object, BasicObject
Returns an object from self or one of its defaults.
-
#[]=(key, value) ⇒ Object
Add a key and value pair to self.
-
#clear ⇒ void
Clear the lookup table of self.
-
#default ⇒ Pry::Config::Behavior
Returns the default used incase a key isn’t found in self.
- #eager_load! ⇒ Object
-
#forget(key) ⇒ void
Removes a key from self.
- #initialize(default = Pry.config) ⇒ Object
- #inspect ⇒ Object
-
#key?(key) ⇒ Boolean
Returns true when “key” is a member of self.
-
#keys ⇒ Array<String>
Returns an array of keys in self.
- #last_default ⇒ Object
- #merge!(other) ⇒ void
- #method_missing(name, *args, &block) ⇒ Object
- #pretty_print(q) ⇒ Object
- #respond_to_missing?(key, include_all = false) ⇒ Boolean
-
#to_hash ⇒ Hash
(also: #to_h)
Returns a duplicate copy of the lookup table used by self.
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
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
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.
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.
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 |
#clear ⇒ void
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 |
#default ⇒ Pry::Config::Behavior
Returns the default used incase a key isn’t found in self.
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.
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 |
#inspect ⇒ Object
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.
120 121 122 123 |
# File 'lib/pry/config/behavior.rb', line 120 def key?(key) key = key.to_s @lookup.key?(key) end |
#keys ⇒ 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_default ⇒ Object
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.
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
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_hash ⇒ Hash Also known as: to_h
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 |