Module: Lite::Memoize::Alias

Defined in:
lib/lite/memoize/alias.rb

Defined Under Namespace

Modules: InstanceMethods Classes: MemoizedMethod

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape_punctuation(string) ⇒ Object



74
75
76
77
78
79
# File 'lib/lite/memoize/alias.rb', line 74

def escape_punctuation(string)
  string = string.is_a?(String) ? string.dup : string.to_s
  return string unless string.end_with?('?', '!')

  string.sub!(/\?\Z/, '_query') || string.sub!(/!\Z/, '_bang') || string
end

.extended(extender) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/lite/memoize/alias.rb', line 44

def extended(extender)
  Lite::Memoize::Alias.memoist_eval(extender) do
    return if singleton_class.method_defined?(:memoized_methods)

    def self.memoized_methods
      @_memoized_methods ||= []
    end
  end
end

.memoist_eval(klass, *args, &block) ⇒ Object



81
82
83
84
85
# File 'lib/lite/memoize/alias.rb', line 81

def memoist_eval(klass, *args, &block)
  return klass.class_eval(*args, &block) if klass.respond_to?(:class_eval)

  klass.singleton_class.class_eval(*args, &block)
end

.memoized_ivar_for(method_name, as = nil) ⇒ Object



54
55
56
# File 'lib/lite/memoize/alias.rb', line 54

def memoized_ivar_for(method_name, as = nil)
  "@#{memoized_prefix(as)}_#{escape_punctuation(method_name)}"
end

.memoized_prefix(as = nil) ⇒ Object



62
63
64
65
66
# File 'lib/lite/memoize/alias.rb', line 62

def memoized_prefix(as = nil)
  return '_memoized' unless as

  "_memoized_#{as}"
end

.unmemoized_method_for(method_name, as = nil) ⇒ Object



58
59
60
# File 'lib/lite/memoize/alias.rb', line 58

def unmemoized_method_for(method_name, as = nil)
  "#{unmemoized_prefix(as)}_#{method_name}".to_sym
end

.unmemoized_prefix(as = nil) ⇒ Object



68
69
70
71
72
# File 'lib/lite/memoize/alias.rb', line 68

def unmemoized_prefix(as = nil)
  return '_unmemoized' unless as

  "_unmemoized_#{as}"
end

Instance Method Details

#all_memoized_structsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lite/memoize/alias.rb', line 89

def all_memoized_structs
  @all_memoized_structs ||= begin
    structs = memoized_methods.dup

    ancestors.grep(Lite::Memoize::Alias).each do |ancestor|
      ancestor.memoized_methods.each do |m|
        next if structs.any? { |am| am.memoized_method == m.memoized_method }

        structs << m
      end
    end

    structs
  end
end

#clear_structsObject



105
106
107
# File 'lib/lite/memoize/alias.rb', line 105

def clear_structs
  @all_memoized_structs = nil
end

#memoize(*method_names) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/lite/memoize/alias.rb', line 111

def memoize(*method_names)
  as = method_names.pop[:as] if method_names.last.is_a?(Hash)

  method_names.each do |method_name|
    unmemoized_method = Lite::Memoize::Alias.unmemoized_method_for(method_name, as)
    memoized_ivar = Lite::Memoize::Alias.memoized_ivar_for(method_name, as)

    Lite::Memoize::Alias.memoist_eval(self) do
      include InstanceMethods

      # rubocop:disable Lint/NonLocalExitFromIterator
      if method_defined?(unmemoized_method)
        warn "Already memoized #{method_name}"
        return
      end
      # rubocop:enable Lint/NonLocalExitFromIterator

      alias_method unmemoized_method, method_name

      mm = MemoizedMethod.new(method_name, memoized_ivar, instance_method(method_name).arity)
      memoized_methods << mm

      if mm.arity.zero?
        module_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{method_name}(reload: false)
            skip_cache = reload || !instance_variable_defined?("#{memoized_ivar}")
            set_cache = skip_cache && !frozen?

            if skip_cache
              value = #{unmemoized_method}
            else
              value = #{memoized_ivar}
            end

            if set_cache
              #{memoized_ivar} = value
            end

            value
          end
        RUBY
      else
        module_eval <<-RUBY, __FILE__, __LINE__ + 1
          def #{method_name}(*args, reload: false)
            skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.key?(args))
            set_cache = skip_cache && !frozen?

            if skip_cache
              value = #{unmemoized_method}(*args)
            else
              value = #{memoized_ivar}[args]
            end

            if set_cache
              #{memoized_ivar} ||= {}
              #{memoized_ivar}[args] = value
            end

            value
          end
        RUBY
      end

      if private_method_defined?(unmemoized_method)
        private method_name
      elsif protected_method_defined?(unmemoized_method)
        protected method_name
      end
    end
  end

  method_names.size == 1 ? method_names.first : method_names
end