Module: Lite::Memoize::Alias
- Defined in:
- lib/lite/memoize/alias.rb
Defined Under Namespace
Modules: InstanceMethods Classes: MemoizedMethod
Class Method Summary collapse
- .escape_punctuation(string) ⇒ Object
-
.extended(extender) ⇒ Object
rubocop:disable Lint/NestedMethodDefinition.
- .memoist_eval(klass, *args, &block) ⇒ Object
-
.memoized_ivar_for(method_name, as = nil) ⇒ Object
rubocop:enable Lint/NestedMethodDefinition.
- .memoized_prefix(as = nil) ⇒ Object
- .unmemoized_method_for(method_name, as = nil) ⇒ Object
- .unmemoized_prefix(as = nil) ⇒ Object
Instance Method Summary collapse
- #all_memoized_structs ⇒ Object
- #clear_structs ⇒ Object
-
#memoize(*method_names) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity.
Class Method Details
.escape_punctuation(string) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/lite/memoize/alias.rb', line 76 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
rubocop:disable Lint/NestedMethodDefinition
45 46 47 48 49 50 51 52 53 |
# File 'lib/lite/memoize/alias.rb', line 45 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
83 84 85 86 87 |
# File 'lib/lite/memoize/alias.rb', line 83 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
rubocop:enable Lint/NestedMethodDefinition
56 57 58 |
# File 'lib/lite/memoize/alias.rb', line 56 def memoized_ivar_for(method_name, as = nil) "@#{memoized_prefix(as)}_#{escape_punctuation(method_name)}" end |
.memoized_prefix(as = nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/lite/memoize/alias.rb', line 64 def memoized_prefix(as = nil) return '_memoized' unless as "_memoized_#{as}" end |
.unmemoized_method_for(method_name, as = nil) ⇒ Object
60 61 62 |
# File 'lib/lite/memoize/alias.rb', line 60 def unmemoized_method_for(method_name, as = nil) "#{unmemoized_prefix(as)}_#{method_name}".to_sym end |
.unmemoized_prefix(as = nil) ⇒ Object
70 71 72 73 74 |
# File 'lib/lite/memoize/alias.rb', line 70 def unmemoized_prefix(as = nil) return '_unmemoized' unless as "_unmemoized_#{as}" end |
Instance Method Details
#all_memoized_structs ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lite/memoize/alias.rb', line 91 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_structs ⇒ Object
107 108 109 |
# File 'lib/lite/memoize/alias.rb', line 107 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
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 184 185 |
# File 'lib/lite/memoize/alias.rb', line 113 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 |