\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/tins-1.25.0/lib/tins/

Viewing File: memoize.rb

require 'tins/extract_last_argument_options'

module Tins
  module Memoize
    module CacheMethods
      # Return the cache object.
      def __memoize_cache__
        @__memoize_cache__ ||= {}
      end

      # Clear cached values for all methods/functions.
      def memoize_cache_clear
         __memoize_cache__.clear
        self
      end

      def memoize_apply_visibility(id)
        visibility = instance_eval do
          case
          when private_method_defined?(id)
            :private
          when protected_method_defined?(id)
            :protected
          end
        end
        yield
      ensure
        visibility and __send__(visibility, id)
      end
    end

    class ::Module
      # Automatically memoize calls of the the methods +method_ids+. The
      # memoized results do NOT ONLY depend on the arguments, but ALSO on the
      # object the method is called on.
      def memoize_method(*method_ids)
        method_ids.extend(ExtractLastArgumentOptions)
        method_ids, opts = method_ids.extract_last_argument_options
        include CacheMethods
        method_ids.each do |method_id|
          method_id = method_id.to_s.to_sym
          memoize_apply_visibility method_id do
            orig_method = instance_method(method_id)
            __send__(:define_method, method_id) do |*args|
              mc = __memoize_cache__
              if mc.key?(method_id) and result = mc[method_id][args]
                result
              else
                (mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
                $DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
                opts[:freeze] and result.freeze
              end
              result
            end
          end
        end
        method_ids.size == 1 ? method_ids.first : method_ids
      end

      include CacheMethods

      # Automatically memoize calls of the functions +function_ids+. The
      # memoized result does ONLY depend on the arguments given to the
      # function.
      def memoize_function(*function_ids)
        function_ids.extend(ExtractLastArgumentOptions)
        function_ids, opts = function_ids.extract_last_argument_options
        mc = __memoize_cache__
        function_ids.each do |function_id|
          function_id = function_id.to_s.to_sym
          memoize_apply_visibility function_id do
            orig_function = instance_method(function_id)
            __send__(:define_method, function_id) do |*args|
              if mc.key?(function_id) and result = mc[function_id][args]
                result
              else
                (mc[function_id] ||= {})[args] = result = orig_function.bind(self).call(*args)
                opts[:freeze] and result.freeze
                $DEBUG and warn "#{self.class} cached function #{function_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
              end
              result
            end
          end
        end
        function_ids.size == 1 ? function_ids.first : function_ids
      end
    end
  end
end

require 'tins/alias'