1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/config/memoization.rb
Kyrylo Silin 4b6fc303bb config: simplify structure
* Replace `require_relative` with `require`
  The project tries to use the `require` form everywhere where possible, which
  is the common form
* `require` from `pry.rb`
  Spreaded `require` statements where we require internal classes is confusing
* Fixed namespace definition for Config classes
  https://github.com/rubocop-hq/ruby-style-guide#namespace-definition recommends
  to use explicit nesting
2018-10-28 17:45:29 +08:00

48 lines
1.3 KiB
Ruby

class Pry
class Config < Pry::BasicObject
module Memoization
MEMOIZED_METHODS = Hash.new {|h,k| h[k] = [] }
module ClassMethods
#
# Defines one or more methods who return a constant value after being
# called once.
#
# @example
# class Foo
# include Pry::Config::Memoization
# def_memoized({
# foo: proc {1+10},
# bar: proc{"aaa"<<"a"}
# })
# end
#
# @param [{String => Proc}] method_table
#
# @return [void]
#
def def_memoized(method_table)
method_table.each do |method_name, method|
define_method(method_name) do
method_table[method_name] = instance_eval(&method) if method_table[method_name].equal? method
method_table[method_name]
end
end
MEMOIZED_METHODS[self] |= method_table.keys
end
end
def self.included(mod)
mod.extend(ClassMethods)
end
#
# @return [Array<Symbol>]
# Returns the names of methods that have been defined by {ClassMethods#def_memoized}.
#
def memoized_methods
MEMOIZED_METHODS[self.class]
end
end
end
end