aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/memoizable.rb
blob: 67ed10d58db6fdccc55dc17170d3605a922cdf5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module ActiveSupport
  module Memoizable
    def self.memoized_ivar_for(symbol)
      "@_memoized_#{symbol.to_s.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')}".to_sym
    end

    module InstanceMethods
      def self.included(base)
        base.class_eval do
          unless base.method_defined?(:freeze_without_memoizable)
            alias_method_chain :freeze, :memoizable
          end
        end
      end

      def freeze_with_memoizable
        memoize_all unless frozen?
        freeze_without_memoizable
      end

      def memoize_all
        prime_cache ".*"
      end

      def unmemoize_all
        flush_cache ".*"
      end

      def prime_cache(*syms)
        syms.each do |sym|
          methods.each do |m|
            if m.to_s =~ /^_unmemoized_(#{sym})/
              if method(m).arity == 0
                __send__($1)
              else
                ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
                instance_variable_set(ivar, {})
              end
            end
          end
        end
      end

      def flush_cache(*syms, &block)
        syms.each do |sym|
          methods.each do |m|
            if m.to_s =~ /^_unmemoized_(#{sym})/
              ivar = ActiveSupport::Memoizable.memoized_ivar_for($1)
              instance_variable_get(ivar).clear if instance_variable_defined?(ivar)
            end
          end
        end
      end
    end

    def memoize(*symbols)
      symbols.each do |symbol|
        original_method = :"_unmemoized_#{symbol}"
        memoized_ivar = ActiveSupport::Memoizable.memoized_ivar_for(symbol)

        class_eval <<-EOS, __FILE__, __LINE__ + 1
          include InstanceMethods

          raise "Already memoized #{symbol}" if method_defined?(:#{original_method}) # raise "Already memoized if_modified_since" if method_defined?(:__unmemoized_if_modified_since)
          alias #{original_method} #{symbol}                                         # alias __unmemoized_if_modified_since if_modified_since

          if instance_method(:#{symbol}).arity == 0                                  # if instance_method(:if_modified_since).arity == 0
            def #{symbol}(reload = false)                                            #   def if_modified_since(reload = false)
              if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty?    #     if reload || !defined?(@_memoized_if_modified_since) || @_memoized_if_modified_since.empty?
                #{memoized_ivar} = [#{original_method}.freeze]                       #       @_memoized_if_modified_since = [__unmemoized_if_modified_since.freeze]
              end                                                                    #     end
              #{memoized_ivar}[0]                                                    #     @_memoized_if_modified_since[0]
            end                                                                      #   end
          else                                                                       # else
            def #{symbol}(*args)                                                     #   def if_modified_since(*args)
              #{memoized_ivar} ||= {} unless frozen?                                 #     @_memoized_if_modified_since ||= {} unless frozen?
              reload = args.pop if args.last == true || args.last == :reload         #     reload = args.pop if args.last == true || args.last == :reload
                                                                                     #
              if defined?(#{memoized_ivar}) && #{memoized_ivar}                      #     if defined?(@_memoized_if_modified_since) && @_memoized_if_modified_since
                if !reload && #{memoized_ivar}.has_key?(args)                        #       if !reload && @_memoized_if_modified_since.has_key?(args)
                  #{memoized_ivar}[args]                                             #         @_memoized_if_modified_since[args]
                elsif #{memoized_ivar}                                               #       elsif @_memoized_if_modified_since
                  #{memoized_ivar}[args] = #{original_method}(*args).freeze          #         @_memoized_if_modified_since[args] = __unmemoized_if_modified_since(*args).freeze
                end                                                                  #       end
              else                                                                   #     else
                #{original_method}(*args)                                            #       __unmemoized_if_modified_since(*args)
              end                                                                    #     end
            end                                                                      #   end
          end                                                                        # end
        EOS
      end
    end
  end
end